Skip to footer content
USING IRONPDF FOR JAVA

How to View PDF Files in Java

PDF viewers often offer features like zooming, page navigation, searching, and annotation support, empowering users to efficiently navigate and analyze documents. These viewers utilize libraries such as IronPDF, providing a versatile and customizable solution for incorporating PDF viewing capabilities into Java projects.

1. IronPDF for Java

IronPDF for Java is a powerful and feature-rich library that enables developers to effortlessly create, edit, and manipulate PDF documents within their applications. With IronPDF, developers can generate PDF files from HTML, URLs, and other sources, apply formatting and styling, add pictures, tables, and text, as well as perform advanced operations such as merging multiple PDFs in Java, splitting PDF documents in Java, and encrypting PDFs for security.

2. Prerequisites

To utilize a Java viewer with IronPDF, certain prerequisites must be met. Ensure that you have the following:

  1. Java Installation: Verify that Java is installed on your system and that its path is set in the environment variables. If you haven't installed Java yet, you can find installation instructions from this Java website.
  2. Java IDE: Install a Java Integrated Development Environment (IDE) such as Eclipse IDE download page or IntelliJ IDEA download section.
  3. IronPDF Library: Download the IronPDF library and add it as a dependency in your project using the command line. Instructions for setting up the IronPDF library can be found on the IronPDF official website.
  4. Maven Installation: Install Maven from Apache before starting the PDF conversion process.

3. Create a New Java Maven Project

For this tutorial, it is recommended to use IntelliJ IDE. However, please note that you can use any IDE of your choice. Keep in mind that the process of creating a new Java project may vary slightly depending on the IDE you are using.

  1. Launch the IntelliJ IDE, or go to an already opened instance of IntelliJ.
  2. Hover on File > New > Project.

    How to View PDF Files in Java, Figure 1: IntelliJ IntelliJ

  3. Enter Project Title name.
  4. Choose a location, a build system, and a JDK.
  5. Click on Finish as shown in the below image.

How to View PDF Files in Java, Figure 2: Create Project Create Project

3.1. Setting up the Project

Once the project is created, add the dependency for IronPDF in the pom.xml.

Below is the dependency code, place it in your pom.xml.

<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf-java</artifactId>
    <version>2023.9.1</version> <!-- Replace with the appropriate version -->
</dependency>
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf-java</artifactId>
    <version>2023.9.1</version> <!-- Replace with the appropriate version -->
</dependency>
XML

Now the environment is set. Let's write the logic to create and open PDF in PDF viewer.

4. Creating and Viewing PDF Document in Default PDF Viewer

Now create a new Java file in src > main > java.

First, import the required references that are important for creating and viewing PDF files in this Java application.

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

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
JAVA

These imports are important because they will help the Java program to execute the desired logic.

Now let's write the example code that will create the PDF document and implement the document viewer logic to open the created PDF file in the PDF Viewer.

public class Main {
    public static void main(String[] args) throws IOException {
        // Use IronPDF to create a PDF document from a URL
        PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com/");

        // Save the PDF document to the local file system
        pdf.saveAs(Paths.get("html_saved.pdf"));

        // Create a new file object for the saved PDF
        File file = new File("html_saved.pdf");

        // Check if the desktop is supported and the file exists, then open the PDF
        if (Desktop.isDesktopSupported() && file.exists()) {
            Desktop desktop = Desktop.getDesktop();
            desktop.open(file);
        }
    }
}
public class Main {
    public static void main(String[] args) throws IOException {
        // Use IronPDF to create a PDF document from a URL
        PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com/");

        // Save the PDF document to the local file system
        pdf.saveAs(Paths.get("html_saved.pdf"));

        // Create a new file object for the saved PDF
        File file = new File("html_saved.pdf");

        // Check if the desktop is supported and the file exists, then open the PDF
        if (Desktop.isDesktopSupported() && file.exists()) {
            Desktop desktop = Desktop.getDesktop();
            desktop.open(file);
        }
    }
}
JAVA

The above code will create a PDF file using PdfDocument.renderUrlAsPdf, which takes a URL as an input stream and returns a PDF file as output. After that, instantiate a new File object using the same PDF document. Then, the desktop.open(file) will be used to open the PDF file in the default PDF Viewer.

PDFs created using IronPDF can be printed. They also load all the pages perfectly with all the images intact and URLs are also clickable. An example output of the PDF file screenshot is attached in the default PDF Viewer.

How to View PDF Files in Java, Figure 3: PDF Output PDF Output

5. Conclusion

A Java PDF Viewer is a valuable tool that allows users to view, print, and interact with PDF files within a Java environment. It provides features such as rendering, navigation, searching, and annotation support, making it easier for users to access and analyze PDF content. IronPDF for Java is a robust library that empowers developers to effortlessly create, edit, and manipulate PDF documents.

With its extensive set of APIs and functionalities, IronPDF enables seamless integration of PDF generation and manipulation capabilities into Java applications. By following the steps to create a new Java project and setting up the environment, developers can leverage IronPDF to create and view PDF documents in the default PDF viewer. This offers advantages such as cross-platform compatibility, customization options, efficient performance, and enhanced security. With IronPDF for Java, developers can generate professional-grade PDF documents with ease and provide a seamless viewing experience for users.

Read our tutorial to learn how to use IronPDF to convert HTML to PDF files in Java.

Frequently Asked Questions

What features do Java PDF viewers typically offer?

Java PDF viewers often offer features like zooming, page navigation, searching, and annotation support, allowing users to efficiently navigate and analyze documents with the help of libraries such as IronPDF.

What is a powerful library for creating and manipulating PDF documents in Java?

IronPDF for Java is a powerful library that enables developers to create, edit, and manipulate PDF documents within their applications. It supports generating PDFs from HTML, URLs, and other sources, and offers features like merging, splitting, and encrypting PDFs.

What are the prerequisites for using a Java library to work with PDFs?

To use IronPDF in a Java project, you need to have Java installed, a Java IDE like IntelliJ or Eclipse, the IronPDF library as a dependency, and Maven installed for managing the project.

How do I create a new Java Maven project using IntelliJ?

To create a new Java Maven project in IntelliJ, launch the IDE, go to File > New > Project, enter the project title, choose a location, select a build system and JDK, then click Finish.

How do I add a PDF library dependency to a Maven project?

You can add the IronPDF dependency to a Maven project by including the following in the `pom.xml`: `com.ironsoftwareironpdf-java2023.9.1`.

How can I create and view a PDF document using a Java library?

To create and view a PDF document using IronPDF in Java, you can use `PdfDocument.renderUrlAsPdf` to generate a PDF from a URL, save it to the local filesystem, and open it using the `Desktop` class if supported.

What are the advantages of using a Java library for PDF manipulation?

IronPDF for Java offers advantages such as cross-platform compatibility, customization options, efficient performance, and enhanced security. It allows developers to generate professional-grade PDF documents easily.

Can a Java library be used to print PDFs?

Yes, PDFs created using IronPDF can be printed. They load all pages perfectly with all images intact and URLs clickable.

Is it possible to use IntelliJ with a Java PDF library?

Yes, you can use IntelliJ IDE to set up and run a Java project that uses IronPDF. The tutorial provides guidance on creating a project and integrating IronPDF within IntelliJ.

Where can I find more tutorials on using a Java PDF library?

You can find more tutorials on using IronPDF for Java on the IronPDF official website, including guides on converting HTML to PDF files.

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.

Install with Maven

Version: 2025.6.5

<dependency>
  <groupId>com.ironsoftware</groupId>
  <artifactId>ironpdf</artifactId>
  <version>2025.6.5</version>
</dependency>
Talk to an Expert Five Star Trust Score Rating

Ready to Get Started?