DEV Community

loading...

How to print PDF document in Java

eiceblue profile image E-iceblue Product Family ・2 min read

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:

  • Print PDF to default printer
  • Print PDF document with Print dialog
  • Print PDF document with customized page size
  • Select some pages in the PDF file to print

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 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();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Print PDF document with Print dialog

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();
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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

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();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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.

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();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

pic
Editor guide