PDFBox:将 PDF 转换为图像比预期慢

bas*_*aad 5 java javafx pdfbox javafx-8

我目前正在使用此代码将 pdf 转换为图像:

@SuppressWarnings("unchecked")
public static Image convertPDFtoImage(ByteArrayInputStream bais) {

    Image convertedImage = null;

    try {

        PDDocument document = PDDocument.load(bais);
        List<PDPage> list = document.getDocumentCatalog().getAllPages();
        PDPage page = list.get(0);

        BufferedImage image = page.convertToImage(BufferedImage.TYPE_INT_RGB, 64);
        convertedImage = SwingFXUtils.toFXImage(image, null);

        document.close();

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

    return convertedImage;
}
Run Code Online (Sandbox Code Playgroud)

然后,我在 JavaFX ImageView 中显示了转换后的图像。

此外,我需要导入这两个包,而我不使用它们:

import org.apache.commons.logging.LogFactory;
import org.apache.fontbox.afm.AFMParser;
Run Code Online (Sandbox Code Playgroud)

两个问题:

  • 将一个简单的一页 PDF 转换为图像,其中 DPI 设置为 64(我认为这不是那么高)通常需要两到三秒钟吗?好像有点慢。
  • 为什么我不使用这两个导入时需要它们?如果我不导入它们,我会收到很多错误并且转换不起作用。

我想在 JavaFX 中快速显示 PDF,两到三秒太长了。非常欢迎在 JavaFX 中显示 PDF 的任何其他方式(除了将其转换为图像)。

任何帮助是极大的赞赏!

Yog*_*rni 1

  1. 我在将 pdf 转换为图像时也遇到了类似的问题,我通过将 PDFBox 从 1.8 升级到 2.0 解决了这个问题。这使我的表现提高了 50%。以前我的应用程序需要大约 10 秒才能将 pdf 转换为图像,现在只需 5 秒。升级 PDFBox 时请使用以下链接作为参考 -
    https://pdfbox.apache.org/2.0/migration.html

  2. PDFBox 不需要额外导入。

问候,
尤格什