How to print PDF document in Java

This following code snippets demonstrate how to use Spire.PDF for Java to print a PDF file in Java programs 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

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();
        } 
    }
}