使用Java中的PrinterJob打印PDF文件

尝试使用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() 

我使用document.silentPrint(job);job.print(printRequestAttributeSet); – 它工作正常。 如果我使用document.silentPrint(job); – 我无法设置PrintRequestAttributeSet

谁能告诉我如何设置PrintRequestAttributeSet

我的打印机不支持原生PDF打印。

我使用开源库Apache PDFBox 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; } } 

这对我来说可以用普通的JRE打印PDF:

 public static void main(String[] args) throws PrintException, IOException { DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PAGEABLE; PrintRequestAttributeSet patts = new HashPrintRequestAttributeSet(); patts.add(Sides.DUPLEX); PrintService[] ps = PrintServiceLookup.lookupPrintServices(flavor, patts); if (ps.length == 0) { throw new IllegalStateException("No Printer found"); } System.out.println("Available printers: " + Arrays.asList(ps)); PrintService myService = null; for (PrintService printService : ps) { if (printService.getName().equals("Your printer name")) { myService = printService; break; } } if (myService == null) { throw new IllegalStateException("Printer not found"); } FileInputStream fis = new FileInputStream("C:/Users/John Doe/Desktop/SamplePDF.pdf"); Doc pdfDoc = new SimpleDoc(fis, DocFlavor.INPUT_STREAM.AUTOSENSE, null); DocPrintJob printJob = myService.createPrintJob(); printJob.print(pdfDoc, new HashPrintRequestAttributeSet()); fis.close(); } 

以下是我用打印对话框打印多个PDF文档的方法:

 public void printPDF() { PrinterJob printerJob = PrinterJob.getPrinterJob(); PrintService printService; if(printerJob.printDialog()) { printService = printerJob.getPrintService(); } DocFlavor docType = DocFlavor.INPUT_STREAM.AUTOSENSE; for (//fetch documents to be printed) { DocPrintJob printJob = printService.createPrintJob(); final byte[] byteStream = // fetch content in byte array; Doc documentToBePrinted = new SimpleDoc(new ByteArrayInputStream(byteStream), docType, null); printJob.print(documentToBePrinted, null); } } 

试试这段代码:

 FileInputStream fis = new FileInputStream(“C:/mypdf.pdf”); Doc pdfDoc = new SimpleDoc(fis, null, null); DocPrintJob printJob = printService.createPrintJob(); printJob.print(pdfDoc, new HashPrintRequestAttributeSet()); fis.close(); 

您也可以按照以下步骤操作

不推荐使用PDDocument的可分页实现,而是使用PDPageable适配器类并尝试使用setPrintable而不是setPageable:

 job.setPrintable(new PDPageable(document));