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 file to Image in Java

1 min read

Rasterize PDF

Do you need to convert PDF files to images in Java? PDF files are not directly supported by Java but you can solve this problem very easily with a third-party library. This tutorial shows in simple steps Java PDF to Image conversion using JPedal PDF library.Rasterize PDF

How to convert PDF to image in Java

Step 1 Download JPedal trial jar.
Step 2 Create a File handle, InputStream or URL pointing to the PDF file

ConvertPagesToImages extract=new ConvertPagesToImages(path);

Step 3 Include a password if file password protected

extract.setPassword("password");

Step 4 Open the PDF file

if (extract.openPDFFile()) {

Step 5 Iterate over the pages

int pageCount = extract.getPageCount();
  for (int page = 1; page <= pageCount; page++) {
      BufferedImage img = extract.getPageAsImage(page, hasAlpha);
  }
}

Step 6 Close the PDF file

 extract.closePDFfile();

How to convert PDF to image in Java – with more control over image size

Step 1 Download JPedal trial jar.
Step 2 Create a File handle, InputStream or URL pointing to the PDF file

ConvertPagesToHiResImages extract=
new ConvertPagesToHiResImages(path);

Step 3 Include a password if file password protected

extract.setPassword("password");

Step 4 Set conversion options for output defined in JPedalSettings

HashMap options=new HashMap();

Step 5 Open the PDF file

if (extract.openPDFFile()) {

Step 6 Iterate over the pages

int pageCount = extract.getPageCount();
  for (int page = 1; page <= pageCount; page++) {
    BufferedImage img = extract.getPageAsHiResImage(page, 
    hasAlpha, options);
  }
}

Step 7 Close the PDF file

 extract.closePDFfile();



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.

Leave a Reply

Your email address will not be published. Required fields are marked *

IDRsolutions Ltd 2021. All rights reserved.