我有三个静态PdfPTable方法,这些方法是从用于创建PDF文档的main方法调用的。
在每个方法中,我使用PdfPCells'向表中添加数据和结构,因此第一个PdfPTable方法是为每个页面创建标题,第二个是创建该页面的主体,第三个是创建每页的页脚。然后在主方法
中调用它们之后将该表添加到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;
}
答案 0 :(得分:0)
似乎无法将标题添加到pdf中的每个新页面,这是从多个表构建的,只有单个表格包含单元格!
我很可能会创建一个方法,为每个新页面添加一个特定的表!