How to print PDF files automatically

We have a number of systems that create PDF files that need to be printed. They are stored in a central repository of documents. The message then goes into the JMS queue, which requires printing of the document. A service written in Java selects them and then invokes its own command. This is called Adobe Reader with the / t flag. This leads to the fact that the document is printed without displaying a graphical interface.

However, since turning off the power no longer works. In the meantime, we have to manually print hundreds of documents. We initially tried to use Java printing, but the PDF files failed.

Which is better for this?

+4
source share
5 answers

This code only works if the printer supports PDF. Otherwise, you need to use your own printer or Java library. This article has a blog article http://pdf.jpedal.org/java-pdf-blog/bid/25566/Printing-PDF-files-from-Java

+3
source

Show us the code. I remember how to print PDF without problems using the Java print API. Below is the code, you may need some modification, but it should work as is,

InputStream in = new FileInputStream(file); DocFlavor flavor = DocFlavor.INPUT_STREAM.PDF; // find the printing service AttributeSet attributeSet = new HashAttributeSet(); attributeSet.add(new PrinterName("FX", null)); attributeSet.add(new Copies(1)); PrintService[] services = PrintServiceLookup.lookupPrintServices( DocFlavor.INPUT_STREAM.PDF, attributeSet); //create document Doc doc = new SimpleDoc(in, flavor, null); // create the print job PrintService service = services[0]; DocPrintJob job = service.createPrintJob(); // monitor print job events PrintJobWatcher watcher = new PrintJobWatcher(job); System.out.println("Printing..."); job.print(doc, null); // wait for the job to be done watcher.waitForDone(); System.out.println("Job Completed!!"); 

Note:

  • Flavor not required in 2 places, 1 place should be enough. You will find it.
  • PrintJobWatcher is a nested class to add a PrintJobListener .
+1
source

Try using ICEpdf . Here is an example page :

 Document pdf = new Document(); pdf.setFile(filePath); // create a new print helper with a specified paper size and print // quality PrintHelper printHelper = new PrintHelper(null, pdf.getPageTree(), 0f, MediaSizeName.NA_LEGAL, PrintQuality.DRAFT); // try and print pages 1 - 10, 1 copy, scale to fit paper. printHelper.setupPrintService(selectedService, 0, 0, 1, true); // print the document printHelper.print(); 
+1
source

Since its inception, Java 1.5, Sun has developed a PDF rendering library for PDF processing. Now it remains in Swing Labs. And not sure if this will be added to future java-APIs. http://java.net/projects/pdf-renderer/

Used to view or print PDF files. for printing pdf files you can name this libray. Here is the piece of code.

 File input = new File(docName); FileInputStream fis = new FileInputStream(input); FileChannel fc = fis.getChannel(); ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); PDFFile curFile=null; PDFPrintPage pages=null; curFile = new PDFFile(bb); // Create PDF Print Page pages = new PDFPrintPage(curFile); PrinterJob pjob = PrinterJob.getPrinterJob(); PrintService[] services = pjob.lookupPrintServices(); for(PrintService ps:services){ String pName = ps.getName(); if(pName.equalsIgnoreCase("PrinterName")){ pjob.setPrintService(ps); System.out.println(pName); break; } } pjob.setJobName(docName); Book book = new Book(); PageFormat pformat = PrinterJob.getPrinterJob().defaultPage(); book.append(pages, pformat, curFile.getNumPages()); pjob.setPageable(book); // print PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); // Print it pjob.print(aset); 
+1
source

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); 
0
source

Source: https://habr.com/ru/post/1300161/


All Articles