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 using Java with 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 code to convert PDF to Image…
File file = new File("/path/to/document.pdf"));
ConvertPagesToImages extract=new ConvertPagesToImages(file);
//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 code for PDF to Image conversion…
File file = new File("/path/to/document.pdf"));
ConvertPagesToHiResImages extract=new ConvertPagesToHiResImages(file);
//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();
Related tutorials on PDF to Image Conversion
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
Why do developers choose JPedal over alternatives?
- Actively developed commercial library with full support and no third party dependencies.
- Simple licensing options and source code access for OEM users.
- Process PDF files up to 3x faster than alternative Java PDF libraries.
The JPedal PDF library allows you to solve these problems in Java
Viewer viewer = new Viewer();
viewer.setupViewer();
viewer.executeCommand(ViewerCommands.OPENFILE, "pdfFile.pdf");
//Convenience static method (see class for additional options)
ExtractClippedImages.writeAllClippedImagesToDir("inputFileOrDirectory", "outputDir", "outputImageFormat", new String[] {"imageHeightAsFloat", "subDirectoryForHeight"});
//Convenience static method (see class for additional options)
ExtractTextAsWordList.writeAllWordlistsToDir("inputFileOrDirectory", "outputDir", -1);
//Convenience static method (see class for additional options)
ArrayList resultsForPages = FindTextInRectangle.findTextOnAllPages("/path/to/file.pdf", "textToFind");
PrintPdfPages print = new PrintPdfPages("C:/pdfs/mypdf.pdf");
if (print.openPDFFile()) {
print.printAllPages("Printer Name");
}
//Convenience static method (see class for additional options)
ExtractClippedImages.writeAllClippedImagesToDir("inputFileOrDirectory", "outputDir", "outputImageFormat", new String[] {"imageHeightAsFloat", "subDirectoryForHeight"});