2012-01-12 9 views
0

私は、PDFドキュメントの作成に使用されるmainメソッドから呼び出される3つの静的なPdfPTableメソッドを持っています。テーブルへの多重呼び出しを使用してitextsharpにヘッダを作成する際の問題

私はPdfPCellsを使ってデータと構造をテーブルに追加するので、最初のPdfPTableメソッドは、各ページのヘッダーを作成し、2番目はそのページの本文を作成し、3番目はすべてのページのフッターを作成します。次に、メインメソッドで呼び出された後にテーブルが追加されます。

table.HeaderRows = 1を使用してすべてのページにヘッダーを追加しようとしましたが、ヘッダーのPdfPTableメソッドに追加すると、そのテーブルのすべて本文のPdfPTableに追加すると、2ページ目のコンテンツが最初のページの最後に移動し、最初のページのコンテンツが2ページ目にコピーされます。

//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] **ページ**;文書内のすべてのページに共通する[2] **表**;テーブルの内容が複数のページにまたがっている場合に表示されます。 – kuujinbo

+0

私はページヘッダーを表示しようとしていますが、上記の投稿には詳細があります。 – DaNet

+1

[PdfPageEventHelper](http://api.itextpdf.com/txt/pdf/PdfPageEventHelper.html)を使用してください。 )ここにいくつかの[サンプルコード](http://kuujinbo.info/cs/itext_img_hdr.aspx)があります。 – kuujinbo

関連する問題