Introduction
Java cannot automatically generate thumbnails of PDF pages, but luckily there is plenty of software which can do this. This example uses our own JPedal library to create Thumbnails in just a few lines of Java code. JPedal is the best Java PDF library for developers.
How to convert PDF to thumbnail images with JPedal
- Download a trial copy of JPedal and add it to your IDE.
- Create a File handle, InputStream or URL pointing to the PDF file
- Include a password if file password protected
- Choose scaling to give required size
- Open the PDF file
- Iterate over the pages
and the Java code to convert PDF to thumbnail image…
File path = new File("/path/to/file.pdf");
ConvertPagesToImages extract=new ConvertPagesToImages(path);
extract.setPassword("password");
extract.setPageScaling(0.25f); //adjust as needed
if (extract.openPDFFile()) {
int pageCount = extract.getPageCount();
for (int page = 1; page <= pageCount; page++) {
BufferedImage img = extract.getPageAsImage(page, hasAlpha);
}
}
extract.closePDFfile();
Key points to remember
The usual reason for doing this conversion is to display the content as a small image thumbnail. However, there are things to remember:
1. Bitmapped images do not scale very well (unlike Vector formats like PDF). So you need to get the size correct. If you make it too small you will not be able to zoom in without pixelation. If you make it too big you will make the download slower and need more memory.
2. Bitmapped images do not have some of the other advantages of PDF files (like text search). You may need to add manual functionality to your application if you wish to have things like text highlighting.
Conclusion
This tutorial showed you how you can use JPedal as a pure Java document thumbnail generator to programmatically produce quality thumbnails without loss in quality.
We have been working with PDFs for 25 years and have other resources to help you learn more about the PDF format.