2010-02-23 51 views
5

我需要一個包含大約12個單元格的表格來顯示爲標題。以下代碼無法執行此操作。我知道table2沒有12個單元格。在第二頁上,僅顯示「測試」。我錯過了什麼?PdfPTable作爲iTextSharp中的頭文件

在此先感謝!

Document document = new Document(); 

     try 
     { 
      PdfWriter.GetInstance(document, new FileStream("TableTest.pdf", FileMode.Create)); 
      document.Open(); 

      PdfPTable table = new PdfPTable(1); 
      table.WidthPercentage = 100; 
      PdfPTable table2 = new PdfPTable(2); 

      //logo 
      PdfPCell cell2 = new PdfPCell(Image.GetInstance(@"C:\path\to\file.gif")); 
      cell2.Colspan = 2; 
      table2.AddCell(cell2); 

      //title 
      cell2 = new PdfPCell(new Phrase("\nTITLE TEXT", new Font(Font.HELVETICA, 16, Font.BOLD | Font.UNDERLINE))); 
      cell2.HorizontalAlignment = Element.ALIGN_CENTER; 
      cell2.Colspan = 2; 
      table2.AddCell(cell2); 

      PdfPCell cell = new PdfPCell(table2); 
      table.HeaderRows = 1; 
      table.AddCell(cell); 
      table.AddCell(new PdfPCell(new Phrase(""))); 

      document.Add(table); 

      document.Add(new Phrase("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\ntesting")); 
     } 
     catch (DocumentException de) 
     { 
      Console.Error.WriteLine(de.Message); 
     } 
     catch (IOException ioe) 
     { 
      Console.Error.WriteLine(ioe.Message); 
     } 

     document.Close(); 

回答

11

賓果! PdfPageEventHandler類爲我工作。

我用PdfPageEventHelper重寫的OnEndPage方法:

class _events : PdfPageEventHelper 
{ 
    public override void OnEndPage(PdfWriter writer, Document document) 
    { 
     PdfPTable table = new PdfPTable(1); 
     //table.WidthPercentage = 100; //PdfPTable.writeselectedrows below didn't like this 
     table.TotalWidth = document.PageSize.Width - document.LeftMargin - document.RightMargin; //this centers [table] 
     PdfPTable table2 = new PdfPTable(2); 

     //logo 
     PdfPCell cell2 = new PdfPCell(Image.GetInstance(@"C:\path\to\file.gif")); 
     cell2.Colspan = 2; 
     table2.AddCell(cell2); 

     //title 
     cell2 = new PdfPCell(new Phrase("\nTITLE", new Font(Font.HELVETICA, 16, Font.BOLD | Font.UNDERLINE))); 
     cell2.HorizontalAlignment = Element.ALIGN_CENTER; 
     cell2.Colspan = 2; 
     table2.AddCell(cell2); 

     PdfPCell cell = new PdfPCell(table2); 
     table.AddCell(cell); 

     table.WriteSelectedRows(0, -1, document.LeftMargin, document.PageSize.Height - 36, writer.DirectContent); 
    } 
} 

然後,建立PDF

Document document = new Document(PageSize.A4, 36, 36, 36 + <height of table>, 36); // note height should be set here 
_events e = new _events(); 
PdfWriter pw = PdfWriter.GetInstance(document, new FileStream("TableTest.pdf", FileMode.Create)); 
pw.PageEvent = e; 
document.Open(); 

for(int i=0;i<100;i++) 
{ 
    document.Add(new Phrase("TESTING\n")); 
} 

document.Close(); 
0

此,如果你有一個以上的頁面將無法正常工作。如果您有多個頁面,則需要使用OnStartPage事件而不是onEndPage事件。

1

我用一個非常簡單的方法在每個頁面上添加一個標題行,當創建一個帶'n'行的PdfPTable時,其中'n'可以是任何整數。我正在使用iTextSharp 4.0.4.0。

因此,您可以創建一個1000行或2行或5000行的表格,但iTextSharp會自動爲您分頁輸出。 iTextSharp不會做的是自動添加標題到每個頁面的開頭。

爲了添加標題行的每一頁,我所做的就是與所有行和單元格創建PdfPTable後添加一行代碼,然後iTextSharp的自動分頁與標題行輸出。 'table'是我在設置HeaderRows屬性之前在代碼中創建的PdfPTable的實例。

table.HeaderRows = 1; //this will automatically insert a header row 
         //at start of every page. The first row you created 
         //in your code will be used as the header row in this case. 
相關問題