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

Salesforce Integration Solutions: Cloud & On-Premise

Ch12: Something on Bits, Target, Difficulty

My journey to cloud computing part 2

THE NEW BUILD IS COMING

Substantial Cost Savings with Capptions!

GCP Crash Course: Storage and Databases

CS373 Fall 2021: Blog #10

Why Go for a Headless CMS?

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

Java Virtual Machine(JVM) Architecture

Java Inner Example -10 Program

JAVA & Git-Hub

Create simple Spring-Boot application