How to print PDF document in Java
Tags: print, java, pdf
Published: 2020-10-15 07:43:36

---
title: How to print PDF document in Java
published: true
description: java print pdf document
tags: print, java, pdf, 
//cover_image: https://direct_url_to_image.jpg
---
Spire.PDF has a powerful function to print PDF document. This article shows how to print a PDF file in Java applications from the following print methods:
<ul>
<li>Print PDF to default printer</li>
<li>Print PDF document with Print dialog</li>
<li>Print PDF document with customized page size</li>
<li>Select some pages in the PDF file to print</li></ul>
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.

```java
import com.spire.pdf.*;

import java.awt.print.*;

public class PrintPdfDocument {

    public static void main(String[] args) {
        //load the sample document
        String inputFile = "Sample.pdf";
        PdfDocument loDoc = new PdfDocument(inputFile);
        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(1);
        loPageFormat.setPaper(loPaper);
        loPrinterJob.setPrintable(loDoc,loPageFormat);

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

Print PDF document with Print dialog

```java
import com.spire.pdf.*;

import java.awt.print.*;

public class PrintPdfWithDialog {

    public static void main(String[] args) {
        //load the sample document
        String inputFile = "Sample.pdf";
        PdfDocument loDoc = new PdfDocument(inputFile);
        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(loDoc,loPageFormat);

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

Print PDF document with customized page size. We can use loPaper.setSize(500,600) to set the page size when printing the PDF.

```java
import com.spire.pdf.*;

import java.awt.print.*;

public class printWithCustomizedPageSize{

    public static void main(String[] args) {
        //load the sample document
        String inputFile = "Sample.pdf";
        PdfDocument loDoc = new PdfDocument(inputFile);
        PrinterJob loPrinterJob = PrinterJob.getPrinterJob();
        PageFormat loPageFormat  = loPrinterJob.defaultPage();
        Paper loPaper = loPageFormat.getPaper();

        //Set page size
        loPaper.setSize(500,600);
        loPageFormat.setPaper(loPaper);
        loPrinterJob.setPrintable(loDoc,loPageFormat);
        //Print
        try {
            loPrinterJob.print();
        } catch (PrinterException e) {
            e.printStackTrace();
        }
    }
}
```

Select the pages we want to print by setting the print range. We could also set to print one side or both sides with PrintRequestAttributeSet object.

```java
import com.spire.pdf.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import java.awt.print.*;


public class setPrintRange {

    public static void main(String[] args) {
        //load the sample document
        String inputFile = "Sample.pdf";
        PdfDocument loDoc = new PdfDocument(inputFile);
        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(loDoc, loPageFormat);
        //Set print range
        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
        aset.add(new PageRanges(6,7));

        //duplex Print
        aset.add(Sides.TWO_SIDED_SHORT_EDGE);

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