You can use Apache PDFBox . Examples:
a) Print PDF as Pageable
PrintService printService = PrintServiceLookup.lookupDefaultPrintService(); DocPrintJob printJob = printService.createPrintJob(); PDDocument pdDocument = PDDocument.load(new File("doc.pdf")); PDFPageable pdfPageable = new PDFPageable(pdDocument); SimpleDoc doc = new SimpleDoc(pdfPageable, DocFlavor.SERVICE_FORMATTED.PAGEABLE, null); printJob.print(doc, null);
b) Print PDF as Printable
This option has the advantage that you can control page sizes, margins, etc. by changing the pageFormat variable.
PrintService printService = PrintServiceLookup.lookupDefaultPrintService(); DocPrintJob printJob = printService.createPrintJob(); PageFormat pageFormat = PrinterJob.getPrinterJob().defaultPage(); PDDocument pdDocument = PDDocument.load(new File("doc.pdf")); PDFPrintable pdfPrintable = new PDFPrintable(pdDocument); Book book = new Book(); book.append(pdfPrintable, pageFormat); SimpleDoc doc = new SimpleDoc(book, DocFlavor.SERVICE_FORMATTED.PAGEABLE, null); printJob.print(doc, null);
source share