【问题标题】:Text is missing when converting pdf file into image in java using pdfbox使用pdfbox将pdf文件转换为java中的图像时缺少文本
【发布时间】:2014-01-11 06:35:22
【问题描述】:

我想将 PDF 页面转换为图像文件。当我使用 java 将 PDF 页面转换为图像时,文本丢失。

转换后我要转换的文件46_2.pdf 向我显示46_2.png

代码:

import java.awt.image.BufferedImage;
import java.io.File;
import java.util.List;

import javax.imageio.ImageIO;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;

public class ConvertPDFPageToImageWithoutText {
    public static void main(String[] args) {
        try {
            String oldPath = "C:/PDFCopy/46_2.pdf";
            File oldFile = new File(oldPath);
           if (oldFile.exists()) {

            PDDocument document = PDDocument.load(oldPath);
            List<PDPage> list = document.getDocumentCatalog().getAllPages();

            for (PDPage page : list) {
                BufferedImage image = page.convertToImage();
                File outputfile = new File("C:/PDFCopy/image.png");
                ImageIO.write(image, "png", outputfile);
                document.close();
            }

        }

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

【问题讨论】:

  • 我会尝试使用 convertToImage( type, resolution ) 方法,看看你会得到什么。我敢打赌,您将不得不多次修改该决议以使其正确。 pdfbox.apache.org/docs/1.8.3/javadocs/org/apache/pdfbox/pdmodel/…, int)
  • 1.8.x 版本在字体渲染方面存在不足。这些已在未发布的 2.0 版本中解决,您可以使用 svn 从存储库中获取,并使用 maven 构建。
  • @TilmanHausherr 你能给我一个下载jar的链接吗?
  • pdfbox.apache.org/downloads.html#scm 注意API是不同的(尤其是渲染),所以看看例子看看它是怎么做的。

标签: java pdf pdfbox javax.imageio apache-commons-logging


【解决方案1】:

由于您使用的是 PDFBox,请尝试使用PDFImageWriter.writeToImage 而不是 PDPage.convertToImage。 This post 似乎与您正在尝试做的事情相关。

【讨论】:

  • 啊,太糟糕了。根据this link ...似乎某些字体存在已知问题。
  • PDFImageWriter.writeToImage 给了我同样的输出。
  • 我明白了。我告诉你 PDFBox apparently has issues with some fonts,所以我认为在开发人员修复 pdfbox 之前,你无法让 PDFBox 成功保留该文本。
  • PDFImageWriter.writeImage() 在内部使用 PDPage.convertToImage() 并将结果 BufferedImage 保存到文件系统中。
【解决方案2】:

我遇到了同样的问题。我找到了一篇文章(不幸的是我不记得在哪里,因为我已经阅读了数百篇)。有作者抱怨将Java版本更新到7.21后PDFBox出现了这样的问题。所以我使用的是 7.17,它对我有用:)

【讨论】:

    【解决方案3】:

    使用最新版本的 PDFBox(我使用的是 2.0.9) 并添加来自 hereJAI Image I/O 依赖项。这是 JAVA 7 上的示例运行代码。

        public void pdfToImageConvertorUsingPdfBox(String inputPdfPath) throws Exception {
        File sourceFile = new File(inputPdfPath);
        String formatName = "png";
        if (sourceFile.exists()) {
            PDDocument document = PDDocument.load(sourceFile);
            PDFRenderer pdfRenderer = new PDFRenderer(document);
            int count = document.getNumberOfPages();
    
            for (int i = 0; i < count; i++) {
                BufferedImage image = pdfRenderer.renderImageWithDPI(i, 200, ImageType.RGB);
                String output = FilenameUtils.removeExtension(inputPdfPath) + "_" + (i + 1) + "." + formatName;
                ImageIO.write(image, formatName, new File(output));
            }
            document.close();
        } else {
            logger.error(sourceFile.getName() + " File not exists");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-06-13
      • 2018-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多