pdfbox 2.0.2>调用PageDrawer.processPage方法捕获异常 [英] pdfbox 2.0.2 > Calling of PageDrawer.processPage method caught exceptions

查看:515
本文介绍了pdfbox 2.0.2>调用PageDrawer.processPage方法捕获异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为pdfbox 2.0.2的新手( https://github.com/apache /pdfbox/tree/2.0.2 )用户,我想得到一个页面(PDPage)的所有描边线(例如,表的列和行边框),因此我创建了以下类:
package org.apache.pdfbox.rendering;

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();
    }
}

根据我的理解,为了得到这条线一个页面的路径,必须先处理页面,所以我在行中调用了processPage方法,在那里我标记了在这里捕获异常。它意外地在提到的行中捕获了NullPointer Excpetions。异常信息如下:

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

推荐答案

扩展PageDrawer并没有真正起作用,所以我扩展了PDFGraphicsStreamEngine,结果就是这里。我做了一些在PageDrawer中完成的工作。要收集线条,要么在strokePath()中评估形状,要么在我包含println的其他方法中收集点和线。

Extending PageDrawer didn't really work, so I extended PDFGraphicsStreamEngine and here's the result. I do some of the stuff that is done in PageDrawer. To collect lines, either evaluate the shape in strokePath(), or collect points and lines in the other methods where I have included a 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
    {
    }
}

这篇关于pdfbox 2.0.2>调用PageDrawer.processPage方法捕获异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆