Convert PDF to image in java

Problem

You have a PDF file and you wish to convert each of its pages to an image. Going one by one, taking screenshot and using an image editor will definitely take time. In this post we will see how to automate this task.

solution

PDF – Portable Document Format is one of the most popular file formats out there. That means all the problems related to this file format are possibly addressed already. In this case we will use a library specifically designed for manipulating PDF files. It comes from Apache Software Foundation, the PDFBox.

In this case, Maven is used to add the dependency to the program as explained in their getting started. The .jar file can be also downloaded and loaded in your IDE as external library.

Algorithm
  1. Open a given PDF document
  2. Iterate through the page(s)
  3. For each one, create an image
  4. Close document
package com.programmerabroad;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

class PdfToJpeg {

    public static void main(String[] args) throws IOException {

        String pdfPath = "ebrochure.pdf";
        PDDocument document = PDDocument.load (new File(pdfPath));
        PDFRenderer renderer = new PDFRenderer(document);
        int pages = document.getNumberOfPages();

        System.out.println("Converting...");

        for(int i=1; i<=pages; i++) {
            System.out.println("\tPage #" + i  + " of " + pages);
            BufferedImage bufferedImage = renderer.renderImageWithDPI(i-1, 300);
            ImageIO.write(bufferedImage, "JPEG", new File("pdf-page-" + i + ".jpeg"));
        }

        document.close();

        System.out.println("Done");
    }
}

As you can see it is very simple to do this task thanks to the great PDFBox library. Of course, the logic would have been better if extracted into a method instead of placing it all in the main method.

Conclusion

In this post we saw how simple it is to convert a PDF to JPEG file(s) using the PDFBox. It is a powerful tool capable of doing other things like extracting text from a PDF, splitting, merging and other. You can check out their documentation and FAQ.

See more coding tutorials.


0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Strictly Necessary Cookies

Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.

Google Analytics Cookies

This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.

Keeping this cookie enabled helps us to improve our website.

HotJar Cookies

We use Hotjar in order to better understand our users’ needs and to optimize this service and experience. Hotjar is a technology service that helps us better understand our users’ experience (e.g. how much time they spend on which pages, which links they choose to click, what users do and don’t like, etc.) and this enables us to build and maintain our service with user feedback. Hotjar uses cookies and other technologies to collect data on our users’ behavior and their devices. This includes a device's IP address (processed during your session and stored in a de-identified form), device screen size, device type (unique device identifiers), browser information, geographic location (country only), and the preferred language used to display our website. Hotjar stores this information on our behalf in a pseudonymized user profile. Hotjar is contractually forbidden to sell any of the data collected on our behalf.

For further details, please see the ‘about Hotjar’ section of Hotjar’s support site.