PDFBox不支持convertToImage()的操作

时间:2016-04-18 11:27:59

标签: java pdfbox

我想将pdf第一页作为jpg图像获取。该程序向我显示了一些错误:

Apr 18, 2016 1:18:40 PM org.apache.pdfbox.util.PDFStreamEngine processOperator
INFO: unsupported/disabled operation: BDC
Apr 18, 2016 1:18:40 PM org.apache.pdfbox.util.PDFStreamEngine processOperator
INFO: unsupported/disabled operation: EMC
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/fontbox/afm/AFMParser
    at org.apache.pdfbox.pdmodel.font.PDFont.addAdobeFontMetric(PDFont.java:165)
    at org.apache.pdfbox.pdmodel.font.PDFont.addAdobeFontMetric(PDFont.java:152)
    at org.apache.pdfbox.pdmodel.font.PDFont.getAdobeFontMetrics(PDFont.java:122)
    at org.apache.pdfbox.pdmodel.font.PDFont.<clinit>(PDFont.java:114)
    at org.apache.pdfbox.pdmodel.font.PDFontFactory.createFont(PDFontFactory.java:108)
    at org.apache.pdfbox.pdmodel.PDResources.getFonts(PDResources.java:213)
    at org.apache.pdfbox.util.PDFStreamEngine.getFonts(PDFStreamEngine.java:607)
    at org.apache.pdfbox.util.operator.SetTextFont.process(SetTextFont.java:59)
    at org.apache.pdfbox.util.PDFStreamEngine.processOperator(PDFStreamEngine.java:557)
    at org.apache.pdfbox.util.PDFStreamEngine.processSubStream(PDFStreamEngine.java:268)
    at org.apache.pdfbox.util.PDFStreamEngine.processSubStream(PDFStreamEngine.java:235)
    at org.apache.pdfbox.util.PDFStreamEngine.processStream(PDFStreamEngine.java:215)
    at org.apache.pdfbox.pdfviewer.PageDrawer.drawPage(PageDrawer.java:139)
    at org.apache.pdfbox.pdmodel.PDPage.convertToImage(PDPage.java:801)
    at org.apache.pdfbox.pdmodel.PDPage.convertToImage(PDPage.java:732)
    at Main.main(Main.java:26)
Caused by: java.lang.ClassNotFoundException: org.apache.fontbox.afm.AFMParser
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 16 more

Main.java:26: BufferedImage image = firstPage.convertToImage();

是否有可能使用PDFBox将第一页作为图像?

完整代码:

try {
            String sourceDir = "/home/linux/Downloads/test.pdf";
            String destinationDir = "/home/linux/Downloads/testImage";
            File sourceFile = new File(sourceDir);
            File destinationFile = new File(destinationDir);

                PDDocument document = PDDocument.load(sourceDir);
                PDPage firstPage = (PDPage) document.getDocumentCatalog().getAllPages().get(1);

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

                    BufferedImage image = firstPage.convertToImage();
                    ImageIO.write(image , "jpg", new File(destinationDir +fileName+"_"+".jpg"));

                document.close();

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

我只需将第一页解析为图像。

1 个答案:

答案 0 :(得分:0)

Convert a PDF file to image

试着研究这个问题,“标记为正确的答案”告诉了如何接近你的愿望的正确方法。 :)