2012-01-12 35 views
0

我有三個從用於創建PDF文檔的主要方法中調用的靜態PdfPTable方法。在itextsharp中創建一個包含多次調用表的表頭的問題

在每種方法中,我使用PdfPCells'將數據和結構添加到表中,因此第一個PdfPTable方法將爲每個頁面創建標題,第二個創建該頁面的主體,第三個是爲每個頁面創建頁腳。然後表被添加到PdfPDocument它們在主方法中調用

我試圖使用table.HeaderRows =桌子上的一切。當我將它添加到主體的PdfPTable時,它將第二頁上的內容移動到第一頁的底部,並將第一頁的內容複製到第二頁。

//table method for call header 
PdfPTable table = CreateTable(textUpperData/*, document, writer*/); 
document.Add(table); 

//table method call for body 
table = CreateTable1(imgInfoData, posData, sizeData, document); 
document.Add(table); 

//table method call for footer 
table = CreateTable2(textLowerData); 
document.Add(table); 

document.Close(); 



//header table static method     
    private static PdfPTable CreateTable(List<KeyValuePair<string, string>> textUpper/*, Document document, PdfWriter writer*/) 
    { 
     //SiteDB sitedb = new SiteDB(); 
     //sitedb.GetEmailText(); 

     iTextSharp.text.Font headerFont = FontFactory.GetFont("Time New Roman", 14, iTextSharp.text.Font.BOLD, BaseColor.BLACK); 
     iTextSharp.text.Font bodyFont = FontFactory.GetFont("Time New Roman", 10, iTextSharp.text.Font.NORMAL, BaseColor.BLACK); 

     PdfPTable table = new PdfPTable(3); 
     table.HorizontalAlignment = Element.ALIGN_CENTER; 
     table.WidthPercentage = 100; 
     table.TotalWidth = 597.6f; 
     table.LockedWidth = true; 
     table.SetWidths(new float[] { 1, 2, 1 }); 

     iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance("C:\\Users\\User\\Documents\\Images\\image0.tiff"); 
     logo.ScaleToFit(150, 235); 
     logo.SetAbsolutePosition(595.2f - 150f, 0); 


     PdfPCell logoCell = new PdfPCell(logo); 
     logoCell.HorizontalAlignment = Element.ALIGN_JUSTIFIED; 
     logoCell.Border = iTextSharp.text.Rectangle.NO_BORDER; 
     table.AddCell(logoCell); 

     Chunk logoChunk = new Chunk(System.Environment.NewLine); 
     Phrase logoPhrase = new Phrase(logoChunk); 

     //Chunk headerChunk = new Chunk(sitedb.header_lit.ToString() + System.Environment.NewLine, headerFont); 
     //Phrase headerPhrase = new Phrase(headerChunk); 
     Chunk headerChunk = new Chunk(textUpper[0].Key.ToString() + System.Environment.NewLine, headerFont); 
     Phrase headerPhrase = new Phrase(headerChunk); 

     //Chunk bodyChunk = new Chunk(sitedb.body_lit.ToString() + System.Environment.NewLine, bodyFont); 
     //Phrase bodyPhrase = new Phrase(bodyChunk); 
     Chunk bodyChunk = new Chunk(textUpper[0].Value.ToString() + System.Environment.NewLine, bodyFont); 
     Phrase bodyPhrase = new Phrase(bodyChunk); 


     Paragraph paragraph = new Paragraph(); 
     paragraph.Alignment = Element.ALIGN_MIDDLE; 
     paragraph.Alignment = Element.ALIGN_TOP; 
     paragraph.Add(headerPhrase); 
     paragraph.Add(bodyPhrase); 


     PdfPCell headerBodyCell = new PdfPCell(paragraph); 
     headerBodyCell.HorizontalAlignment = Element.ALIGN_JUSTIFIED; 
     headerBodyCell.VerticalAlignment = Element.ALIGN_TOP; 
     headerBodyCell.Colspan = 2; 
     headerBodyCell.PaddingLeft = 5f; 
     headerBodyCell.PaddingRight = 5f; 
     headerBodyCell.PaddingTop = 8f; 
     headerBodyCell.Border = iTextSharp.text.Rectangle.NO_BORDER; 
     table.AddCell(headerBodyCell); 

     return table; 
    } 

回答

0

那麼似乎頭不能被添加到每個新頁面在從多個表,只有單個表與電池內置的PDF!

我很可能會創建一個方法,爲每個新頁面添加一個特定的表格!

+0

你試圖顯示什麼類型的頭文件:[1] ** page **;通用於文檔中的每個頁面,或[2] ** table **;表格是唯一的,當表格內容跨越多頁時顯示。 – kuujinbo 2012-01-14 15:32:49

+0

我試圖顯示頁眉,我想出了上面的帖子有詳細信息 – DaNet 2012-01-16 14:29:17

+1

你應該使用[PdfPageEventHelper](http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfPageEventHelper.html )。這裏有一些[示例代碼](http://kuujinbo.info/cs/itext_img_hdr.aspx)。 – kuujinbo 2012-01-16 17:14:52

相關問題