2013-05-09 57 views
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 
+0

是否重叠表或头?您正在使用像'PdfContentByte.BeginText()'这样的原始PDF命令混合iTextSharp的抽象形式,例如'Document.Add()'和'PdfPTables',这很容易中断。抽象化可以自由流畅地处理所有事情,但在您编写手动PDF命令的那一刻,所有投注都将关闭。 – 2013-05-09 21:53:04

+0

克里斯哈斯是正确的。另外:如果你想要一个很好的答案,你必须澄清。最终结果应该是什么样子?现在,你的问题看起来像你没有阅读太多的文档。 – 2013-05-10 06:35:09

+0

我的歉意。我拥有的是我创建的大量表格。我不希望一个表中的数据从它后面的表中运行到数据中。每次新页面的顶部都会有一个新表格。实际上,这样做是正确的,我只是不知道是否有更好的方法来创建分离。克里斯你是对的。感谢您的建议。我通常不会发布到论坛,但是当我这样做时,我更喜欢使用stackoverflow。现在解决这个问题是因为它实际上可以这样做。表 - PageBreak - 表 - PageBreak等 – 2013-05-10 16:14:58

回答

1

只是删除contentbyte并在文件中增加了一个新段落。很简单,这就解决了我需要实现的功能:带有标题标题的表格,从新页面开始的新表格。

 doc.Open(); 

     doc.Add(new Paragraph(new Chunk("Header for Searches" + Chunk.NEWLINE + Chunk.NEWLINE, fb))); 

     PdfPTable tableSearches = new PdfPTable(3); 
     PdfPCell cellSearches = new PdfPCell(); 

     cellSearches.BackgroundColor = BaseColor.WHITE; 

     cellSearches.Phrase = new Phrase("Facility ID"); 
     tableSearches.AddCell(cellSearches); 
     cellSearches.Phrase = new Phrase("Facility"); 
     tableSearches.AddCell(cellSearches); 
     cellSearches.Phrase = new Phrase("Phone Number"); 
     tableSearches.AddCell(cellSearches); 

     //loop through the records in facilities collection and add row 
     foreach (var m in facilityList) 
     { 
      cellSearches.BackgroundColor = BaseColor.LIGHT_GRAY; 

      cellSearches.Phrase = new Phrase(m.Id.ToString()); 
      tableSearches.AddCell(cellSearches); 

      cellSearches.Phrase = new Phrase(m.Facility); 
      tableSearches.AddCell(cellSearches); 

      cellSearches.Phrase = new Phrase(m.Phone); 
      tableSearches.AddCell(cellSearches); 
     } 

     doc.Add(tableSearches); 

     //Page 2? AM Searches 

     doc.NewPage(); 

     doc.Add(new Paragraph(new Chunk("Header for AM Searches" + Chunk.NEWLINE + Chunk.NEWLINE, fb))); 

     PdfPTable tableAM = new PdfPTable(3); 
     PdfPCell cellAM = new PdfPCell(); 

     cellAM.BackgroundColor = BaseColor.WHITE; 

     cellAM.Phrase = new Phrase("Facility ID"); 
     tableAM.AddCell(cellAM); 
     cellAM.Phrase = new Phrase("Facility"); 
     tableAM.AddCell(cellAM); 
     cellAM.Phrase = new Phrase("Phone Number"); 
     tableAM.AddCell(cellAM); 

     //loop through the records and add row 
     foreach (var m in facilityList) 
     { 
      cellAM.BackgroundColor = BaseColor.CYAN; 
      cellAM.Phrase = new Phrase(m.Id.ToString()); 
      tableAM.AddCell(cellAM); 

      cellAM.Phrase = new Phrase(m.Facility); 
      tableAM.AddCell(cellAM); 

      cellAM.Phrase = new Phrase(m.Phone); 
      tableAM.AddCell(cellAM); 
     } 

     doc.Add(tableAM); 

     doc.Close(); 

enter image description here