You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何判断裁剪路径是否包含于PDF的常规描边/填充路径(PDFBox3.0.2)

PDFBox 3.0.2裁剪路径与常规路径关联判断问题

我使用PDFBox 3.0.2提取PDF文件中的裁剪路径与常规路径,但无法确定获取到的裁剪路径是否与常规路径相关联,或是包含在常规路径范围内。有时会提取到大量裁剪路径,无法判断这些路径是否为所需内容,以及是否处于常规路径之中。

获取路径的方法

裁剪路径获取入口

@Override
public void endPath() throws IOException {}

常规路径获取入口

@Override
public void strokePath() {}
@Override
public void fillPath(int i) {}

裁剪路径具体实现代码

@Override
public void endPath() throws IOException {
    if (clipWindingRule != -1) {
        linePath.setWindingRule(clipWindingRule); 
        Rectangle2D bounds = linePath.getBounds2D();
        ClipPathInfo info = new ClipPathInfo();
        Area currentClippingPath = getGraphicsState().getCurrentClippingPath();
        if (clipWindingRule == PathIterator.WIND_EVEN_ODD) {
            info.setRule("Even-Odd");
        } else {
            info.setRule("NonZero");
        }

        Matrix ctm = getGraphicsState().getCurrentTransformationMatrix();

        PDRectangle cropBox = page.getCropBox();
        float pageHeight = cropBox.getHeight();

        double x = bounds.getX() * scale;
        double y = (pageHeight - bounds.getY() - bounds.getHeight()) * scale;
        double width = bounds.getWidth() * scale;
        double height = bounds.getHeight() * scale;

        Matrix matrix = new Matrix();
        matrix.scale(1, -1);
        matrix.translate(0, (float) -y);
        matrix.scale(scale, scale);
        Matrix multiply = matrix.multiply(ctm);

        double[][] mCTMatrix = {
            {multiply.getScaleX(), multiply.getShearY(), 0},
            {multiply.getShearX(), multiply.getScaleY(), 0},
            {multiply.getTranslateX(), multiply.getTranslateY(), 1},
        };
        info.setCtm(mCTMatrix);

        info.setBoundary(new OfdBoxImpl(x, y, width, height));

        AbbreviatedData data = new AbbreviatedData();
        drawLine(linePath.getPathIterator(null), data, pageHeight * scale, true);
        info.setData(data);
        clipPathInfos.add(info);
        clipWindingRule = -1;
    }
    linePath.reset();
}

相关辅助代码

@Override
public void appendRectangle(Point2D p0, Point2D p1, Point2D p2, Point2D p3) {
    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());
    linePath.closePath();
}


private void drawLine(PathIterator iterator, AbbreviatedData data, float height, boolean isPath) throws IOException {
    double[] coords = new double[6];
    while (!iterator.isDone()) {
        switch (iterator.currentSegment(coords)) {
            case SEG_MOVETO:
                data.moveTo(coords[0] * scale, height - coords[1] * scale);
                break;
            case SEG_LINETO:
                data.lineTo(coords[0] * scale, height - coords[1] * scale);
                break;
            case SEG_CUBICTO:
                data.B(coords[0] * scale, height - coords[1] * scale,
                    coords[2] * scale, height - coords[3] * scale,
                    coords[4] * scale, height - coords[5] * scale);
                break;
            case SEG_CLOSE:
                data.close();
                if (isPath) {
                    closePath();
                }
                break;
            default:
                break;
        }
        iterator.next();
    }
}

内容的提问来源于stack exchange,提问作者Serendipity

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 10:25:06