如何在java中没有用户交互的情况下将pdf打印到特定的托盘

Gal*_*ezu 7 java printing pdf

我正在尝试设置一个晚上运行的服务,将一堆发票和其他文件自动打印到一堆打印机上.截至目前,我可以打印好文件,但我需要能够指定一个托盘(一个带有我们公司的信头和一个带有白纸)我迄今为止所尝试的所有东西都没有用,我指定了PrintRequestAttribute集中的MediaTray属性,但似乎没有做任何事情.有人有这样的经历吗?

我用于测试的当前代码看起来像这样.

// Create a PDFFile from a File reference
File f = new File("C:\\File.pdf");
FileInputStream fis = new FileInputStream(f);
FileChannel fc = fis.getChannel();
ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
PDFFile pdfFile = new PDFFile(bb); // Create PDF Print Page
PDFPrintPage pages = new PDFPrintPage(pdfFile);

// Create Print Job
PrinterJob pjob = PrinterJob.getPrinterJob();
PageFormat pf = PrinterJob.getPrinterJob().defaultPage();
pjob.setJobName(f.getName());
Book book = new Book();
book.append(pages, pf, pdfFile.getNumPages());
pjob.setPageable(book);
// Send print job to default printer


PrintRequestAttributeSet aset=new HashPrintRequestAttributeSet();
aset.add(MediaTray.MIDDLE); //Used several of the tray options here
pjob.print(aset);
Run Code Online (Sandbox Code Playgroud)

mar*_*ens 0

你实际上用什么来打印PDF?仅当打印机直接支持 PDF 时,才能将 PDF 直接发送到打印机。否则,您需要使用 Java 库进行光栅化。有一篇博客文章建议从 JAva 打印 PDF 的方法,网址为http://www.jpedal.org/PDFblog/2010/01/printing-pdf-files-from-java/