How to Print PDF Files in Java

Darrius Serrant
Darrius Serrant
October 21, 2024
Updated December 17, 2024
Share:

Printing PDFs programmatically from Java applications allows you to automate document handling and seamlessly integrate printing functionality. With IronPDF for Java, you can send PDFs directly to a physical printer, providing precise control over print settings such as copies, page ranges, and more. This guide demonstrates how to use IronPDF’s features to streamline printing tasks within your Java applications.

Print PDF

The first step is to load the PDF document you want to print. The print method opens the standard print dialog, allowing you to select the printer, page range, and other options before printing. Here's an example:

import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;

License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");

// Render HTML to PDF
PdfDocument pdf = PdfDocument.renderHtmlAsPdf("<h1>testing</h1>");

// Print with Dialog
pdf.print();
JAVA

You will be prompted with a print dialog to select the printer and options, as shown below.

Print Dialog


Print PDF without the Print Dialog

The printWithoutDialog method bypasses the print dialog and sends the document straight to the default printer. This is useful in automation scenarios where no user interaction is needed.

import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;

License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");

// Render HTML to PDF
PdfDocument pdf = PdfDocument.renderHtmlAsPdf("<h1>testing</h1>");

// Print without Dialog
pdf.printWithoutDialog();
JAVA
Darrius Serrant
Full Stack Software Engineer (WebOps)

Darrius Serrant holds a Bachelor’s degree in Computer Science from the University of Miami and works as a Full Stack WebOps Marketing Engineer at Iron Software. Drawn to coding from a young age, he saw computing as both mysterious and accessible, making it the perfect medium for creativity and problem-solving.

At Iron Software, Darrius enjoys creating new things and simplifying complex concepts to make them more understandable. As one of our resident developers, he has also volunteered to teach students, sharing his expertise with the next generation.

For Darrius, his work is fulfilling because it is valued and has a real impact.