Generate Thumbnails from PDFs in Java
This guide shows how to use a scaled page render to create a thumbnail image.
Render a Page to Scale
Creating a thumbnail uses the same method that’s used to render a page.
Using PdfPage.renderPage
, pass the dimensions (in pixels) of the thumbnail to renderPage
. For example, for a 50×50-pixel thumbnail, do the following:
final PdfDocument document = PdfDocument.open(new FileDataProvider(new File("document.pdf"))); final PdfPage page = document.getPage(0); BufferedImage bitmap = page.renderPage(50, 50);
Saving a Bitmap to a File
renderPage
returns a native Java BufferedImage
object. Saving it out as a PNG image, for example, would look like this:
final BufferedImage bitmap = page.renderPage(); final File file = new File("out/test.png"); final boolean written = ImageIO.write(bitmap, "png", file);