Do you need to convert your 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 you how to convert PDF to Image in Java 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.
Java PDF to image conversion (Method 1 – fast and standard quality image)
- Download JPedal trial jar.
- Create a File handle, InputStream or URL pointing to the PDF file
- Include a password if file password protected
- Open the PDF file
- Iterate over the pages
- Close the PDF file
and here is the Java…
ConvertPagesToImages extract=new ConvertPagesToImages(path);
extract.setPassword("password");
if (extract.openPDFFile()) {
int pageCount = extract.getPageCount();
for (int page = 1; page <= pageCount; page++) {
BufferedImage img = extract.getPageAsImage(page, hasAlpha);
}
}
extract.closePDFfile();
Java PDF to image conversion (Method 2 – slower and very high quality image)
- Download JPedal trial jar.
- Create a File handle, InputStream or URL pointing to the PDF file
- Include a password if file password protected
- Set conversion options for output defined in JPedalSettings
- Open the PDF file
- Iterate over the pages
- Close the PDF file
and here is the Java…
ConvertPagesToHiResImages extract=new ConvertPagesToHiResImages(path);
extract.setPassword("password");
HashMap options=new HashMap();
if (extract.openPDFFile()) {
int pageCount = extract.getPageCount();
for (int page = 1; page <= pageCount; page++) {
BufferedImage img = extract.getPageAsImage(page, hasAlpha);
}
}
extract.closePDFfile();
Related tutorials
If you want to convert a PDF file directly to any of these image formats in Java, check out our related tutorials. Here we document how to
The JPedal PDF library allows you to
Display PDF files in Java Apps |
View PDF files in Java |
Convert PDF Files to image |