Print PDF files Using Java
Printing PDF files is a common task in our daily work. In this article, you will learn how to use a free Java library to print a PDF file programmatically from the following three aspects:
- Silent print PDF with default printer
- Print PDF with Print dialog
- Print PDF with customized page size
Import JAR Dependency
- You can download the free library (Free Spire.PDF for Java) and unzip it. Then add the Spire.Pdf.jar file to your project as dependency.
- Or you can directly add the jar dependency to maven project by adding the following configurations to the pom.xml.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf.free</artifactId>
<version>4.3.0</version>
</dependency>
</dependencies>
Silent Print PDF with Default Printer
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 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();
}
}
}