首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用itextsharp添加页脚(带有变量/类)

如何使用itextsharp添加页脚(带有变量/类)
EN

Stack Overflow用户
提问于 2020-11-13 10:40:22
回答 1查看 124关注 0票数 0

我有这段代码,我想添加一个表作为一个页脚。但问题是我不能传递变量:

代码语言:javascript
复制
Document doc = new Document();
doc.AddTitle("e-8D Report_" + report.OSIE8DNO + ".pdf");
PdfWriter writer = PdfWriter.GetInstance(doc, fs);

doc.SetPageSize(PageSize.A4);
doc.SetMargins(18f, 18f, 18f, 18f); // 0.25 inch margins

doc.Open();

PdfPTable table = new PdfPTable(10)
{
       WidthPercentage = 100,
       LockedWidth = true,
       TotalWidth = 560f
};
float[] widths = new float[] { 32f, 73f, 70f, 70f, 70f, 70f, 70f, 70f, 73f, 32f };
table.SetWidths(widths);

// rest of the document
// ...
// rest of the document

// Below is the part that I want to add as footer

#region Footer
// left margin
cell = new PdfPCell(new Phrase(" ", font6))
{
      Border = Rectangle.NO_BORDER
};
table.AddCell(cell);

cell = new PdfPCell(new Phrase("Reviewed & Approved by :", font6))
{
      Colspan = 4,
      BackgroundColor = bgBlue,
      HorizontalAlignment = Element.ALIGN_LEFT,
      Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER
};
table.AddCell(cell);

cell = new PdfPCell(new Phrase("For document approval by email, no Manual / e-Signature required &", font2))
{
       Colspan = 4,
       BackgroundColor = bgBlue,
       HorizontalAlignment = Element.ALIGN_LEFT,
       Border = Rectangle.TOP_BORDER | Rectangle.RIGHT_BORDER
};
table.AddCell(cell);

// right margin
cell = new PdfPCell(new Phrase(" ", font6))
{
       Border = Rectangle.NO_BORDER
};
table.AddCell(cell);

// the rest of the cell
//...
// the rest of the cell

#endregion

doc.Add(table);
//writer.PageEvent = new PDFFooter();
doc.Close();

使用这些变量,我无法使用PdfPageEventHelper从以下源代码生成页脚:Footer in pdf with iTextSharp

请帮帮忙

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-16 03:00:08

已经知道答案了。我将参数添加到PdfPageEventHelper类中:

代码语言:javascript
复制
public PdfPTable footer { get; set; }

public override void OnEndPage(PdfWriter writer, Document document)
{
     base.OnEndPage(writer, document);
     document.Add(footer);
}

并从页面调用(首先在页面上创建完整的表):

代码语言:javascript
复制
writer.PageEvent = new PDFHeaderFooter() { footer = table };
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64819453

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档