How to View PDF Files in Java

A PDF document viewer in Java PDF library is a software component or application that allows users to view, print and interact with PDF (Portable Document Format) pages within a Java environment. It provides functionalities to load, render, print and navigate through PDFs, enabling users to access the content of files, such as text, pictures, and graphics.

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

Advantages of PDF Viewers

A PDF file viewer in Java offers numerous advantages, including cross-platform compatibility, seamless integration into existing applications, and a rich feature set that includes zooming, page navigation, and text selection. These viewers are highly customizable, allowing developers to tailor the viewer's appearance and behavior to suit their application's needs. Additionally, Java PDF viewers deliver efficient performance for handling large and complex files, ensuring a smooth rendering experience. With built-in security features and compatibility with other Java libraries, PDF viewers in Java provide a comprehensive solution for users to view, interact with, and protect PDFs within their applications.

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, splitting, and encrypting PDFs. The library offers a comprehensive set of APIs and functionalities, making it easy to integrate PDF generation and manipulation capabilities into Java applications with minimal effort. IronPDF for Java provides a reliable and efficient solution for developers looking to incorporate PDF generation and manipulation capabilities into their Java projects, empowering them to create professional-grade PDF documents with ease.

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 link.
  2. Java IDE: Install a Java Integrated Development Environment (IDE) such as Eclipse or IntelliJ.
  3. IronPDF Library: Download the IronPDF library and add it as a dependency in your project using command line. Instructions for setting up the IronPDF library can be found on the IronPDF website.
  4. Maven Installation: Install Maven before starting the PDF conversion process.

3. Create a New Java Maven Project

For this tutorial, I will demonstrate the steps using 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 already opened instance of IntelliJ.
  2. Hover on File > New > Project.

    How to View PDF Files in Java: Figure 1 - 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

3.1. Setting up the Project

Once the project is created, we need to 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>com.ironsoftware</artifactId>
   <version>2024.3.1</version>
</dependency>
XML

Now our environment is set. We need to 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, we will import the required references that are important for creating and viewing PDF files in our 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;
JAVA

These imports are important because these will help the Java program to execute our desired logics.

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

class Main {
    public void main(String[] args) throws IOException {
        // used to input PDF stream
        PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com/");
        pdf.saveAs(Paths.get("html_saved.pdf"));
        File file = new File("html_saved.pdf");

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

The above code will create a PDF file using PdfDocument.renderUrlAsPdf method that will take a URL as input stream and return a PDF file as output. After that, we will instantiate a new File object using the same PDF document. Then, we will use the desktop.open(file) to open the PDF file in default PDF Viewer.

PDF created using IronPDF can be printed. It also loads all the pages perfectly with all the images intact and URLs are also clickable. Example output of PDF file screenshot is attached in the default PDF Viewer.

How to View PDF Files in Java: Figure 3 - 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.