如何使用pdpageconttream addRect()方法准确地绘制文本提取的矩形?
在下面的代码中,我使用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);
}
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论