首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >pdfbox 2.0.2 >调用PageDrawer.processPage方法捕获异常

pdfbox 2.0.2 >调用PageDrawer.processPage方法捕获异常
EN

Stack Overflow用户
提问于 2016-08-13 17:45:40
回答 1查看 3.4K关注 0票数 4

作为pdfbox2.0.2 (https://github.com/apache/pdfbox/tree/2.0.2)用户的新手,我希望获得页面(PDPage)的所有笔画行(例如,表格的列和行边框),因此我创建了以下类: package org.apache.pdfbox.rendering;

代码语言:javascript
运行
复制
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,其中我标记为"catch exception here“。它意外地在提到的行中捕获了NullPointer Excpetions。异常信息如下:

代码语言:javascript
运行
复制
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)

有没有人可以给我一些关于我的逻辑的建议或帮助调试代码?提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-13 21:08:06

扩展PageDrawer并不能真正起作用,所以我扩展了PDFGraphicsStreamEngine,结果如下。我做了一些在PageDrawer中做的事情。要收集线条,可以在strokePath()中对形状求值,或者在我包含println的其他方法中收集点和线。

代码语言:javascript
运行
复制
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的后续答案。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38931422

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档