As a newbie of pdfbox 2.0.2 (https://github.com/apache/pdfbox/tree/2.0.2) user, I would like to get all the stroked lines (for instance, column and row borders of a table) of a page (PDPage), and thus I created the following class: package org.apache.pdfbox.rendering;
import java.awt.geom.GeneralPath;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import org.apache.commons.io.IOUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.rendering.PageDrawer;
import org.apache.pdfbox.rendering.PageDrawerParameters;
public class LineCatcher {
private PageDrawer pageDrawer;
private PDDocument document;
private PDFRenderer pdfRenderer;
private PDPage page;
public LineCatcher(URI pdfSrcURI) throws IllegalArgumentException,
MalformedURLException, IOException {
this.document = PDDocument.load(IOUtils.toByteArray(pdfSrcURI));
this.pdfRenderer = new PDFRenderer(this.document);
}
public GeneralPath getLinePath(int pageIndex) throws IOException {
this.page = this.document.getPage(pageIndex);
PageDrawerParameters parameters = new PageDrawerParameters (this.pdfRenderer, this.page);
this.pageDrawer = new PageDrawer(parameters);
this.pageDrawer.processPage(this.page); //catches exception here
return this.pageDrawer.getLinePath();
}
}
According to my understanding, in order to get the line path of a page, the page has to be processed first, so I called the method processPage in the line, where I marked "catch exception here". It caught NullPointer Excpetions int the mentioned line unexpectedly. The exception info are the following:
java.lang.NullPointerException
at org.apache.pdfbox.rendering.PageDrawer.fillPath(PageDrawer.java:599)
at org.apache.pdfbox.contentstream.operator.graphics.FillNonZeroRule.process(FillNonZeroRule.java:36)
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.LineCatcher.getLinePath(LineCatcher.java:33)
at org.apache.pdfbox.rendering.TestLineCatcher.testGetLinePath(TestLineCatcher.java:21)
Is there anyone, who can give some advice about my logic or help to debug the code? Thanks in advance