How To Print PDF Files Using Java

Introduction

PDF is important in Java applications because it allows developers to create and manipulate PDF documents in a platform-independent manner. The PDF format is widely used for storing and sharing documents, so being able to work with it is important for many Java applications that deal with document management or document-based workflows.

There are several ways to generate and print PDF files in Java. A common approach is to use a library that provides classes for creating and manipulating PDF documents. This How-to Guide will show how to use the IronPDF library to generate and print PDF files in Java applications.


IronPDF: Java PDF Library

IronPDF is a Java library that can be used to generate, manipulate, and convert PDF documents. It is based on the IronPDf C# .NET library, which provides a similar set of features for the .NET platform.

IronPDF provides a high-level API for working with PDF documents, allowing developers to work with PDF files without having to deal with the low-level details of the file type. It supports common PDF operations such as creating new documents, adding content, formatting text, and merging PDF files, and splitting PDF files.

IronPDF provides support for converting HTML, CSS, and JavaScript code to PDF, making it easy to generate PDF files from web pages or HTML templates. It also offers the option to print PDF documents.

Steps to Print a PDF Document Using IronPDF Java

Prerequisites

To print PDF files in Java, there are some prerequisites:

  1. Eclipse IDE or any other Java IDE
  2. A Maven Project running in Eclipse or in any other IDE
  3. A stable internet connection to install the IronPDF Java library

Install IronPDF Library in Maven Project

To install IronPDF in a Maven project, you need to add the IronPDF dependency to your project's pom.xml file.

Add the following dependencies to the <dependencies> section of the pom.xml file:

<dependency>
   <groupId>com.ironsoftware</groupId>
   <artifactId>com.ironsoftware</artifactId>
   <version>2024.3.1</version>
</dependency>
XML

After adding the dependencies to the pom.xml file, run the mvn install command in the terminal, or press Ctrl+S to download and install IronPDF in your Maven project.

Before we can start using IronPDF, must first import the IronPDF classes in the main App.java source file, as found in the src folder.

Java Print PDFs - Figure 1: Package explorer tree for ironpdf-java

Package Explorer Tree for IronPDF for Java

Open the "App.java" file and add the IronPDF package by using the following code.

import com.ironsoftware.ironpdf.*;
JAVA

Load a PDF in a Java Application

IronPDF for Java provides a constructor for loading PDF content into the library. Valid arguments that this constructor can accept include a byte array and a file path. For password-protected documents, a third parameter containing the password for the PDF file also be provided.

The code snippet below loads a PDF located on the filesystem.

License.setLicenseKey("Enter-Your-License");  
PdfDocument pdf = new PdfDocument(Paths.get("MyPdf.pdf"));
JAVA

Print a PDF Document With Default Settings

IronPDF provides two ways to print PDF files. The first way is to print the document immediately using default printer and page settings. You can use the printWithoutDialog method to perform this action.

pdf.printWithoutDialog();
JAVA

The Print Dialog

The second way is to allow the user to specify printing options prior to printing. You can achieve this functionality using the print method.

pdf.print();
JAVA

The print dialog window will appear when the this method is invoked, allowing the user to change the printer, set paper size, change the number of copies, etc.

Java Print PDFs - Figure 2: Print dialog shown after running the program using print()

Print dialog shown after running the program using the print() method

Full Source Code

The complete source file used in this How-To Guide is below.

package IronPDF.ironpdf_java;
//Import statement for IronPDF Java  
import com.ironsoftware.ironpdf.*;

import java.awt.print.PrinterException;
import java.io.IOException;  
import java.nio.file.Paths; 
public class App 
{
    public static void main( String [] args ) throws PrinterException,IOException
    {
     // Apply your license key
        License.setLicenseKey("Enter-Your-License");     
        PdfDocument pdf = new PdfDocument(Paths.get("MyPdf.pdf"));
        pdf.printWithoutDialog();
        pdf.print();
    }
}
JAVA

Learn more about PDF Printing in Java using the IronPDF library by clicking this link.

Summary

In conclusion, IronPDF is a powerful and easy-to-use library for printing PDFs in Java applications. With its rich set of features and extensive documentation, IronPDF makes it simple to generate and customize professional-quality PDFs that can be printed or shared with others. Whether you need to create invoices, reports, or any other type of document, IronPDF has you covered.

IronPDF offers a free trial for testing in production. Pricing of IronPDF starts from $749. Give IronPDF a try and see how it can help you streamline your PDF printing workflow.