A brief introduction to the operating environment:
Language: Java
Tool: eclipse
System: Windows 7
(The printing device does not have one yet, so only preview pictures can be provided)
Recently, the project needs to print shopping receipts for the mall. In daily life, we go to the supermarket to buy things. When checking out, the cashier will print a receipt. Generally, the mall also needs such a small function. The demo given in this article is an example of a 58mm thermal printer. If it is a printer of other paper types, just adjust the paper width.
package test;import java.awt.*;import java.awt.print.*;/** * Printer test class (58mm) * 1. The target printer must be set as the default printer* 2. The width of the print page is related to the specific printer. It is generally the width of the printing paper and needs to be configured as system parameters* 3. The width of a Chinese character is about 12 points*/public class PrintTest {public static void main(String[] args){if(PrinterJob.lookupPrintServices().length>0){/* Print format*/PageFormat pageFormat = new PageFormat();//Set the print starting point starts from the upper left corner, from left to right, from top to bottom, pageFormat.setOrientation(PageFormat.PORTRAIT);/* Print page format settings*/Paper paper = new Paper();//Set the print width (fixed, related to the specific printer) and height (related to the actual print content) paper.setSize(140, 450);//Set the print starting point coordinate, print width and height paper.setImageableArea(0, 0, 135, 450);pageFormat.setPaper(paper);//Create a print document Book book = new Book();book.append(new Printable() {@Override public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {if(pageIndex>0){return NO_SUCH_PAGE;}Graphics2D graphics2D = (Graphics2D) graphics;Font font = new Font("安体", Font.PLAIN, 5);graphics2D.setFont(font);drawString(graphics2D, "/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 119, 8);font = new Font("宋体", Font.PLAIN, 7);graphics2D.setFont(font);int yIndex = 30;int lineHeight = 10;int lineWidth = 120;Color defaultColor = graphics2D.getColor();Color grey = new Color(145, 145, 145);//Received information yIndex = drawString(graphics2D, "Consignee: Passerby A", 10, yIndex, lineWidth, lineHeight);yIndex = drawString(graphics2D, "Received Address: Baidu Building, No. 10 Shangdi Street, Haidian District, Beijing", 10, yIndex + lineHeight, lineWidth, lineHeight);//Received Information Border Stroke stroke = new BasicStroke(0.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL,0,new float[]{4, 4},0);graphics2D.setStroke(stroke);graphics2D.drawRect(5, 10, 129, yIndex);//Drugstore name lineWidth = 129;lineHeight = 8;graphics2D.setFont(new Font("宋体", Font.BOLD, 8));graphics2D.setColor(defaultColor);yIndex = drawString(graphics2D, "Beijing Pharmacy Retail Ticket", 5, yIndex + lineHeight + 20, lineWidth, 12);graphics2D.setFont(new Font("宋体", Font.PLAIN, 6));graphics2D.setColor(grey);yIndex = drawString(graphics2D, "Operator: Xiaoqing", 5, yIndex + lineHeight + 2, lineWidth, lineHeight);yIndex = drawString(graphics2D, "Date: 2017-01-05", 5 + lineWidth/2, yIndex, lineWidth, lineHeight);yIndex = drawString(graphics2D, "Product Name", 5, yIndex + lineHeight * 2 - 5, lineWidth, lineHeight);yIndex = drawString(graphics2D, "Specification", (lineWidth/10)*4, yIndex, lineWidth, lineHeight);yIndex = drawString(graphics2D, "unit price", (lineWidth/10)*8, yIndex, lineWidth, lineHeight);yIndex = drawString(graphics2D, "quantity", (lineWidth/10)*10, yIndex, lineWidth, lineHeight);for (int i=0; i<5; i++){graphics2D.setFont(new Font("宋体", Font.PLAIN, 7));yIndex = drawString(graphics2D, "E Vitamin B Complex Tablets 100 E Vitamin B Complex Tablets 100 Tablets", 5, yIndex + 15, (lineWidth/10)*7, 10);graphics2D.setFont(new Font("安安", Font.PLAIN, 6));graphics2D.setColor(grey);yIndex = drawString(graphics2D, "100 tablets/box", 5, yIndex + 11, lineWidth, lineHeight);yIndex = drawString(graphics2D, "14.50", (lineWidth/10)*8, yIndex, lineWidth, lineHeight);yIndex = drawString(graphics2D, "2", (lineWidth/10)*10, yIndex, lineWidth, lineHeight);graphics2D.setFont(new Font("宋体", Font.PLAIN, 7));yIndex = yIndex + 2;graphics2D.drawLine(5, yIndex, 5 + lineWidth, yIndex);}graphics2D.setColor(defaultColor);yIndex = drawString(graphics2D, "Member Name: Xiaoqing", 5, yIndex + lineHeight * 2, lineWidth, lineHeight);yIndex = drawString(graphics2D, "Total: 6", 5, yIndex + lineHeight, lineWidth, lineHeight);yIndex = drawString(graphics2D, "Total: 55.30", 5, yIndex + lineHeight, lineWidth, lineHeight);yIndex = drawString(graphics2D, "Cash: 100.00", 5, yIndex + lineHeight, lineWidth, lineHeight);yIndex = drawString(graphics2D, "change: 44.70", 5, yIndex + lineHeight, lineWidth, lineHeight);graphics2D.setFont(new Font("宋体", Font.PLAIN, 6));graphics2D.setColor(grey);yIndex = drawString(graphics2D, "Tel: 020-123456", 5, yIndex + lineHeight * 2, lineWidth, lineHeight);yIndex = drawString(graphics2D, "Address: Baidu Building, No. 10 Shangdi Street, Haidian District, Beijing", 5, yIndex + lineHeight, lineWidth, lineHeight);yIndex = yIndex + 20;graphics2D.drawLine(0, yIndex, 140, yIndex);return PAGE_EXISTS;}}, pageFormat);//Get the default printer PrinterJob printerJob = PrinterJob.getPrinterJob();printPageable(book);try {printerJob.print();}catch (PrinterException e) {e.printStackTrace();System.out.println("Print exception");}} else{System.out.println("Print service cannot be discovered");}}/** * String output* @param graphics2D brush* @param text Print text* @param x Print starting point x coordinate* @param y Print starting point y coordinate* @param lineWidth line width* @param lineHeight line height* @return Return to end point y coordinate*/private static int drawString(Graphics2D graphics2D, String text, int x, int y, int lineWidth, int lineHeight){FontMetrics fontMetrics = graphics2D.getFontMetrics();if(fontMetrics.stringWidth(text)<lineWidth){graphics2D.drawString(text, x, y);return y;} else{char[] chars = text.toCharArray();int charsWidth = 0;StringBuffer sb = new StringBuffer();for (int i=0; i<chars.length; i++){if((charsWidth + fontMetrics.charWidth(chars[i]))>lineWidth){graphics2D.drawString(sb.toString(), x, y);sb.setLength(0);y = y + lineHeight;charsWidth = fontMetrics.charWidth(chars[i]);sb.append(chars[i]);} else{charsWidth = charsWidth + fontMetrics.charWidth(chars[i]);sb.append(chars[i]);}}if(sb.length()>0){graphics2D.drawString(sb.toString(), x, y);y = y + lineHeight;}return y - lineHeight;}}}Running results:
Effect preview:
Summarize
Simply put, it is to write a Java program, save the output result as a "*.xps" format file, and output it by the printer, which is very simple. Hope it will be helpful to everyone. If you have any questions, please leave a message and point it out. Thank you friends for your support for this site.