[英]itextsharp pdfpcell header
我想要一個PdfPCell類的單元格表,每個單元都有一個小標題,主字符串和小頁腳。 我找不到插入它們的方法,因為HeaderandFooter不允許元素添加到單元格,一個段落覆蓋另一個,依此類推。 有任何想法嗎?
提前致謝
您可以使用嵌套表。
而不是PdfPCell,插入一個帶有小標題和一個小頁腳的1x1表。
編輯:
讓我們忘記iTextSharp的表格頁腳和標題功能,因為當表格跨越多個頁面然后重復頁腳和標題時它很有用。 在我們的例子中,頁眉和頁腳將屬於僅包含3個單元格的內部表,所以讓我們使用PdfPCell來表示所有這些單元格。
然后main函數是GetHFCell
,它將返回一個包含自定義標題單元格(高度,字體,文本,...)的PdfPTable,一個自定義頁腳單元格和一個包含主文本的中間單元格。 每當我們想要向主表添加單元格時,就會調用此函數(如何在GeneratePDF
使用此函數)。
private static PdfPTable GetHFCell(string header, string footer, string text)
{
PdfPTable pdft;
PdfPCell hc;
PdfPCell fc;
pdft = new PdfPTable(1);
pdft.WidthPercentage = 100f;
pdft.DefaultCell.Border = 0;
hc = new PdfPCell(new Phrase(header));
hc.Top = 0f;
hc.FixedHeight = 7f;
hc.HorizontalAlignment = 1;
hc.BackgroundColor = iTextSharp.text.Color.ORANGE;
((Chunk)(hc.Phrase[0])).Font = new iTextSharp.text.Font(((Chunk)(hc.Phrase[0])).Font.Family, 5f);
fc = new PdfPCell(new Phrase(footer));
hc.Top = 0f;
fc.FixedHeight = 7f;
hc.HorizontalAlignment = 1;
fc.BackgroundColor = iTextSharp.text.Color.YELLOW;
((Chunk)(fc.Phrase[0])).Font = new iTextSharp.text.Font(((Chunk)(fc.Phrase[0])).Font.Family, 5f);
pdft.AddCell(hc);
pdft.AddCell(text);
pdft.AddCell(fc);
return pdft;
}
public void GeneratePDF()
{
Document document = new Document();
try
{
PdfWriter.GetInstance(document, new FileStream("File1.pdf", FileMode.Create));
document.Open();
PdfPTable table = new PdfPTable(5);
table.DefaultCell.Padding = 0;
table.DefaultCell.BorderWidth = 2f;
for (int j = 1; j < 6; j++)
{
for (int i = 1; i < 6; i++)
{
//calling GetHFCell
table.AddCell(
GetHFCell("header " + ((int)(i + 5 * (j - 1))).ToString(),
"footer " + ((int)(i + 5 * (j - 1))).ToString(),
"z" + j.ToString() + i.ToString()));
}
}
document.Add(table);
}
catch (DocumentException de)
{
//...
}
catch (IOException ioe)
{
//...
}
document.Close();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.