简体   繁体   中英

PDFBox unsupported operation for convertToImage()

I want to get pdf first page as jpg image. The program shows me some errors:

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();

Is there any possibility to get first page as image by using PDFBox?

Full code:

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();
}

I just need to parse first page as image.

Convert a PDF file to image

Trying looking into this question, the "marked as correct answer" tells the correct way of how to approach your desire. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM