0
我正在使用iTextSharp创建PDF,并且正在创建多个在我的代码中以内联方式运行的表格。当我用我收藏的价值填充表格时,我不知道表格将会变多久。而且我不想让一张桌子进入或通过下一个桌子。这是我到目前为止,但如果表运行到下一个页面,它重叠,我摆在下一行代码有一个分页符-NewPage()表 -如何使用iTextsharp在PDF中添加表格而不知道表格将生成的确切页数
// Page 1 Searches
doc.NewPage();
cb.BeginText();
centerText(cb, "HeaderText for Searches", 300, 760, _fontbold, 18);
cb.EndText();
PdfPTable tableSearches = new PdfPTable(4);
PdfPCell cellSearches = new PdfPCell();
cellSearches.BackgroundColor = BaseColor.WHITE;
cellSearches.Phrase = new Phrase("Company");
tableSearches.AddCell(cellLKQ);
cellSearches.Phrase = new Phrase("Contact");
tableSearches.AddCell(cellLKQ);
cellSearches.Phrase = new Phrase("Phone Number");
tableSearches.AddCell(cellLKQ);
cellSearches.Phrase = new Phrase("Amount");
tableSearches.AddCell(cellLKQ);
//loop through the records in facilities collection and add row
foreach (var m in facilities)
{
cellSearches.BackgroundColor = BaseColor.LIGHT_GRAY;
cellSearches.Phrase = new Phrase(m.Facility);
tableSearches.AddCell(cellSearches);
cellSearches.Phrase = new Phrase(m.FacilityContact);
tableSearches.AddCell(cellSearches);
cellSearches.Phrase = new Phrase(m.Phone);
tableSearches.AddCell(cellSearches);
cellSearches.Phrase = new Phrase(m.SalvageQuote.ToString());
tableSearches.AddCell(cellSearches);
}
doc.Add(tableSearches);
//Page 2? AM Searches
doc.NewPage();
cb.BeginText();
centerText(cb, "HeaderText AM Searches", 300, 760, _fontbold, 18);
cb.EndText();
PdfPTable tableAM = new PdfPTable(4);
PdfPCell cellAM = new PdfPCell();
cellAM.BackgroundColor = BaseColor.WHITE;
cellAM.Phrase = new Phrase("Company");
tableAM.AddCell(cellAM);
cellAM.Phrase = new Phrase("Contact");
tableAM.AddCell(cellAM);
cellAM.Phrase = new Phrase("Phone Number");
tableAM.AddCell(cellAM);
cellAM.Phrase = new Phrase("Amount");
tableAM.AddCell(cellAM);
//loop through the records and add row
foreach (var m in amfacilities)
{
cellAM.BackgroundColor = BaseColor.CYAN;
cellAM.Phrase = new Phrase(m.Facility);
tableAM.AddCell(cellAM);
cellAM.Phrase = new Phrase(m.FacilityContact);
tableAM.AddCell(cellAM);
cellAM.Phrase = new Phrase(m.Phone);
tableAM.AddCell(cellAM);
cellLKQ.Phrase = new Phrase(m.SalvageQuote.ToString());
tableAM.AddCell(cellAM);
}
doc.Add(tableAM);
//Page 3? Another Table
doc.NewPage();
// Code for next table
是否重叠表或头?您正在使用像'PdfContentByte.BeginText()'这样的原始PDF命令混合iTextSharp的抽象形式,例如'Document.Add()'和'PdfPTables',这很容易中断。抽象化可以自由流畅地处理所有事情,但在您编写手动PDF命令的那一刻,所有投注都将关闭。 – 2013-05-09 21:53:04
克里斯哈斯是正确的。另外:如果你想要一个很好的答案,你必须澄清。最终结果应该是什么样子?现在,你的问题看起来像你没有阅读太多的文档。 – 2013-05-10 06:35:09
我的歉意。我拥有的是我创建的大量表格。我不希望一个表中的数据从它后面的表中运行到数据中。每次新页面的顶部都会有一个新表格。实际上,这样做是正确的,我只是不知道是否有更好的方法来创建分离。克里斯你是对的。感谢您的建议。我通常不会发布到论坛,但是当我这样做时,我更喜欢使用stackoverflow。现在解决这个问题是因为它实际上可以这样做。表 - PageBreak - 表 - PageBreak等 – 2013-05-10 16:14:58