How to print PDF files in Java
Tags: print, java, pdfprint
Published: 2019-02-27 08:18:50

---
title: How to print PDF files in Java
published: true
description: print PDF documents in Java
tags: print, java, pdf print
---

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:
<ul>
<li>Silent print PDF document with default printer</li>
<li>Print PDF document with Print dialog</li>
<li>Print PDF document with customized page size</li>
</ul>

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.