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)
- Download JPedal trial jar.
- Create a File handle, InputStream or URL pointing to the PDF file
ConvertPagesToImages extract=new ConvertPagesToImages(path);
- Include a password if file password protected
extract.setPassword("password");
- Open the PDF file
if (extract.openPDFFile()) {
- Iterate over the pages
int pageCount = extract.getPageCount(); for (int page = 1; page <= pageCount; page++) { BufferedImage img = extract.getPageAsImage(page, hasAlpha); } }
- Close the PDF file
extract.closePDFfile();
How to convert PDF to image in Java (Method 2 – slower and very high quality image)
- Download JPedal trial jar.
- Create a File handle, InputStream or URL pointing to the PDF file
ConvertPagesToHiResImages extract= new ConvertPagesToHiResImages(path);
- Include a password if file password protected
extract.setPassword("password");
- Set conversion options for output defined in JPedalSettings
HashMap options=new HashMap();
- Open the PDF file
if (extract.openPDFFile()) {
- Iterate over the pages
int pageCount = extract.getPageCount(); for (int page = 1; page <= pageCount; page++) { BufferedImage img = extract.getPageAsHiResImage(page, hasAlpha, options); } }
- 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