使用PDFBox将PDF文件转换为图像

73

有人能给我提供一个使用Apache PDFBox将PDF文件转换为不同图像(每个PDF页面为一个图像)的示例吗?


它可以只在一张图片中吗? - Mohamed Taboubi
7个回答

133

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-tools)中。在构建之前阅读2.0依赖项页面,您需要额外的jar文件来处理包含jbig2图像的PDF、用于保存为tiff图像以及读取加密文件。

确保使用您正在使用的JDK版本的最新版本,例如,如果您使用jdk8,则不要使用1.8.0_5版本,而是使用1.8.0_191或您在阅读时的最新版本。早期版本非常缓慢。


11
请注意,在使用2.0版的ImageIOUtil时,您需要在pdfbox-tools上添加依赖。 - metaforge
4
很好的例子!谢谢。不过有两点需要注意:
  • 在2.0版本中,第五行缺少BufferedImage bim =
  • 请注意,“300”是缩放级别,而不是(我之前认为的)dpi值或其他什么东西。在我遇到大量OutOfMemory异常之前,我才想到去检查API!
- colouredmirrorball
2
@Aeseir,你需要pdfbox-tools。还需要levigo jbig2解码器和jai_imageio.jar。 - Tilman Hausherr
1
谢谢,伙计。我发现他们把工具迁移到了另一个JAR文件中。 - Aeseir
1
@dieter JavaScript被忽略了(假设这是问题)。 - Tilman Hausherr
显示剩余9条评论

21

我今天尝试了PdfBox 2.0.15。

import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.rendering.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;


public static void PDFtoJPG (String in, String out) throws Exception
{
    PDDocument pd = PDDocument.load (new File (in));
    PDFRenderer pr = new PDFRenderer (pd);
    BufferedImage bi = pr.renderImageWithDPI (0, 300);
    ImageIO.write (bi, "JPEG", new File (out)); 
}

2
工作得非常好,只有一件事。如果PDF文档有多页,请使用pd.getNumberOfPages()为每个页面执行循环。 - Ralsho
这给了我Java堆空间,可能的原因是什么? - GhostDede
@GhostDede 你有尝试调整 DPI 值吗?也许降低 DPI 值可以解决你的问题。 - Spindizzy
是的,降低 DPI 值可以解决我的问题,但我需要 500 DPI。 - GhostDede
@GhostDede,很肯定你需要增加Java堆空间。500 DPI所需的堆比300 DPI更多。 - Julian Pieles

4
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、commons-logging-1.2和fontbox-2.0.8库。

祝编程愉快 :)


2

2
会有依赖关系。当您拥有具有受限权限或具有JPEG 2000图像或JBIG2图像的PDF时,您将看到它。 - Tilman Hausherr
2
在那种情况下会有依赖关系。对于我的用例,我们将创建的PDF转换为IMG,因此不存在受限制的权限或不同类型的IMG的问题。 - kittyminky
这些图像在PDF文件中。例如:https://issues.apache.org/jira/secure/attachment/12865265/jbig2.pdf 如果您事先知道输入的PDF文件中没有这些类型的图像,那么您才是安全的。 - Tilman Hausherr
链接中的javadoc没有描述其他选项(实际上它没有描述任何选项)。目前,可以使用此镜像源代码来查看选项:https://github.com/apache/pdfbox/blob/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFToImage.java - Googie

1

为了新的Apache pdfbox版本3(3.0.0-RC1),只需添加以下代码片段:

        try(PDDocument pddDoc =  Loader.loadPDF(docFile) ){
            PDFRenderer pr = new PDFRenderer (pddDoc );
            BufferedImage backImage = pr.renderImage(0);
        } catch (IOException  e) {
            e.printStackTrace();
        }

注释

  • 版本3(3.0.0-RC1)与GraalVM兼容,新发布的Liberica Native Image Kit允许在Linux / Windows / Mac上使用awt
  • PDDocument.load等已被新的org.apache.pdfbox.Loader类替换

1
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.file.Path;

public class Pdf2Image {

    public String convertPdf2Img(String fileInput, Path path) {
        String destDir = "";
        try {
            String destinationDir = path.toString();
            File sourceFile = new File(fileInput);
            File destinationFile = new File(destinationDir);

            if (!destinationFile.exists()) {
                destinationFile.mkdir();
                System.out.println("Folder Created -> " + destinationFile.getAbsolutePath());
            }

            if (sourceFile.exists()) {
                PDDocument document = PDDocument.load(sourceFile);
                PDFRenderer pdfRenderer = new PDFRenderer(document);

                String fileName = sourceFile.getName().replace(".pdf", "");

                // int pageNumber = 0;

                // for (PDPage page : document.getPages()) {
                for (int pageNumber = 0; pageNumber < document.getNumberOfPages(); ++pageNumber) {
                    BufferedImage bim = pdfRenderer.renderImage(pageNumber);

                    destDir = destinationDir + File.separator + fileName + "_" + pageNumber + ".png";

                    ImageIO.write(bim, "png", new File(destDir));
                }

                document.close();

                System.out.println("Image saved at -> " + destinationFile.getAbsolutePath());
            } else {
                System.err.println(sourceFile.getName() + " File does not exist");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return destDir;
    }

}

与其他解决方案相比,您的解决方案有什么优势? - mkl

0
这是我代码的一部分,用于将多部分文件中的PDF转换为JPG缩略图。我将图像保存为Base64字符串。使用了Pdfbox 2.0.21版本。
private static String generatePdfThumbnail(byte[] imageInBytesArray) throws IOException {
    PDDocument document = PDDocument.load(imageInBytesArray);
    PDFRenderer renderer = new PDFRenderer(document);
    BufferedImage bufferedImage = renderer.renderImage(0);
    Graphics2D bufImageGraphics = bufferedImage.createGraphics();
    bufImageGraphics.drawImage(bufferedImage, 0, 0, null);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    boolean foundWriter = ImageIO.write(bufferedImage, "jpg", baos);
    byte[] fileContent = null;
    if (!foundWriter) {
      return "";
    }

    fileContent = baos.toByteArray();
    return Base64.getEncoder().encodeToString(fileContent);
  }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接