Home >Java >javaTutorial >How to Convert PDF Files to Images with PDFBox?

How to Convert PDF Files to Images with PDFBox?

Susan Sarandon
Susan SarandonOriginal
2024-12-07 07:22:10813browse

How to Convert PDF Files to Images with PDFBox?

Converting PDF Files to Images with PDFBox

Apache PDFBox offers a comprehensive set of tools for manipulating PDF documents, including converting them into images. By employing PDFBox, you can efficiently extract individual pages of a PDF file as separate images.

Solution (Version 1.8.*):

PDDocument document = PDDocument.loadNonSeq(new File(pdfFilename), null);
List<PDPage> pdPages = document.getDocumentCatalog().getAllPages();
int page = 0;
for (PDPage pdPage : pdPages)
{
    ++page;
    BufferedImage bim = pdPage.convertToImage(BufferedImage.TYPE_INT_RGB, 300);
    ImageIOUtil.writeImage(bim, pdfFilename + "-" + page + ".png", 300);
}
document.close();

Solution (Version 2.0):

PDDocument document = PDDocument.load(new File(pdfFilename));
PDFRenderer pdfRenderer = new PDFRenderer(document);
for (int page = 0; page < document.getNumberOfPages(); ++page)
{
    BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB);
    ImageIOUtil.writeImage(bim, pdfFilename + "-" + (page+1) + ".png", 300);
}
document.close();

Solution (Version 3.0):

PDDocument document = Loader.loadPDF(new File(pdfFilename));

Remember to configure logging and use the latest JDK version available.

The above is the detailed content of How to Convert PDF Files to Images with PDFBox?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn