Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the methods for Java to print PDF documents

Shulou Source: shulou.com Published: 2022-06-02 18:30:49 09月14日 Update

This article mainly explains "what are the methods of Java printing PDF documents", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "what are the methods of Java printing PDF documents"!

Use tools: Spire.PDF for Java

Jar file acquisition and import:

Method 1: download the jar package. After downloading, extract the file and import the Spire.Pdf.jar under the lib folder into the java program.

Method 2: can be imported through the maven library. Refer to the import method.

Java code example

[example 1] Silent printing

That is, print the PDF document directly by using the default printer. When printing, we can set the number of printed copies, set the margin of paper printing, and so on.

Import com.spire.pdf.*;import java.awt.print.*; public class Print {public static void main (String [] args) {/ / load document PdfDocument pdf = new PdfDocument (); pdf.loadFromFile ("Sample.pdf"); PrinterJob loPrinterJob = PrinterJob.getPrinterJob (); PageFormat loPageFormat = loPrinterJob.defaultPage (); Paper loPaper = loPageFormat.getPaper () / / Delete the default margin loPaper.setImageableArea (0Jing 0jinloPageFormat.getWidth (), loPageFormat.getHeight ()); / / set the number of printed copies loPrinterJob.setCopies (2); loPageFormat.setPaper (loPaper); loPrinterJob.setPrintable (pdf,loPageFormat); try {loPrinterJob.print ();} catch (PrinterException e) {e.printStackTrace () }}}

[example 2] display a print dialog box to print an PDF document

Import com.spire.pdf.*;import java.awt.print.*; public class Print {public static void main (String [] args) {/ / load document PdfDocument pdf = new PdfDocument (); pdf.loadFromFile ("Sample.pdf"); PrinterJob loPrinterJob = PrinterJob.getPrinterJob (); PageFormat loPageFormat = loPrinterJob.defaultPage (); Paper loPaper = loPageFormat.getPaper () / / Delete the default margin loPaper.setImageableArea (0Magne0Magneto PageFormat.getWidth (), loPageFormat.getHeight ()); loPageFormat.setPaper (loPaper); loPrinterJob.setPrintable (pdf,loPageFormat); / / Show print dialog box if (loPrinterJob.printDialog ()) {try {loPrinterJob.print () } catch (PrinterException e) {e.printStackTrace ();}

[example 3] Custom paper size when printing

Import com.spire.pdf.*;import java.awt.print.*;public class Print {public static void main (String [] args) {/ / load document PdfDocument pdf = new PdfDocument (); pdf.loadFromFile ("Sample.pdf"); PrinterJob loPrinterJob = PrinterJob.getPrinterJob (); PageFormat loPageFormat = loPrinterJob.defaultPage (); / / set print paper size Paper loPaper = loPageFormat.getPaper () LoPaper.setSize (500600); loPageFormat.setPaper (loPaper); loPrinterJob.setPrintable (pdf,loPageFormat); try {loPrinterJob.print ();} catch (PrinterException e) {e.printStackTrace ();} so far, I believe you have a deeper understanding of "what are the methods of Java printing PDF documents?" Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Documents methods examples files paper copies content size dialog boxes learning dialogue practical deeper code interest practicality practical tools printers easy to operate Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker vpn NVidia macOS Xiaomi