PdfPCell的页码?

时间:2018-12-10 03:56:59

标签: itext

是否可以确定PdfPCell登陆在哪个页面上?

当然,一个表格单元格可以拆分成多个页面,因此很高兴知道单元格顶部或底部的页码。

最终,我想在关闭文档后知道第二次渲染过程的页码和单元格范围。矩形边界很容易通过PdfPCellEvent确定。但是我在页号上遇到了麻烦。

iText 5.5.3

1 个答案:

答案 0 :(得分:0)

  

很容易通过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来绘制表。如果通过在任意PdfPTable.WriteSelectedRows上使用PdfContentByte来绘制表格,则数据可能不正确。

代码已通过iText 5.5.14-SNAPSHOT测试。