这会像一个古老的问题,但对于那些正在寻找像我这样的答案的人,我希望它会有所帮助.
通过将printable转换为javax.print,只能使用java.awt事件的事件.以下是如何在不破坏pageFormat的情况下正确执行此操作.
// get default service
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
// this is your old awt job, which you can use to get default pageFormat
PrinterJob job = PrinterJob.getPrinterJob();
// set your printer service to old awt so that you can get the default paper
job.setPrintService(service);
// get the default page format from the printer you selected
PageFormat pageFormat = job.getPageFormat(null);
// do some paper stuff etc. here
// get your doc
DocPrintJob printJob = service.createPrintJob();
Book book = new Book();
// Print interface implements printable
book.append(new PrintInterface(), pageFormat);
// make your attr here
PrintRequestA