S Hornby
S Hornby

Reputation: 115

Pdfbox - Cannot find symbol for PDType0Font.load

I have been attempting to solve this issue for a while. I have the latest PDFBox (2.0.7) and FontBox (2.0.7) for my program, and yet no matter what I do, I am getting the same compilation error.

Within this class, here are my relevant imports:

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDTrueTypeFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.pdmodel.common.PDRectangle;

I am attempting to set the font with the following sample:

PDDocument pdfDoc = new PDDocument();
PDPage page = new PDPage();
pdfDoc.addPage(page);

PDPageContentStream contents = new PDPageContentStream(pdfDoc, page);
PDFont font = PDType0Font.load(pdfDoc, new File("/path/to/font/Roboto-Regular.ttf"));
contents.setFont(font, 20);

Unfortunately, as I have stated, I get the following compilation error every time:

 error: cannot find symbol
 PDFont font = PDType0Font.load(pdfDoc, new File("/path/to/font/Roboto-Regular.ttf"));
 symbol:   method load(PDDocument,File)
 location: class PDType0Font

I have looked at the Javadocs multiple times, I have opened up the JAR file to confirm that that method is there (it is), and I have tried other things such as initializing the "font" as an instance of PDType0Font instead of the interface PDFont. Same error. I have tried importing every single JAR the website offers for 2.0.7. (Preflight, xmpbox, pdfbox-tools, pdfbox-debugger) and I still get the same error. I have tried importing every single class from the pdmodel and pdmodel.font packages. Same error. Everything else works fine - it is just this one particular method. Initially I had used PDTrueTypeFont instead of PDType0Font and it was just fine. But I have to switch to PDType0Font due to foreign characters.

EDIT: Solved. It turns out an outdated Tika JAR in my classpath was creating a conflict and reverting PDFBox to version 1.8.13.

Upvotes: 1

Views: 3950

Answers (1)

S Hornby
S Hornby

Reputation: 115

This issue has been solved. It turns out there was a conflict in my classpath. I had a very outdated Tika JAR that had PDFBox 1.8 within it, so I have updated Tika to the most recent version, and no longer have issues. Thank you to Tilman Hausherr who suggested the solution.

Could it be that there's an old .jar file in your class path? Try adding Exception e = new COSVisitorException(new Exception());. If that one works, then it means you have an 1.8 version in your classpath (and you shouldn't!)

Pdfbox - Cannot find symbol for PDType0Font.load

Upvotes: 1

Related Questions