Coreman
Coreman

Reputation: 39

Adding a Table into the Footer of a PDF

After creating a PDF from HTML, I need to add a Footer to each page. The Footer is going to be a single-row, three-column table, with the left cell being an external reference ID, the center being a "Page X of Y", and the right being a date stamp. I have no experience with iTextSharp, but after reading various posts I created the following 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

After editing my PageEvent, I'm still having issues. It's come to mind that I'm probably having issues with calling the PageEvent and adding it to the PDF (no experience with iTextSharp). Below is my attempt to add the Footer to an existing PDF that has been passed as byte[].

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;

Any help is appreciated. Thanks in advance.

Upvotes: 0

Views: 4504

Answers (2)

Muhammad Muneer
Muhammad Muneer

Reputation: 51

Try this, its working for me:

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

Check this post

Header, footer and large tables with iTextSharp

Upvotes: 2

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

You wrote:

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

However, the method is:

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

Which means that you're asking to write a selection of rows starting with row 0 and ending with row 0, or: you're asking to write not a single row.

Moreover, you provide an x value instead of a y value. Change that line into:

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

Upvotes: 0

Related Questions