我使用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);
}
如果有人知道怎么做,请帮助我。
答案 0 :(得分: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
因此,您在评论中声称
每当我计算“y”值时,它都保持不变
或
每次我向文档中添加一个新表时,我都会用它来进行测试。每当我获得相同的价值时。
无法使用您的代码进行复制: y 显然正在不断变化。
因此,如果您需要帮助,请提供示例代码,以便人们重现您的问题,而不是反驳您的声明。