Page number of a PdfPCell?



是否可以确定PdfPCell位于哪个页码?

当然,一个表格单元格可以拆分为多个页面,所以很乐意知道单元格顶部或底部的页码。

最终,我想知道关闭文档后第二次渲染过程的页码和单元格边界。矩形边界很容易通过PdfPCellEvent确定。但是我的页码有问题。

iText 5.5.3

矩形边界很容易通过PdfPCellEvent确定。但是我的页码有问题。

PdfPCellEvent确实是可行的,在单元事件方法调用期间,您只需要检索当前的PdfWriter.PageNumber值,例如:

public class CellEvent : IPdfPCellEvent
{
public CellEvent(PdfWriter writer, String name)
{
this.writer = writer;
this.name = name;
}
public void CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases)
{
Console.WriteLine("Cell {0} on page {1} at {2}, {3} - {4}x{5}", name, writer.PageNumber, position.Bottom, position.Left, position.Width, position.Height);
}
PdfWriter writer;
String name;
}

像这样使用:

using (FileStream fileStream = new FileStream(@"Table-CellLayoutInformation.pdf", FileMode.Create))
using (Document document = new Document())
{
PdfWriter writer = PdfWriter.GetInstance(document, fileStream);
document.Open();
PdfPTable table = new PdfPTable(1);
table.WidthPercentage = 90;
table.SplitRows = true;
table.SplitLate = false;
for (int i = 0; i < 20; i++)
{
PdfPCell cell = new PdfPCell();
cell.CellEvent = new CellEvent(writer, i.ToString());
cell.AddElement(new Paragraph("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."));
cell.AddElement(new Paragraph("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."));
table.AddCell(cell);
}
document.Add(table);
}

我得到输出

Cell 0 on page 1 at 514, 62.14999 - 470.7x292
Cell 1 on page 1 at 222, 62.14999 - 470.7x292
Cell 2 on page 1 at 36, 62.14999 - 470.7x186
Cell 2 on page 2 at 694, 62.14999 - 470.7x112
Cell 3 on page 2 at 402, 62.14999 - 470.7x292
Cell 4 on page 2 at 110, 62.14999 - 470.7x292
Cell 5 on page 2 at 36, 62.14999 - 470.7x74
Cell 5 on page 3 at 568, 62.14999 - 470.7x238
Cell 6 on page 3 at 276, 62.14999 - 470.7x292
Cell 7 on page 3 at 36, 62.14999 - 470.7x240
Cell 7 on page 4 at 748, 62.14999 - 470.7x58
Cell 8 on page 4 at 456, 62.14999 - 470.7x292
Cell 9 on page 4 at 164, 62.14999 - 470.7x292
Cell 10 on page 4 at 36, 62.14999 - 470.7x128
Cell 10 on page 5 at 622, 62.14999 - 470.7x184
Cell 11 on page 5 at 330, 62.14999 - 470.7x292
Cell 12 on page 5 at 38, 62.14999 - 470.7x292
Cell 13 on page 6 at 514, 62.14999 - 470.7x292
Cell 14 on page 6 at 222, 62.14999 - 470.7x292
Cell 15 on page 6 at 36, 62.14999 - 470.7x186
Cell 15 on page 7 at 694, 62.14999 - 470.7x112
Cell 16 on page 7 at 402, 62.14999 - 470.7x292
Cell 17 on page 7 at 110, 62.14999 - 470.7x292
Cell 18 on page 7 at 36, 62.14999 - 470.7x74
Cell 18 on page 8 at 568, 62.14999 - 470.7x238
Cell 19 on page 8 at 276, 62.14999 - 470.7x292

上面的事件侦听器代码显然假设您确实通过将表添加到匹配的Document来绘制表。如果在任意PdfContentByte上使用PdfPTable.WriteSelectedRows绘制表格,则数据可能不正确。

使用iText 5.5.14-SNAPSHOT测试的代码。

最新更新