PdfPCell的页码?

问题描述 投票:1回答:1

是否有可能确定PdfPCell登陆哪个页码?

当然,表格单元格可以分割多个页面,因此很乐意知道单元格顶部或底部的页码。

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

iText 5.5.3

itext
1个回答
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测试代码。

© www.soinside.com 2019 - 2024. All rights reserved.