How to Print PDF Files in Java
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.
How to Print PDF Files in Java
- Install the Java library to print PDF files
- Load an existing PDF or render a new one
- Use the
print
method to print with a dialog - Use the
printWithoutDialog
method to print without a dialog - Check the printed PDF document
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();
You will be prompted with a print dialog to select the printer and options, as shown below.
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();