PDFBox Error When Converting to BufferedImage: NoClassDefFoundError: org/apache/fontbox/FontBoxFont
Asked Answered
M

2

7

I'm trying to convert a PDF doc to .png files using PDFBox. I followed this answer to get an idea of what dependencies were needed and to give me a starting point. When I try to loop through the pages and create the BufferedImage I receive a NoClassDefFoundError error. It's looking for org/apache/fontbox/FontBoxFont but through some extensive Googling I've not found a thing about FontBoxFont. Is this a separate jar that needs to be included? What is causing this error? The following .jar's are included in the project:

pdfbox-2.0.2.jar
levigo-jbig2-imageio-1.6.5.jar
pdfbox-tools-2.0.2.jar
jai-imageio-core-1.3.1.jar
commons-logging-1.2.jar

Here is the main method:

public static void main(String[] args) {

    String sourceDir = "C:/Dev/Workspace/PdfToPng/Stocks.pdf";
    String destinationDir = "C:/Dev/Workspace/PdfToPng/pages/";

    try {
        PDDocument document = PDDocument.load(new File(sourceDir));
        PDFRenderer pdfRenderer = new PDFRenderer(document);
        for(int page = 0; page < document.getNumberOfPages(); ++page) {
            BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB);
            ImageIOUtil.writeImage(bim, destinationDir + (page+1) + ".png", 300);
        }
        document.close();
    } catch(Exception e) {
        System.out.println(e.getStackTrace());
    }
}

The error is thrown on BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB);

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/fontbox/FontBoxFont
    at org.apache.pdfbox.pdmodel.font.PDFontFactory.createFont(PDFontFactory.java:75)
    at org.apache.pdfbox.pdmodel.PDResources.getFont(PDResources.java:123)
    at org.apache.pdfbox.contentstream.operator.text.SetFontAndSize.process(SetFontAndSize.java:60)
    at org.apache.pdfbox.contentstream.PDFStreamEngine.processOperator(PDFStreamEngine.java:815)
    at org.apache.pdfbox.contentstream.PDFStreamEngine.processStreamOperators(PDFStreamEngine.java:472)
    at org.apache.pdfbox.contentstream.PDFStreamEngine.processStream(PDFStreamEngine.java:446)
    at org.apache.pdfbox.contentstream.PDFStreamEngine.processPage(PDFStreamEngine.java:149)
    at org.apache.pdfbox.rendering.PageDrawer.drawPage(PageDrawer.java:189)
    at org.apache.pdfbox.rendering.PDFRenderer.renderPage(PDFRenderer.java:208)
    at org.apache.pdfbox.rendering.PDFRenderer.renderImage(PDFRenderer.java:139)
    at org.apache.pdfbox.rendering.PDFRenderer.renderImageWithDPI(PDFRenderer.java:94)
    at PdfToPng.main(PdfToPng.java:25)
Caused by: java.lang.ClassNotFoundException: org.apache.fontbox.FontBoxFont
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 12 more
Misconceive answered 13/9, 2016 at 19:1 Comment(0)
N
13

Obviously you need to add Apache FontBox 2.0.2 to your classpath, you can get it from mvnrepository

Novelia answered 13/9, 2016 at 19:32 Comment(1)
@Shaggy, when you download the bundle from - pdfbox.apache.org/download.cgi#20x - pdfbox-2.0.13-src.zip - you will get all jars in that bundle, as Nick said above.Cough
R
0

I had same issue when use Apache FontBox 2.0.2 from mvnrespoisity But when i try upgrade to laster version 2.0.26. Issue has fixed. I dont know how it fix but you can try :D.

Rhodos answered 28/6, 2022 at 5:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.