2013-05-09 59 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