Using PDFBox to Convert PDF Files to Images
Introduction
PDFBox is a popular open-source Java library for working with PDF documents. Among its many capabilities, PDFBox allows developers to convert PDF files into images, facilitating the extraction of individual pages as image formats. This tutorial will guide you through the process of using PDFBox to achieve this conversion.
Sample Code for Conversion
Solution for PDFBox 1.8.* versions:
PDDocument document = PDDocument.loadNonSeq(new File(pdfFilename), null); ListpdPages = 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 for PDFBox 2.0 version:
PDDocument document = PDDocument.load(new File(pdfFilename)); PDFRenderer pdfRenderer = new PDFRenderer(document); for (int page = 0; pageSolution for PDFBox 3.0 versions:
PDDocument document = Loader.loadPDF(new File(pdfFilename)); // The rest of the code is similar to the PDFBox 2.0 solution.Additional Considerations
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3