DEV Community

E-iceblue Product Family
E-iceblue Product Family

Posted on

How to print PDF files in Java

Printing PDF files in Java is a common requirement and we always need to print the PDF files. In this post, I’ll give you an example of how to print a PDF in Java applications by using Spire.PDF for Java library in the following three aspects:

  • Silent print PDF document with default printer
  • Print PDF document with Print dialog
  • Print PDF document with customized page size

Before Start

Install Spire.PDF for Java from Maven repository. The download contains two jars which should be referenced in your Java project.

Print the PDF document to default printer without showing print dialog, we could also customize some print settings, such as removing the default print margins, setting the number of copies, etc.

import com.spire.pdf.*;
import java.awt.print.*;

public class Print {
    public static void main(String[] args) {
        //load the sample document
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromFile("Sample.pdf");

        PrinterJob loPrinterJob = PrinterJob.getPrinterJob();
        PageFormat loPageFormat  = loPrinterJob.defaultPage();
        Paper loPaper = loPageFormat.getPaper();

        //remove the default printing margins
        loPaper.setImageableArea(0,0,loPageFormat.getWidth(),loPageFormat.getHeight());

        //set the number of copies
        loPrinterJob.setCopies(2);

        loPageFormat.setPaper(loPaper);
        loPrinterJob.setPrintable(pdf,loPageFormat);
        try {
            loPrinterJob.print();
        } catch (PrinterException e) {
            e.printStackTrace();
        }
    }
}

Print PDF with print dialog.

import com.spire.pdf.*;
import java.awt.print.*;

public class Print {
    public static void main(String[] args) {
        //load the sample document
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromFile("Sample.pdf");

        PrinterJob loPrinterJob = PrinterJob.getPrinterJob();
        PageFormat loPageFormat  = loPrinterJob.defaultPage();
        Paper loPaper = loPageFormat.getPaper();

        //remove the default printing margins
        loPaper.setImageableArea(0,0,loPageFormat.getWidth(),loPageFormat.getHeight());

        loPageFormat.setPaper(loPaper);
        loPrinterJob.setPrintable(pdf,loPageFormat);

        //display the print dialog
        if (loPrinterJob.printDialog()) {
            try {
                loPrinterJob.print();
            } catch (PrinterException e) {
                e.printStackTrace();
            }
        }
    }
}

Print PDF document with customized page size.

import com.spire.pdf.*;
import java.awt.print.*;

public class Print {

    public static void main(String[] args) {

        //load the sample document
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromFile("Sample.pdf");

        PrinterJob loPrinterJob = PrinterJob.getPrinterJob();
        PageFormat loPageFormat  = loPrinterJob.defaultPage();

       //set the print page size
        Paper loPaper = loPageFormat.getPaper();
        loPaper.setSize(500,600);
        loPageFormat.setPaper(loPaper);
                loPrinterJob.setPrintable(pdf,loPageFormat);

        try {
            loPrinterJob.print();
        } catch (PrinterException e) {
            e.printStackTrace();
        } 
    }
}

Conclusion

It’s very simple to print the PDF files in Java applications. This is only one simple case where I use in my process of printing PDF. It also supports the other operations on the PDF files, such as converting PDF to image, adding the digital signatures to PDF files. You could explore more by yourself.

Top comments (0)