首页 文章

将表添加到PDF的页脚中

提问于
浏览
0

从HTML创建PDF后,我需要为每个页面添加一个页脚 . 页脚将是一个单行,三列表,左侧单元格是外部参考ID,中间是“Y页面X”,右侧是日期戳 . 我没有使用iTextSharp的经验,但在阅读了各种帖子后,我创建了以下PageEventHandler

UPDATED CODE

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);
    }
}

ADDED RESPONSE

编辑我的PageEvent后,我仍然遇到问题 . 我想到我可能在调用PageEvent并将其添加到PDF时遇到问题(没有使用iTextSharp的经验) . 下面是我尝试将Footer添加到已作为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;

任何帮助表示赞赏 . 提前致谢 .

2 回答

  • 1

    试试这个,它为我工作:

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

    查看这篇文章

    Header, footer and large tables with iTextSharp

  • 0

    你写了:

    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);
    

相关问题