我在尝试使用 Java 打印 PDF 文件时遇到问题。这是我的代码:
PdfReader readFtp = new PdfReader(); // This class is used for reading a PDF file
PDDocument document = readFtp.readFTPFile(documentID);
printRequestAttributeSet.add(new PageRanges(1, 10));
job.setPageable(document);
job.print(printRequestAttributeSet); // calling for print
document.close()
I use document.silentPrint(job);
and job.print(printRequestAttributeSet);
- 效果很好。如果我使用document.silentPrint(job);
- 我无法设置PrintRequestAttributeSet
.
谁能告诉我如何设置打印请求属性集?
我的打印机不支持本机 PDF 打印。
我使用了开源库 Apache PDFBoxhttps://pdfbox.apache.org https://pdfbox.apache.org打印 PDF。打印本身仍然由Java的PrinterJob处理。
import java.awt.print.PrinterJob;
import java.io.File;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.printing.PDFPageable;
public class PrintingExample {
public static void main(String args[]) throws Exception {
PDDocument document = PDDocument.load(new File("C:/temp/example.pdf"));
PrintService myPrintService = findPrintService("My Windows printer Name");
PrinterJob job = PrinterJob.getPrinterJob();
job.setPageable(new PDFPageable(document));
job.setPrintService(myPrintService);
job.print();
}
private static PrintService findPrintService(String printerName) {
PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
for (PrintService printService : printServices) {
if (printService.getName().trim().equals(printerName)) {
return printService;
}
}
return null;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)