【发布时间】:2012-01-14 02:09:35
【问题描述】:
我有三个静态 PdfPTable 方法,它们从用于创建 PDF 文档的 main 方法调用。
在每种方法中,我使用 PdfPCells' 将数据和结构添加到表中,因此第一个 PdfPTable 方法是为每个页面创建标题,第二个是创建该页面的正文,第三个是创建每页的页脚。然后在 main 方法中调用表格后将表格添加到 PdfPDocument
我尝试使用 table.HeaderRows = 1 为每个页面添加标题,但是当添加到标题的 PdfPTable 方法时,它会删除该表中的所有内容。当我将它添加到正文的 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;
}
【问题讨论】:
标签: c# pdf itextsharp