Emily Emily is part of the Marketing and Sales team of IDR Solutions. She enjoys creating content for social media and diving into digital marketing.

Do you need to process or display PDF files?

Find out why you should be using IDRSolutions software

Java PDF to PNG conversion

1 min read

Convert PDF file to PNG in Java

Convert PDF to PNG in Java

Why convert PDF files to PNG images?

A PDF is a vector image (so it is rendered when you display it at whatever size you specify). A PNG is a bit-mapped file of a set size with fixed values. So to convert a PDF to a PNG file we need to create a blank image and then draw the PDF onto this. Then will have the PNG file (or a TIF or JPEG – the process is the same).

This process is usually done with a PDF tool such as Acrobat and our Java PDF library also includes this functionality. There are lots of Open Source and commercial tools in most major languages. If you are using Java, it is not a function that is built into Java – you will need an external application to do this.

How to convert PDF to PNG in Java with JPedal

  1. Download a trial copy of JPedal and add it to your IDE.
  2. Create a File handle, InputStream or URL pointing to the PDF file
    ConvertPagesToImages extract=new ConvertPagesToImages(path);
  3. Include a password if file password protected
    extract.setPassword("password");
  4. Open the PDF file
    if (extract.openPDFFile()) {
  5. Iterate over the pages
    int pageCount = extract.getPageCount();
      for (int page = 1; page <= pageCount; page++) {
          BufferedImage img = extract.getPageAsImage(page, hasAlpha);
      }
    }
  6. Close the PDF file
     extract.closePDFfile();

Key points to remember

The usual reason for doing this conversion is to display the content as an image (e.g. a thumbnail on a website). However, there are things so remember:

1. Bitmapped images do not scale very well (unlike Vector formats like PDF). So you need to get the size correct. If you make it too small you will not be able to zoom in without pixelation. If you make it too big you will make the download slower and need more memory.

2. Bitmapped images do not have some of the other advantages of PDF files (like text search).  You may need to add manual functionality to your application if you wish to have things like text highlighting.

3. If you are doing it to print the PDF you will need a very large image to get a quality print at 300 or 600 dpi (what looks good on screen at 72dpi will not appear as crisp on a printout).

So long as you remember these, Java PDF to PNG conversion is a straightforward process with a tool such as JPedal.

 



Our software libraries allow you to

Convert PDF files to HTML
Use PDF Forms in a web browser
Convert PDF Documents to an image
Work with PDF Documents in Java
Read and write HEIC and other Image formats in Java
Emily Emily is part of the Marketing and Sales team of IDR Solutions. She enjoys creating content for social media and diving into digital marketing.

What is PNG?

Mark Stephens
36 sec read

Comparing PNG Compression Algorithms

In this article we will be comparing the different compression methods available to use within PNG images. To do this, we will be using...
Jacob Collins
49 sec read