gpt4 book ai didi

c# - itextsharp pdfpcell header

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:58:37 35 4
gpt4 key购买 nike

我想要一个 PdfPCell 类的单元格表,每个单元格都有一个小标题、主字符串和小页脚。我找不到插入它们的方法,因为不允许将 HeaderandFooter 元素添加到单元格,一个段落覆盖另一个段落等等。有什么想法吗?

提前致谢

最佳答案

您可以使用嵌套表格。
插入带有小页眉和小页脚的 1x1 表格,而不是 PdfPCell。

编辑:

让我们忘掉 iTextSharp 的表格页脚和页眉功能,因为当表格跨越多个页面并且您有重复的页脚和页眉时,它很有用。在我们的例子中,页眉和页脚将属于仅包含 3 个单元格的内部表格,因此让我们对所有这些单元格使用 PdfPCell。

然后主要函数是 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();
}

关于c# - itextsharp pdfpcell header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1491577/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com