DEV Community

Mehr Muhammad Hamza
Mehr Muhammad Hamza

Posted on

Convert PDF to Image in Java (Code Example Tutorial)

In this tutorial, we will learn to convert pdf into images. We will convert pdf into images using Java Language. We will also convert URL and HTML files into images.

There are many uses for converting PDF pages and documents to image files like JPEG, PNG, and TIFF in the software business. Sometimes you only have the choice of taking a screenshot of the page if you need an image of a particular page in a PDF file to use elsewhere. Assume you're working on a Java project that includes the ability to load and convert PDF pages to image files that can then be used for other reasons. The conversion is nearly impossible using conventional Java code. Therefore, we need a third-party library that can convert pdf to images.

Now, There are numerous libraries available that can be used for this purpose. But each comes with some limitations such as some are paid, difficult to use, or do not provide accuracy and performance. I have found IronPDF for Java a perfect choice when dealing with pdf files. Before going further, let's explore a little bit about it.

IronPDF for Java:

IronPDF is a java library developed and maintained by Iron Software which prioritizes accuracy, speed, and ease of use. It is free for development, easy to use, and provides a wide range of features which includes creating, reading, and manipulating pdf files. It includes almost all the features which you can think of while dealing with a pdf files.

Create a New Java Project:

I am using IntelliJ IDE. steps for creating a new project may differ from IDE to IDE.
Open IntelliJ IDE.
Image description
Click on New Project, and a New Window will appear as shown below.
Image description
Name your Project, Select Location, Language, Build System, and JDK. Click on Create Button, and a new project will be created.
Now, we need to install the IronPDF library in our project.

Install IronPDF:

We need to add a maven dependency in our project to install this library. Follow this step to add the dependency:
Open Pom.XML File.
Add the following xml tags under the properties tag.

<dependency>
        <groupId>com.ironsoftware</groupId>
        <artifactId>com.ironsoftware</artifactId>
        <version>2023.3.2</version>
    </dependency>
    <dependency>
        <groupId>com.ironsoftware</groupId>
        <artifactId>org.slf4j</artifactId>
        <version>2.0.5</version>
    </dependency>
Enter fullscreen mode Exit fullscreen mode

Build the project to install the dependency. Moreover, API's jar file can also be downloaded from this link.

Convert the PDF document into an Image:

First of all, we need to import java and IronPDF Library to use in our program. Add the following import statement at the top of the program.

import com.ironsoftware.ironpdf.*;
import com.ironsoftware.ironpdf.edit.PageSelection;
import com.ironsoftware.ironpdf.image.ToImageOptions;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;
Enter fullscreen mode Exit fullscreen mode

Now, Let's write a code to convert a pdf file into an image file.

I am using the following example pdf document:
Image description
For simplicity, I am using just 1 pdf page text-only pdf document, in another example, I will use a pdf document with multiple pdf pages with images, tables, and text.

public static void main(String[] args) throws IOException {     
PdfDocument instance = PdfDocument.fromFile(Paths.get("lorem-ipsum.pdf"));
        List<BufferedImage> extractedImages = instance.toBufferedImages();
        int pageIndex = 1;
        for (BufferedImage extractedImage : extractedImages) {
            String fileName = "image" + pageIndex++ + ".jpeg";
            ImageIO.write(extractedImage, "PNG", new File(fileName));
        }
}
Enter fullscreen mode Exit fullscreen mode

Build and Run the program to see the output image.

Output image:

Image description
As observed, our pdf document is converted into JPEG image format.

Now, Let's discuss the code in detail.

Code Explanation:

We have done PDF-to-image conversion in JPEG format with just a few lines of code using IronPDF for Java.

First of all, we have initialized the pdf document instance by loading a pdf document from our drive. In the second line of code, we have used the toBufferedImages() method which returns a list containing a collection of BufferedImage objects, and it is arranged in ascending order corresponding with page numbers. In this way, we have converted our pdf document into the output stream of image files.

In for loop, we have looped through each list of image to save it in our device as JPEG image files. You can also save it as PNG images, jpg, tiff, or any of the image formats.

Now, let's convert pdf pages into images with specific output image resolutions.

Convert pdf documents into Image Files with Specified resolutions:

I am using a pdf document with 30 pages and 1 MB size. This document consists of graphs, images, and tables. This sample document for test purposes can be obtained from the following link.

Consider the following code:

public static void main(String[] args) throws IOException {
        PdfDocument instance = PdfDocument.fromFile(Paths.get("file-example_PDF_1MB.pdf"));
        List<BufferedImage> extractedImages = instance.toBufferedImages();
        ToImageOptions rasterOptions = new ToImageOptions();
        rasterOptions.setImageMaxHeight(800);
        rasterOptions.setImageMaxWidth(500);
        List<BufferedImage> sizedExtractedImages = instance.toBufferedImages(rasterOptions, PageSelection.allPages());
        int pageIndex = 1;
        for (BufferedImage extractedImage : sizedExtractedImages) {
            String fileName = "myConvertedImages/image" + pageIndex++ + ".png";
            ImageIO.write(extractedImage, "PNG", new File(fileName));
        }
Enter fullscreen mode Exit fullscreen mode

It can be observed that the code is exactly the same as demonstrated in the above example. Just the difference is of extra lines which we have added to the specified resolution images. In this example, we have saved images in PNG Format.

Output:

Following is the output generated by our program. This program takes only a couple of seconds to execute 1 MB of file and convert them into image files.
Image description
We can also convert URLs and HTML files to images using this library. Let's consider the following example.

Convert URL into Images:

We need to change 1 line of code, the rest of the code will remain the same as demonstrated in the above examples.

PdfDocument instance = PdfDocument.renderUrlAsPdf("https://www.oracle.com/pk/java/technologies/downloads/#jdk17-windows");
Enter fullscreen mode Exit fullscreen mode

In the above sample code, we have initialized the PDF instance by Rendering our URL as pdf instead of loading pdf from our device.

The output of the above code is as:

Output:

Image description
Similarly, we can create images from HTML files by rendering HTML as pdf documents as shown below.

PdfDocument instance = PdfDocument.renderHtmlAsPdf("index.html");
Enter fullscreen mode Exit fullscreen mode

We have just initialized the pdf documents instance by using the renderHtmlAsPdf() method.

Summary:

In the above examples, we have converted pdf to images. Moreover, we have also created images from URL and HTML files by using the renderUrlAsPdf() and renderHtmlAsPdf() methods respectively. We have performed our task with minimum code and effort while achieving maximum performance, efficiency, and accuracy. IronPDF is not just an image converter library rather It has 50+ features such as creating, reading, and manipulating pdf files. It also provides us functions to add a bookmark, insert a header, footer, page #, merge pdf documents, add a digital signature, and password, and create and fill pdf forms. There are other useful features, but I cannot list all of them here. Please visit their official documentation for more details.

It is completely free for development, However, we need to purchase its license for commercial use. It also provides a 30-day free trial. We can use the trial period to explore it and give it a try in the production environment.

I have kept this article simple and clear. I hope you have understood it. Please feel free to ask any questions in the comment box.

Top comments (1)

Collapse
 
vikolaz profile image
Rala Volo • Edited

Ready to take your reading experience to the next level? I just discovered an enlightening article that discusses the benefits of converting PDF files into goodereader.com/blog/e-book-news/h... e-books. Whether you prefer reading on your tablet, e-reader, or smartphone, converting PDFs to e-books allows for a more immersive and enjoyable reading experience. Dive into the world of digital literature with this insightful piece!