将表格添加到PDF的页脚中

本文关键字:PDF 表格 添加 | 更新日期: 2023-09-27 17:57:55

从HTML创建PDF后,我需要在每个页面上添加一个页脚。页脚将是一个单行、三列的表格,左边的单元格是外部引用ID,中间是"第X页,共Y页",右边是日期戳。我没有使用iTextSharp的经验,但在阅读了各种帖子后,我创建了以下PageEventHandler

更新代码

public class FooterEvent : PdfPageEventHelper
{
    PdfContentByte cb;
    #region Properties
    private string _FooterLeft;
    public string FooterLeft
    {
        get { return _FooterLeft; }
        set { _FooterLeft = value; }
    }
    private string _FooterCenter;
    public string FooterCenter
    {
        get { return _FooterCenter; }
        set { _FooterCenter = value; }
    }
    private string _FooterRight;
    public string FooterRight
    {
        get { return _FooterRight; }
        set { _FooterRight = value; }
    }
    private Font _FooterFont;
    public Font FooterFont
    {
        get { return _FooterFont; }
        set { _FooterFont = value; }
    }
    #endregion
    public override void OnEndPage(PdfWriter writer, Document document)
    {
        base.OnEndPage(writer, document);
        Font font = new Font(Font.FontFamily.HELVETICA, 12, Font.NORMAL, BaseColor.BLACK);
        PdfPTable FooterTable = new PdfPTable(3);
        FooterTable.TotalWidth = document.PageSize.Width - document.LeftMargin - document.RightMargin;
        PdfPCell FooterLeftCell = new PdfPCell(new Phrase(2, FooterLeft, FooterFont));
        FooterLeftCell.HorizontalAlignment = Element.ALIGN_LEFT;
        FooterLeftCell.VerticalAlignment = Element.ALIGN_CENTER;
        FooterLeftCell.Border = 0;
        FooterTable.AddCell(FooterLeftCell);
        PdfPCell FooterCenterCell = new PdfPCell(new Phrase(2, FooterCenter, FooterFont));
        FooterCenterCell.HorizontalAlignment = Element.ALIGN_CENTER;
        FooterCenterCell.VerticalAlignment = Element.ALIGN_CENTER;
        FooterCenterCell.Border = 0;
        FooterTable.AddCell(FooterCenterCell);
        PdfPCell FooterRightCell = new PdfPCell(new Phrase(2, FooterRight, FooterFont));
        FooterRightCell.HorizontalAlignment = Element.ALIGN_RIGHT;
        FooterRightCell.VerticalAlignment = Element.ALIGN_CENTER;
        FooterRightCell.Border = 0;
        FooterTable.AddCell(FooterRightCell);
        FooterTable.WriteSelectedRows(0, -1, document.LeftMargin, document.BottomMargin, cb);
    }
}

添加响应

编辑我的PageEvent后,我仍然有问题。我突然想到,我可能在调用PageEvent并将其添加到PDF中时遇到了问题(没有使用iTextSharp的经验)。以下是我尝试将页脚添加到已作为byte[]传递的现有PDF中。

byte[] output = null;
string identifier = id;
string time = DateTime.Now.ToString();
string page = null;
PdfReader reader = new PdfReader(original);
int n = reader.NumberOfPages;
try
{
   using (MemoryStream ms = new MemoryStream())
   {
      using (Document doc = new Document(PageSize.LETTER, 100, 100, 100, 100))
      {
         using (PdfWriter writer = PdfWriter.GetInstance(doc, ms))
         {
            FooterEvent footer = new FooterEvent();
            writer.PageEvent = footer;
            footer.FooterFont = FontFactory.GetFont(BaseFont.HELVETICA, 12, BaseColor.BLACK);
            doc.Open();
            for (int i = 1; i < n + 1; ++i)
            {
               doc.NewPage();
               page = "Page " + i + " of " + n;
               footer.FooterLeft = identifier;
               footer.FooterCenter = page;
               footer.FooterRight = time;
               doc.Add(new Paragraph(reader.GetPageContent(i).ToString()));
               //Probably wrong. Trying to add contents from each page in original PDF
            }
            doc.Close();
         }
      }
      output = ms.ToArray();
   }
}
catch (Exception ex)
{
   //Some Message added later
}
return output;

感谢您的帮助。提前谢谢。

将表格添加到PDF的页脚中

试试这个,它对我有用:

FooterTable.WriteSelectedRows(0, -1, document.LeftMargin, FooterTable.TotalHeight, cb);

检查此张贴

带有iTextSharp 的页眉、页脚和大表格

您写道:

FooterTable.WriteSelectedRows(0, 0, document.LeftMargin, document.RightMargin, cb);

然而,方法是:

FooterTable.WriteSelectedRows(rowStart, rowEnd, x, y, cb);

这意味着你要求写一组从第0行开始到第0行结束的行,或者:你要求不写一行。

此外,您提供的是x值,而不是y值。将该行更改为:

FooterTable.WriteSelectedRows(0, -1, document.LeftMargin, document.BottomMargin, cb);