Mark Stephens Mark has been working with Java and PDF since 1999 and is a big NetBeans fan. He enjoys speaking at conferences. He has an MA in Medieval History and a passion for reading.

How to convert a PDF to Image in Java

1 min read

Do you need to convert PDF files to images in Java? PDF files are not directly supported in Java but you can solve this problem very easily with a third-party Java PDF library.

This tutorial shows in simple steps Java PDF to Image conversion using our popular JPedal Java PDF library. You can convert a PDF file to a BufferedImage in Java or save it directly to BMP, GIF, HEIC, JPEG, JPEG2000, PNG, TIFF, or WebP.

How to convert PDF to image in Java (Method 1 – fast and standard quality image)

  1. Download JPedal trial jar.
  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();
    

How to convert PDF to image in Java (Method 2 – slower and very high quality image)

  1. Download JPedal trial jar.
  2. Create a File handle, InputStream or URL pointing to the PDF file
    ConvertPagesToHiResImages extract=
    new ConvertPagesToHiResImages(path);
  3. Include a password if file password protected
    extract.setPassword("password");
  4. Set conversion options for output defined in JPedalSettings
    HashMap options=new HashMap();
  5. Open the PDF file
    if (extract.openPDFFile()) {
  6. Iterate over the pages
    int pageCount = extract.getPageCount();
      for (int page = 1; page <= pageCount; page++) {
        BufferedImage img = extract.getPageAsHiResImage(page, 
        hasAlpha, options);
      }
    }
  7. Close the PDF file
     extract.closePDFfile();
    
    

Related tutorials

If you want to convert a PDF file directly to any of these image formats, check out our related tutorials. Here we document how to



Do you need to...

Display PDF files in Java Apps →

Convert PDF Files to image →

Mark Stephens Mark has been working with Java and PDF since 1999 and is a big NetBeans fan. He enjoys speaking at conferences. He has an MA in Medieval History and a passion for reading.