weixin_43885794
湛蓝天
2019-01-24 12:10
采纳率: 100%
浏览 1.2k
已采纳

Java调用小票打印机,只能打印大约A4纸那么长,怎么能完全打印?

try {
// 通俗理解就是书、文档
Book book = new Book();
// 设置成竖打
PageFormat pf = new PageFormat();
pf.setOrientation(PageFormat.PORTRAIT );
// 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
Paper paper = new Paper();
paper.setSize(266.8, 30000);// 纸张大小
paper.setImageableArea(10, 15, 266.8, 30000);// A4(595 X
// 842)设置打印区域,其实0,0应该是72,72,因为A4纸的默认X,Y边距是72
pf.setPaper(paper);

        book.append(new PrintableAllOrder(orderInfo), pf);

        // 获取打印服务对象
        PrinterJob job = PrinterJob.getPrinterJob();
        // 设置打印类
        job.setPageable(book);
        //指定XP-80C打印机打印分单
        HashAttributeSet hs = new HashAttributeSet();
        String printerName=printName;
        hs.add(new PrinterName(printerName,null));
        PrintService[] pss = PrintServiceLookup.lookupPrintServices(DocFlavor.SERVICE_FORMATTED.PAGEABLE, hs);
        if(pss.length==0)
        {
            System.out.println("无法找到打印机:");
            throw new SimpleException("无法找到打印机:");

// return FootResult.ERROR("无法找到打印机");
}

        // 获取打印服务对象

        //添加指定的打印机
        job.setPrintService(pss[0]);
        //开始打印
        job.print();
    } catch (PrinterException e) {
        e.printStackTrace();
    }

// return FootResult.SUCCESS("已添加到打印队列");
}


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • weixin_43885794
    湛蓝天 2019-02-13 11:58
    已采纳

    public static void printClassfyGoods(OrderInfo orderInfo, String printName) {
    DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
    PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
    // 纵向打印
    pras.add(OrientationRequested.PORTRAIT);
    PrintService[] pss = PrintServiceLookup.lookupPrintServices(flavor,null);
    if(pss.length==0)
    {
    throw new SimpleException("无法找到打印机:");
    }else{
    for (PrintService pppp:pss
    ) {
    if(pppp.getName().equals(printName)){
    DocAttributeSet das = new HashDocAttributeSet();
    // 设置打印纸张的大小(以毫米为单位)
    das.add(new MediaPrintableArea(0, 0, 80, 3276, MediaPrintableArea.MM));
    // 获取打印服务对象
    DocPrintJob job = pppp.createPrintJob();
    PrintableClassfyGoods printableAllOrder=new PrintableClassfyGoods(orderInfo);
    Doc doc = new SimpleDoc(printableAllOrder,flavor,das);
    //添加指定的打印机
    try {
    job.print(doc,pras);
    } catch (PrintException e) {
    e.printStackTrace();
    }
    return;
    }
    }

        }
    }
    
    
    
    点赞 评论

相关推荐