import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.print.PageFormat; import java.awt.print.Printable; import java.awt.print.PrinterException; import java.util.ArrayList; import java.util.List; /** * @description: 打印订单详情 * @author: zyh * @time: 2019/3/17 17:07 */ public class Print implements Printable{ // 菜品集合 public static List testList = new ArrayList(); // 设置小票打印 public int print(Graphics g, PageFormat pf, int page) throws PrinterException { if (page > 0) { return NO_SUCH_PAGE; } Graphics2D g2d = (Graphics2D) g; // 设置颜色 g2d.setColor(Color.BLACK); //模式 字体 字体大小 g2d.setFont(new Font("Default", Font.PLAIN, 16)); // 参数1:显示内容 参数2:横向偏移 参数3:纵向偏移 g2d.drawString("××××订单", 35, 50); g2d.setFont(new Font("Default", Font.PLAIN, 12)); g2d.drawString("------------------------------------------------", 0, 70); g2d.drawString("桌号:A1", 0, 90); g2d.drawString("人数:5", 0, 110); g2d.drawString("用户信息:15514769010", 0, 130); g2d.drawString("------------------------------------------------", 0, 150); g2d.drawString("订单详情", 0, 170); g2d.drawString("菜品 单价 小计", 0, 190); //行高 int H1 = 190; //总金额 double sunMoney = 0; //序号 int count = 0; List<Test> tests =getTestList(); for (Test test : tests) { count = count + 1; H1 = H1 + 20; sunMoney = test.getMoney() * test.getNum() + sunMoney; g2d.setFont(new Font("Default", Font.PLAIN, 8)); g2d.drawString(count + "." + test.getName() + "*" + test.getNum() + " ¥" + test.getMoney()+" ¥"+test.getMoney()*test.getNum(), 0, H1); } g2d.setFont(new Font("Default", Font.PLAIN, 10)); g2d.drawString("------------------------------------------------", 0, H1+20); g2d.drawString("总计商品数: 4", 0, H1+40); g2d.drawString("订单金额: 447.98", 0, H1+60); g2d.drawString("应付金额: 400.98", 0, H1+80); g2d.drawString("------------------------------------------------", 0, H1+100); g2d.drawString("订单编号: WW46789874552214", 0, H1+120); g2d.drawString("下单时间: 2021-04-09 11:11:11", 0, H1+140); g2d.drawString("核销时间: 2021-04-09 12:33:45", 0, H1+160); g2d.drawString("------------------------------------------------", 0, H1+180); g2d.drawString("订单备注内容: 这里是对于订单内容的备注信息", 0, H1+200); g2d.drawString("------------------------------------------------", 0, H1 + 220); g2d.drawString("感谢你选择【××××××××】", 0, H1+240); g2d.drawString(" 欢迎你再次光临!", 0, H1+255); return PAGE_EXISTS; } //打印的商品集合 public static List getTestList() { return testList; } //打印的商品集合 public static void setTestList(List testList) { Prient.testList = testList; } }
import java.awt.print.Book; import java.awt.print.PageFormat; import java.awt.print.Paper; import java.awt.print.PrinterException; import java.awt.print.PrinterJob; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import javax.print.DocFlavor; import javax.print.PrintService; import javax.print.PrintServiceLookup; import javax.print.attribute.HashAttributeSet; import javax.print.attribute.HashPrintRequestAttributeSet; import javax.print.attribute.standard.PrinterName; /** * @description: * @author: zyh * @time: 2021/3/17 17:13 */ public class MainTest { public static DocFlavor psInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE; public static HashPrintRequestAttributeSet hashPrintRequestAttributeSet = new HashPrintRequestAttributeSet(); public static PrintService printService[] = PrintServiceLookup.lookupPrintServices(psInFormat, hashPrintRequestAttributeSet); public static void main(String[] args) { System.out.println("================== 当前的打印设备列表为 =================="); if(printService.length > 0){ for (int i = 0; i < printService.length; i++) { System.out.println(printService[i]); } //我们指定要进行打印的打印机 ,此名称为电脑中设备中打印机的名称 dataModel58("XP-58"); dataModel80("XP-80C"); }else { System.out.println("***************** 没有打印设备 ****************"); } } /** * 设置数据模型,设定打印模式 */ public static void dataModel58(String printName){ //这里是测试用的自定义数据数据 List testList = new ArrayList(); Test t1 = new Test("麻辣牛肉", 23.00, 2); Test t2 = new Test("麻辣牛肉", 23.00, 4); Test t3 = new Test("精品千层肚", 24.00, 3); Test t4 = new Test("麻辣牛肉", 23.00, 2); testList.add(t1); testList.add(t2); testList.add(t3); testList.add(t4); //设置小票模型菜品集合 Prient.setTestList(testList); //定义纸张高度 int height = 200 + (testList.size() * 20) + 290; //通俗理解就是书、文档 Book book = new Book(); //打印格式 PageFormat pf = new PageFormat(); //原点在纸张的左上方,x 指向右方,y 指向下方。 pf.setOrientation(PageFormat.PORTRAIT); //通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。 Paper p = new Paper(); //页面宽度 页面高度 p.setSize(550, height); //x轴 y轴 宽度 高度 p.setImageableArea(0, 0, 550, height); pf.setPaper(p); //把 PageFormat 和 Printable 添加到书中,组成一个页面 book.append(new Prient(), pf); //指定打印机打印(printerName打印机名称) HashAttributeSet hs = new HashAttributeSet(); String printerName = printName; hs.add(new PrinterName(printerName, null)); PrintService[] pss = PrintServiceLookup.lookupPrintServices(null, hs); //获取打印服务对象 PrinterJob job = PrinterJob.getPrinterJob(); //写入书 job.setPageable(book); try { //添加指定的打印机 job.setPrintService(specifyPrinter(printName)); //打印执行 job.print(); } catch (PrinterException e) { System.out.println("打印出现异常:" +e); } } /** * 设置数据模型,设定打印模式 */ public static void dataModel80(String printName){ //这里是测试用的数据,自己定义就好了 List testList = new ArrayList(); Test t1 = new Test("麻辣牛肉", 23.00, 2); Test t2 = new Test("麻辣牛肉", 23.00, 4); Test t3 = new Test("精品千层肚", 24.00, 3); Test t4 = new Test("麻辣牛肉", 23.00, 2); testList.add(t1); testList.add(t2); testList.add(t3); testList.add(t4); //设置小票模型菜品集合 PrintOther.setTestList(testList); //定义纸张高度 int height = 200 + (testList.size() * 20) + 300; //通俗理解就是书、文档 Book book = new Book(); //打印格式 PageFormat pf = new PageFormat(); //原点在纸张的左上方,x 指向右方,y 指向下方。 pf.setOrientation(PageFormat.PORTRAIT); //通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。 Paper p = new Paper(); //页面宽度 页面高度 p.setSize(780, height); //x轴 y轴 宽度 高度 p.setImageableArea(0, 0, 780, height); pf.setPaper(p); //把 PageFormat 和 Printable 添加到书中,组成一个页面 book.append(new PrintOther(), pf); //指定打印机打印(printerName打印机名称) HashAttributeSet hs = new HashAttributeSet(); String printerName = printName; hs.add(new PrinterName(printerName, null)); PrintService[] pss = PrintServiceLookup.lookupPrintServices(null, hs); //获取打印服务对象 PrinterJob job = PrinterJob.getPrinterJob(); //写入书 job.setPageable(book); try { //添加指定的打印机 job.setPrintService(specifyPrinter(printName)); //打印执行 job.print(); } catch (PrinterException e) { System.out.println("打印出现异常:" +e); } } /** * 获取打印服务,如果找不到,则使用默认打印机 * @param printerName * @return */ public static PrintService specifyPrinter(String printerName) { PrintService myPrinter = null; for (PrintService printService2 : printService) { String svcName = printService2.toString(); if (svcName.contains(printerName)) { myPrinter = printService2; break; } } if (myPrinter == null) { System.out.println("***************** 使用默认设备开始打印 *****************"); myPrinter = PrintServiceLookup.lookupDefaultPrintService(); } return myPrinter; } }
如有错误,欢迎指正,请联系WX:15514769010