Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
This article will demonstrate how to use IronPDF to preview PDF files within a Java application.
IronPDF is a high-performance Java library, offering fast and accurate operations, making it an excellent choice for PDF-related tasks such as reading PDF files, extracting text and images, merging, and splitting.
With the help of the IronPDF library, you can create PDFs from HTML, URLs, and strings with precise pixel-perfect rendering.
To create a document viewer for PDF documents in Java, you need the following things set in place.
Add the IronPDF library to your project using Maven by adding the dependencies shown below in your project's pom.xml
file:
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>com.ironsoftware</artifactId>
<version>2024.9.1</version>
</dependency>
Add the necessary imports:
import com.ironsoftware.ironpdf.PdfDocument;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
To view PDF documents, the next step is to load the PDF file in this Java PDF viewer application by using the PdfDocument
class.
public class PDFPreview extends JFrame {
private List<String> imagePaths = new ArrayList<>();
private List<String> ConvertToImages() throws IOException {
PdfDocument pdfDocument = PdfDocument.fromFile(Paths.get("example.pdf"));
// Create a list of paths for images
List<BufferedImage> extractedImages = pdfDocument.toBufferedImages();
int i = 1;
for (BufferedImage extractedImage : extractedImages) {
String fileName = "assets/images/" + i + ".png";
ImageIO.write(extractedImage, "PNG", new File(fileName));
imagePaths.add("assets/images/" + i + ".png");
i++;
}
return imagePaths;
}
}
The output PDF file
Converted to Images:
Convert PDF file to images
Now, you can display the converted images in a preview window using Java Swing components.
public class PDFPreview extends JFrame {
private JPanel imagePanel;
private JScrollPane scrollPane;
public PDFPreview() {
try {
imagePaths = this.ConvertToImages();
} catch (Exception e) {
}
// Create imagePanel
imagePanel = new JPanel();
imagePanel.setLayout(new BoxLayout(imagePanel, BoxLayout.Y_AXIS));
// Add images to the panel
for (String imagePath : imagePaths) {
ImageIcon imageIcon = new ImageIcon(imagePath);
JLabel imageLabel = new JLabel(imageIcon);
imageLabel.setBorder(new EmptyBorder(10, 10, 10, 10));
imagePanel.add(imageLabel);
}
// Create the scroll pane and add imagePanel to it
scrollPane = new JScrollPane(imagePanel);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
// Set up the frame
getContentPane().add(scrollPane);
setTitle("PDF Viewer");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
}
Finally, place the following code in the main method in PDFPreview
class:
public static void main(String[] args) {
SwingUtilities.invokeLater(
PDFPreview::new
);
}
PDFPreview
extends JFrame
, a top-level container for window creation.imagePanel
, scrollPane
, and imagePaths
.ConvertToImages()
takes in PDF file example.pdf
, and converts it to a series of images. The PdfDocument
loads the PDF file and converts each page to a BufferedImage
, then saves each as a PNG in the assets/images/ directory and adds the paths to imagePaths
.PDFPreview()
initializes the application. It calls ConvertToImages()
to populate imagePaths
.imagePanel
is initialized and sets its layout as vertical box layout.imagePaths
and creates ImageIcon
for each image, adds them to JLabel
, and adds labels to imagePanel
.JScrollPane
and sets imagePanel
as its viewport.scrollPane
to the frame's content pane, sets frame's title, sets default close operation, packs components, centers frame on the screen, and makes it visible.main()
is the entry point of the program. It invokes the PDFPreview
constructor using SwingUtilities.invokeLater()
to ensure the Swing components are created and modified on the Event Dispatch Thread, the dedicated thread for GUI operations.Now, execute the program and the PDF document file viewer will be displayed with the loaded PDF document.
The output PDF file
This article demonstrated how to use IronPDF for Java-based applications to preview PDF files within a Java application, and how to access and display a PDF file.
With IronPDF, you can easily integrate PDF preview functionality into your Java application. For detailed guidance and examples on utilizing IronPDF for Java, you can refer to this example. For the Java PDF reader tutorial visit this article to read PDF files.
IronPDF is free for development purposes. To learn more about the licensing details, you can visit the provided licensing page. A free trial for commercial use is also available.
9 .NET API products for your office documents