【问题标题】:How to get last written data location in a PDF page during PDF creation using iTextSharp?如何在使用 iTextSharp 创建 PDF 期间获取 PDF 页面中最后写入的数据位置?
【发布时间】:2016-10-07 10:57:45
【问题描述】:

我正在使用 iTextSharp 将数百个表格一个接一个地添加到 PDF 文档中。但是这样做的问题是我们不知道何时创建新页面。有时表格的一半转到下一页,一半留在当前页面。有什么办法可以让我有最后写的位置,以便我可以决定是否创建新页面。

我在 StackOverFlow 上找到了一些代码,但没有一个对我有用。 在向文档添加新数据之前,我尝试使用以下代码获取位置。

float y = PdfPageHeight; 
for(int i=0;i<100;i++)
{
    if(y<=document.document.BottomMargin)
    {
      document.NewPage();
    }
    mainTableHeader = new PdfPTable(1);
    mainTableHeader.SetWidthPercentage(new float[] { PageSize.A4.Width }, PageSize.A4);
    AddContent(ref mainTableHeader); //Adding some cells to the table
    document.Add(mainTableHeader);
    y=writer.GetVerticalPosition(false);
} 

如果有人知道怎么做,请帮助我。

【问题讨论】:

  • 我不知道它是否会有所帮助,但PdfPTable 有一个名为TableEvent 的属性,您可以通过创建一个实现接口IPdfPTableEvent 的类来订阅它,实际上还有更多名为IPdfPTableEventSplit 的信息接口,专用于表拆分。您可以查看其usage here 的示例。您也许可以使用它来跟踪全局对象。
  • y&lt;=document.document.RightMargin - 为什么要将 y 值与 right 边距进行比较。这没有意义,不是吗?
  • @mkl - 嘿,它的“y
  • 你什么时候做那个测试? writer.GetVerticalPosition 仅在将某些内容添加到文档时更改,而不是在您仍然构建要添加的表格时更改。
  • 您是使用document.Add() 将表格添加到文档中,还是使用ColumnTextWriteSelectedRows() 之类的东西?

标签: c#-4.0 pdf-generation itextsharp


【解决方案1】:

我对您的代码进行了细微更改(将您的未知方法 AddContent 替换为实际代码向表格中添加了一些单元格;添加了一些 Console 输出):

using (Document document = new Document())
{
    PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(dest, FileMode.Create, FileAccess.Write));
    document.Open();

    float y = document.PageSize.Height;
    for (int i = 0; i < 100; i++)
    {
        if (y <= document.BottomMargin)
        {
            Console.Write("New Page!\n");
            document.NewPage();
        }
        PdfPTable mainTableHeader = new PdfPTable(1);
        mainTableHeader.SetWidthPercentage(new float[] { PageSize.A4.Width }, PageSize.A4);
        mainTableHeader.AddCell("Test");
        mainTableHeader.AddCell(writer.GetVerticalPosition(false).ToString());
        mainTableHeader.AddCell(writer.GetVerticalPosition(false).ToString());
        document.Add(mainTableHeader);
        y = writer.GetVerticalPosition(false);
        Console.Write("After table {0} y is at {1}\n", i, y);
    } 
}

运行我在控制台上看到的这段代码:

After table 0 y is at 758
After table 1 y is at 710
After table 2 y is at 662
After table 3 y is at 614
After table 4 y is at 566
After table 5 y is at 518
After table 6 y is at 470
After table 7 y is at 422
After table 8 y is at 374
After table 9 y is at 326
After table 10 y is at 278
After table 11 y is at 230
After table 12 y is at 182
After table 13 y is at 134
After table 14 y is at 86
After table 15 y is at 38
After table 16 y is at 758
After table 17 y is at 710
After table 18 y is at 662
After table 19 y is at 614
After table 20 y is at 566
After table 21 y is at 518
After table 22 y is at 470
After table 23 y is at 422
After table 24 y is at 374
After table 25 y is at 326
After table 26 y is at 278
After table 27 y is at 230
After table 28 y is at 182
After table 29 y is at 134
After table 30 y is at 86
After table 31 y is at 38
After table 32 y is at 758
After table 33 y is at 710
After table 34 y is at 662
After table 35 y is at 614
After table 36 y is at 566
After table 37 y is at 518
After table 38 y is at 470
After table 39 y is at 422
After table 40 y is at 374
After table 41 y is at 326
After table 42 y is at 278
After table 43 y is at 230
After table 44 y is at 182
After table 45 y is at 134
After table 46 y is at 86
After table 47 y is at 38
After table 48 y is at 758
After table 49 y is at 710
After table 50 y is at 662
After table 51 y is at 614
After table 52 y is at 566
After table 53 y is at 518
After table 54 y is at 470
After table 55 y is at 422
After table 56 y is at 374
After table 57 y is at 326
After table 58 y is at 278
After table 59 y is at 230
After table 60 y is at 182
After table 61 y is at 134
After table 62 y is at 86
After table 63 y is at 38
After table 64 y is at 758
After table 65 y is at 710
After table 66 y is at 662
After table 67 y is at 614
After table 68 y is at 566
After table 69 y is at 518
After table 70 y is at 470
After table 71 y is at 422
After table 72 y is at 374
After table 73 y is at 326
After table 74 y is at 278
After table 75 y is at 230
After table 76 y is at 182
After table 77 y is at 134
After table 78 y is at 86
After table 79 y is at 38
After table 80 y is at 758
After table 81 y is at 710
After table 82 y is at 662
After table 83 y is at 614
After table 84 y is at 566
After table 85 y is at 518
After table 86 y is at 470
After table 87 y is at 422
After table 88 y is at 374
After table 89 y is at 326
After table 90 y is at 278
After table 91 y is at 230
After table 92 y is at 182
After table 93 y is at 134
After table 94 y is at 86
After table 95 y is at 38
After table 96 y is at 758
After table 97 y is at 710
After table 98 y is at 662
After table 99 y is at 614

因此,您在 cmets 中的声明

每次我计算“y”值时它都保持不变

每次我向文档中添加一个新表时,我都会进行该测试。每次我得到相同的价值。

无法用您的代码重现:y 显然一直在剧烈变化。

因此,如果您需要帮助,请务必提供示例代码,以便人们重现您的问题,而不是反驳您的主张。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-03
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    相关资源
    最近更新 更多