Java – pdfbox: the problem of converting PDF pages into images
•
Java
My mission is simple: convert every page of PDF file into an image I try to use the open source version of icepdf to generate images, but they don't use the correct font to generate images So I started using PDF@R_494_2419 @. the code is as follows:
PDDocument document = PDDocument.load(new File("testing.pdf")); List<PDPage> pages = document.getDocumentCatalog().getAllPages(); for (int i = 0; i < pages.size(); i++) { PDPage singlePage = pages.get(i); BufferedImage buffImage = convertToImage(singlePage,8,12); ImageIO.write(buffImage,"png",new File(PdfUtil.DATA_OUTPUT_DIR+(count++)+".png")); }
The font looks good, but the pictures in the PDF file look dizzy (see attachment) I look at the source code, but I still have no clue how to solve it Do you have any ideas? Please help. thank you!!
Solution
use pdf@R_494_2419 @Convert PDF files to images
Download this file and paste it into the documents folder
Example:
package com.pdf.pdf@R_494_2419@.test; import java.awt.HeadlessException; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.io.File; import java.util.List; import org.apache.pdf@R_494_2419@.pdmodel.PDDocument; import org.apache.pdf@R_494_2419@.pdmodel.PDPage; import org.apache.pdf@R_494_2419@.util.PDFImageWriter; public class ConvertPDFPageToImageWithoutText { public static void main(String[] args) { try { String oldPath = "C:/Documents/04-Request-Headers.pdf"; File oldFile = new File(oldPath); if (oldFile.exists()) { PDDocument document = PDDocument.load(oldPath); @SuppressWarnings("unchecked") List<PDPage> list = document.getDocumentCatalog().getAllPages(); String fileName = oldFile.getName().replace(".pdf",""); String imageFormat = "png"; String password = ""; int startPage = 1; int endPage = list.size(); String outputPrefix = "C:/Documents/PDFCopy/";//converted images saved here File file = new File(outputPrefix); if (!file.exists()) { file.mkdirs(); } int imageType = 24; String color = "rgb"; int resolution; try { resolution = Toolkit.getDefaultToolkit().getScreenResolution(); } catch (HeadlessException e) { resolution = 96; } if ("bilevel".equalsIgnoreCase(color)) { imageType = BufferedImage.TYPE_BYTE_BINARY; } else if ("indexed".equalsIgnoreCase(color)) { imageType = BufferedImage.TYPE_BYTE_INDEXED; } else if ("gray".equalsIgnoreCase(color)) { imageType = BufferedImage.TYPE_BYTE_GRAY; } else if ("rgb".equalsIgnoreCase(color)) { imageType = BufferedImage.TYPE_INT_RGB; } else if ("rgba".equalsIgnoreCase(color)) { imageType = BufferedImage.TYPE_INT_ARGB; } else { System.err.println("Error: the number of bits per pixel must be 1,8 or 24."); } PDFImageWriter pdfImageWriter = new PDFImageWriter(); boolean imageWriter = pdfImageWriter.writeImage(document,imageFormat,password,startPage,endPage,outputPrefix + fileName,imageType,resolution); if (!imageWriter) { throw new Exception("No writer found for format '" + imageFormat + "'"); } document.close(); } else { System.err.println(oldPath +" File Can't be found"); } } catch (Exception e) { e.printStackTrace(); } }
}
or
Try the following solution to convert PDF files to image format
How to Convert PDF to image with resolution in java Using PDF Renderer
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码