Sunday, 6 December 2020

How to print a PDF document in Java

This article will show you how to use a free API called Free Spire.PDF for Java to print a PDF document in Java programs. According to different requirements, there are three aspects to be shown.

l  Silently print a PDF document with default printer

l  Print a PDF document with Print dialog

l  Print a PDF document with customized page size

About Free Spire.PDF for Java

It is a free PDF API that enables Java applications to create, edit, read, convert and print PDF documents without using Adobe Acrobat. Using this Java PDF library, developers and programmers can implement rich capabilities to create PDF files from scratch or process existing PDF documents entirely on Java applications (J2SE and J2EE).

Maven Dependency

Before typing code snippets, you need to add Spire.Pdf.jar to your project as a dependency. If you’re creating a Maven program, please add the following configuration into the pom.xml file, and click the button “Import Changes”.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

Or you can download the package from the link, unzip it and find Spire.Pdf.jar in the “lib” folder.

Finally manually add it to your project.


Using the code

Silently print a PDF document with default printer

In the process of printing 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 SilentlyPrintPDF {
public static void main(String[] args) {
//load the sample document
PdfDocument pdf = new PdfDocument();
pdf.loadFromFile("C:\\Users\\Test1\\Desktop\\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 a PDF document with Print dialog

import com.spire.pdf.*;
import java.awt.print.*;
public class PrintDialog {
public static void main(String[] args) {
//load the sample document
PdfDocument pdf = new PdfDocument();
pdf.loadFromFile("C:\\Users\\Test1\\Desktop\\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 a PDF document with customized page size

import com.spire.pdf.*;
import java.awt.print.*;
public class CustomizedPageSize {
public static void main(String[] args) {
//load the sample document
PdfDocument pdf = new PdfDocument();
pdf.loadFromFile("C:\\Users\\Test1\\Desktop\\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();
}
}
}

 

No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...