Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
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.
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.
To utilize a Java viewer with IronPDF, certain prerequisites must be met. Ensure that you have the following:
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.
Hover on File > New > Project.
IntelliJ
Create 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>com.ironsoftware</artifactId>
<version>2024.11.4</version>
</dependency>
Now the environment is set. Let's write the logic to create and open PDF in 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;
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.
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);
}
}
}
The above code will create a PDF file using PdfDocument.renderUrlAsPdf method in IronPDF API
that will take a URL as an input stream and return 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.
PDF Output
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.
9 .NET API products for your office documents