PDF files are not directly supported by Java. This tutorial shows you how to convert a PDF file to images in simple steps using JPedal PDF library.
Why use a third party library to handle PDF files?
PDF files are a very complex binary/text hybrid data structure which is a subset of the even more complicated Postscript format. The data needs to be parsed and assembled from many sources to create the pages displayed or extract images from a PDF file. In this example, we will use our JPedal PDF library to make this task simple.
How to convert PDF to image in JPedal – simple example
Step 1 Create a File handle, InputStream or URL pointing to the PDF file
ConvertPagesToImages extract=new ConvertPagesToImages(path); |
Step 2 Include a password if file password protected
extract.setPassword("password"); |
Step 3 Open the PDF file
if (extract.openPDFFile()) { |
Step 4 Iterate over the pages
int pageCount = extract.getPageCount(); for (int page = 1; page <= pageCount; page++) { BufferedImage img = extract.getPageAsImage(page, hasAlpha); } } |
Step 5 Close the PDF file
extract.closePDFfile(); |
How to convert PDF to image in JPedal – with more control over extraction
Step 1 Create a File handle, InputStream or URL pointing to the PDF file
ConvertPagesToHiResImages extract= new ConvertPagesToHiResImages(path); |
Step 2 Include a password if file password protected
extract.setPassword("password"); |
Step 2 Set conversion options for output defined in JPedalSettings
HashMap options=new HashMap(); |
Step 3 Open the PDF file
if (extract.openPDFFile()) { |
Step 4 Iterate over the pages
int pageCount = extract.getPageCount(); for (int page = 1; page <= pageCount; page++) { BufferedImage img = extract.getPageAsHiResImage(page, hasAlpha, options); } } |
Step 6 Close the PDF file
extract.closePDFfile(); |