作为pdfbox 2.0.2(https://github.com/apache/pdfbox/tree/2.0.2)用户的新手,我想得到一个页面(PDPage)的所有描边线(例如,表的列和行边框),因此我创建了以下类: 包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();
}
}
根据我的理解,为了获得页面的行路径,必须首先处理页面,所以我在行中调用了processPage方法,在那里我标记了"在这里捕获异常" 。它意外地在提到的行中捕获了NullPointer Excpetions。例外信息如下:
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)
有没有人可以就我的逻辑提供一些建议或帮助调试代码?提前致谢
答案 0 :(得分:4)
扩展PageDrawer并没有真正起作用,所以我扩展了PDFGraphicsStreamEngine,这就是结果。我做了一些在PageDrawer中完成的事情。要收集线条,要么在strokePath()中评估形状,要么在我包含println的其他方法中收集点和线。
public class LineCatcher extends PDFGraphicsStreamEngine
{
private final GeneralPath linePath = new GeneralPath();
private int clipWindingRule = -1;
public LineCatcher(PDPage page)
{
super(page);
}
public static void main(String[] args) throws IOException
{
try (PDDocument document = PDDocument.load(new File("Test.pdf")))
{
PDPage page = document.getPage(0);
LineCatcher test = new LineCatcher(page);
test.processPage(page);
}
}
@Override
public void appendRectangle(Point2D p0, Point2D p1, Point2D p2, Point2D p3) throws IOException
{
System.out.println("appendRectangle");
// to ensure that the path is created in the right direction, we have to create
// it by combining single lines instead of creating a simple rectangle
linePath.moveTo((float) p0.getX(), (float) p0.getY());
linePath.lineTo((float) p1.getX(), (float) p1.getY());
linePath.lineTo((float) p2.getX(), (float) p2.getY());
linePath.lineTo((float) p3.getX(), (float) p3.getY());
// close the subpath instead of adding the last line so that a possible set line
// cap style isn't taken into account at the "beginning" of the rectangle
linePath.closePath();
}
@Override
public void drawImage(PDImage pdi) throws IOException
{
}
@Override
public void clip(int windingRule) throws IOException
{
// the clipping path will not be updated until the succeeding painting operator is called
clipWindingRule = windingRule;
}
@Override
public void moveTo(float x, float y) throws IOException
{
linePath.moveTo(x, y);
System.out.println("moveTo");
}
@Override
public void lineTo(float x, float y) throws IOException
{
linePath.lineTo(x, y);
System.out.println("lineTo");
}
@Override
public void curveTo(float x1, float y1, float x2, float y2, float x3, float y3) throws IOException
{
linePath.curveTo(x1, y1, x2, y2, x3, y3);
System.out.println("curveTo");
}
@Override
public Point2D getCurrentPoint() throws IOException
{
return linePath.getCurrentPoint();
}
@Override
public void closePath() throws IOException
{
linePath.closePath();
}
@Override
public void endPath() throws IOException
{
if (clipWindingRule != -1)
{
linePath.setWindingRule(clipWindingRule);
getGraphicsState().intersectClippingPath(linePath);
clipWindingRule = -1;
}
linePath.reset();
}
@Override
public void strokePath() throws IOException
{
// do stuff
System.out.println(linePath.getBounds2D());
linePath.reset();
}
@Override
public void fillPath(int windingRule) throws IOException
{
linePath.reset();
}
@Override
public void fillAndStrokePath(int windingRule) throws IOException
{
linePath.reset();
}
@Override
public void shadingFill(COSName cosn) throws IOException
{
}
}
更新19.3.2019:另请参阅mkl here的后续回答。