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个回答

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;
}
}

    }
}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐