阿帕奇PDFBox的PDF转换为图片阿帕奇PDFBox的PDF转换为图片(Apache PDFBox

2019-05-09 04:04发布

能有人给我如何使用Apache PDFBox的转换在不同的图像PDF文件(一个PDF的每一页)的例子。 提前致谢

Answer 1:

。解决方案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();

不要忘记阅读1.8依赖网页做你的构建之前。

解决方案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);

    // suffix in filename will be used as the file format
    ImageIOUtil.writeImage(bim, pdfFilename + "-" + (page+1) + ".png", 300);
}
document.close();

该ImageIOUtil类是在一个单独的下载/神器(PDF的工具)。 阅读2.0依赖网页做你的构建之前,你需要用JBIG2图像的PDF额外的jar文件,保存为TIFF图像和加密文件的阅读。

确保使用最新版本的任何JDK版本您使用,也就是说,如果你使用的是jdk8,那么就不要使用版本1.8.0_5,使用1.8.0_191或任何在你正在阅读的时间最晚。 早期版本是非常缓慢的。



Answer 2:

W / O任何额外的依赖,你可以只使用PDFToImage已经包含在类PDFBox

科特林:

PDFToImage.main(arrayOf<String>("-outputPrefix", "newImgFilenamePrefix", existingPdfFilename))

其他配置OPTS: https://pdfbox.apache.org/docs/2.0.8/javadocs/org/apache/pdfbox/tools/PDFToImage.html



Answer 3:

     public class PDFtoJPGConverter {

     public List<File> convertPdfToImage(File file, String destination) throws Exception {

    File destinationFile = new File(destination);

    if (!destinationFile.exists()) {
        destinationFile.mkdir();
        System.out.println("DESTINATION FOLDER CREATED -> " + destinationFile.getAbsolutePath());
    }else if(destinationFile.exists()){
        System.out.println("DESTINATION FOLDER ALLREADY CREATED!!!");
    }else{
        System.out.println("DESTINATION FOLDER NOT CREATED!!!");
    }

    if (file.exists()) {
        PDDocument doc = PDDocument.load(file);
        PDFRenderer renderer = new PDFRenderer(doc);
        List<File> fileList = new ArrayList<File>();

        String fileName = file.getName().replace(".pdf", "");
        System.out.println("CONVERTER START.....");

        for (int i = 0; i < doc.getNumberOfPages(); i++) {
            // default image files path: original file path
            // if necessary, file.getParent() + "/" => another path
            File fileTemp = new File(destination + fileName + "_" + i + ".jpg"); // jpg or png
            BufferedImage image = renderer.renderImageWithDPI(i, 200);
            // 200 is sample dots per inch.
            // if necessary, change 200 into another integer.
            ImageIO.write(image, "JPEG", fileTemp); // JPEG or PNG
            fileList.add(fileTemp);
        }
        doc.close();
        System.out.println("CONVERTER STOPTED.....");
        System.out.println("IMAGE SAVED AT -> " + destinationFile.getAbsolutePath());
        return fileList;
    } else {
        System.err.println(file.getName() + " FILE DOES NOT EXIST");
    }
    return null;
    }

      public static void main(String[] args) {

    try {
        PDFtoJPGConverter converter = new PDFtoJPGConverter();
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter your destination folder where save image \n");
        // Destination = D:/PPL/;
        String destination = sc.nextLine();

        System.out.print("Enter your selected pdf files name with source folder \n");
        String sourcePathWithFileName = sc.nextLine();
        // Source Path = D:/PDF/ant.pdf,D:/PDF/abc.pdf,D:/PDF/xyz.pdf
        if (sourcePathWithFileName != null || sourcePathWithFileName != "") {
            String[] files = sourcePathWithFileName.split(",");
            for (String file : files) {
                File pdf = new File(file);
                System.out.print("FILE:>> "+ pdf);
                converter.convertPdfToImage(pdf, destination);
            }
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }
  }

}

====================================

在这里我使用Apache PDFBOX-2.0.8,共享记录-1.2和fontbox-2.0.8库

编码愉快:)



文章来源: Apache PDFBox convert pdf to images
标签: pdfbox