如何使用pdpageconttream addRect()方法准确地绘制文本提取的矩形?

发布于 2025-02-03 05:10:01 字数 1594 浏览 3 评论 0原文

在下面的代码中,我使用Point2D和Dimension 2D来获取文本的坐标。

try {
    PdfTextFindCollection pdfTextFindCollection = page.findText(result.trim());
    PdfTextFind[] extractedResult = pdfTextFindCollection.getFinds();

    for (PdfTextFind find : extractedResult) {
        Point2D pointF = find.getPosition();
        Dimension2D widthHeight = find.getSize();
        double x = pointF.getX();
        double y = pointF.getY();
        double width = widthHeight.getWidth();
        double height = widthHeight.getHeight();
        position.setX(x);
        position.setY(y);
        position.setHeight(height);
        position.setWidth(width);
    }
} catch (Exception e) {
    
}

获得文本的坐标后,我需要在上面绘制一个矩形。

try ( PDPageContentStream contentStream = new PDPageContentStream(doc, page, PDPageContentStream.AppendMode.APPEND, true, true)) {
    contentStream.saveGraphicsState();
    contentStream.transform(Matrix.getScaleInstance(1, -1));
    contentStream.transform(Matrix.getTranslateInstance(0, -page.getCropBox().getHeight()));
    contentStream.saveGraphicsState();
    contentStream.setStrokingColor(Color.RED);
    contentStream.addRect(positionPropertyUnitX, positionPropertyUnitY, positionPropertyUnitWidth, positionPropertyUnitHeight);
}

输出: -

问题: - 如何使“ pdpageconttream addRect”的坐标与“ pdftextfind []”文本位置相同?从文本提取的开始到结尾,可以准确绘制矩形。

In the code below here, I was using Point2D and Dimension 2D to get the coordinate of the text.

try {
    PdfTextFindCollection pdfTextFindCollection = page.findText(result.trim());
    PdfTextFind[] extractedResult = pdfTextFindCollection.getFinds();

    for (PdfTextFind find : extractedResult) {
        Point2D pointF = find.getPosition();
        Dimension2D widthHeight = find.getSize();
        double x = pointF.getX();
        double y = pointF.getY();
        double width = widthHeight.getWidth();
        double height = widthHeight.getHeight();
        position.setX(x);
        position.setY(y);
        position.setHeight(height);
        position.setWidth(width);
    }
} catch (Exception e) {
    
}

After I get the coordinate of the text, I need to draw a rectangle on it.

try ( PDPageContentStream contentStream = new PDPageContentStream(doc, page, PDPageContentStream.AppendMode.APPEND, true, true)) {
    contentStream.saveGraphicsState();
    contentStream.transform(Matrix.getScaleInstance(1, -1));
    contentStream.transform(Matrix.getTranslateInstance(0, -page.getCropBox().getHeight()));
    contentStream.saveGraphicsState();
    contentStream.setStrokingColor(Color.RED);
    contentStream.addRect(positionPropertyUnitX, positionPropertyUnitY, positionPropertyUnitWidth, positionPropertyUnitHeight);
}

Output:-
enter image description here

Questions:-
How to make the coordinate of "PDPageContentStream addRect" same as the coordinate of "PdfTextFind[]" text position? So that the rectangle can be drawn accurately from start to end of the text extraction.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文