Print PDF files Using Java

  • 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

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();
}
}
}
}
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();
}
}
}

--

--

--

Sharing Java Code

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Starting at Student Beans as an iOS Developer

Multiples Nodes Connections to Elastic Search in Asp.Net Core 3.0

Setting Up Your Own Remote VPS Using Raspberry Pi : Part One

What types of S3 storage are available?

Creating a Python-based Automated Mailing Alert System

Mortgage Calculator

How data compression works: exploring LZ77

Accessing Pods Outside Of The Cluster In Kubernetes

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Andrew Wilson

Andrew Wilson

Sharing Java Code

More from Medium

AWS EC2 with Spring Boot Java — Part 1

Database connectivity with java 🤯, using mysql…

multi threading in java

JIB — A Powerful Docker Image Builder