java - 在 iText 中检索段落的坐标
问题描述
我正在尝试从Paragraph
iText 中创建的 x、y 坐标检索。我遵循了如何在 iText 7 中编写文档时获得垂直光标位置中的批准答案?但我没有得到预期的结果。
PdfDocument pdfDoc = new PdfDocument(new PdfWriter("output/ITextSandbox/Coordinates.pdf"));
pdfDoc.setDefaultPageSize(PageSize.LETTER); // 8.5 x 11
Document document = new Document(pdfDoc);
PdfFont font = PdfFontFactory.createFont(StandardFonts.COURIER, PdfEncodings.UTF8);
document.setFont(font);
document.setFontSize(10);
Paragraph paragraph = null;
// Print 5 lines to ensure the y coord is sufficiently moved away from the top of the page.
for (int i = 1; i <= 5; i++)
{
paragraph = new Paragraph();
paragraph.add(new Text("Line " + i));
document.add(paragraph);
}
// Print a new paragraph from which to obtain the x, y coordinates.
paragraph = new Paragraph();
paragraph.add(new Text("Line 6"));
document.add(paragraph);
// Follow the steps from the approved answer in
// https://stackoverflow.com/questions/51953723/how-to-get-vertical-cursor-position-when-writing-document-in-itext-7
IRenderer renderer = paragraph.createRendererSubTree().setParent(document.getRenderer());
float width = document.getPageEffectiveArea(PageSize.LETTER).getWidth();
float height = document.getPageEffectiveArea(PageSize.LETTER).getHeight();
LayoutResult layoutResult = renderer.layout(new LayoutContext(new LayoutArea(1, new Rectangle(width, height))));
float y = layoutResult.getOccupiedArea().getBBox().getY();
float x = layoutResult.getOccupiedArea().getBBox().getX();
System.out.println("x = " + x + ", y = " + y); // y should be approximately 630, not 710.
使用标准边距和 10 pt 字体,第 6 行的坐标应该大约是 x = 0,y = 630。相反,我得到 y = 710。
解决方案
问题中的代码示例模拟了在某个布局区域上的渲染。如果在完全相同的布局区域上模拟完全相同的元素,则该方法可以确定页面上的坐标。
在问题中,仅模拟了第 6 段,因此预计 y 坐标较大(页面上较高)。为了获得正确的 y 坐标,还必须模拟前面的 5 段。
此外,布局区域与页面不同。此代码没有正确考虑页边距:
float width = document.getPageEffectiveArea(PageSize.LETTER).getWidth();
float height = document.getPageEffectiveArea(PageSize.LETTER).getHeight();
LayoutResult layoutResult =
renderer.layout(new LayoutContext(new LayoutArea(1, new Rectangle(width, height))));
使用 的默认页边距36
,正确的布局区域将是:
float width = document.getPageEffectiveArea(PageSize.LETTER).getWidth();
float height = document.getPageEffectiveArea(PageSize.LETTER).getHeight();
LayoutResult layoutResult =
renderer.layout(new LayoutContext(new LayoutArea(1, new Rectangle(36, 36, width, height))));
渲染某些元素后获取当前坐标的一种更简单的方法是:
/*...*/
paragraph = new Paragraph();
paragraph.add(new Text("Line 6"));
document.add(paragraph);
Rectangle remaining = document.getRenderer().getCurrentArea().getBBox();
float y = remaining.getTop();
System.out.println("y = " + y);
结果:y = 631.6011
为了说明剩余的布局区域,让我们在页面上绘制它:
PdfCanvas canvas = new PdfCanvas(pdfDoc.getPage(1));
canvas.setStrokeColor(ColorConstants.RED).rectangle(remaining).stroke();
有一些不同的页边距:
document.setMargins(5, 25, 15, 5);
推荐阅读
- python - 为什么opencv和枕头将png图像转换为差异值范围
- sql - 如何使用 Postgres 添加指向 2 个表之一的外键?
- linux - Postgres 扩展不可用 - CentOS 8
- python - 如何使提升的小部件在 Qt Designer 中显示得更大
- r - 进一步按年份将绘图数据缩放为间隔
- python - TypeError:“NoneType”对象不可下标(PYTHON)
- c# - 无法为 WSDL 文件创建 Web 引用
- javascript - Javascript HasOwnProperty Polyfill
- python - 如何为大型图像数据集使用更少的内存?(Python - Keras)
- php - 如何使用数组中的值重新格式化 JSON 响应