How to Print PDF Files in Java

by Mehr Muhammad Hamza

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