Add Footer to a pdf using iTextSharp C#

Malam Malam 121 Reputation points
2022-02-23T16:00:33.497+00:00

The code below saves a 1 page pdf
How do I add a footer to it?

table.AddCell(PhraseCell(new Phrase("Name:", FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.BOLD, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
 phrase = new Phrase(new Chunk(sName + "\n\n", FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)));
 table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT));
 cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
 cell.Colspan = 2;
 cell.PaddingBottom = 10f;
 table.AddCell(cell);

 document.Add(table);
 document.Close();

byte[] bytes = memoryStream.ToArray();
string uncPath1 = @"\\MyServer\MyFolder$\temp\";
File.WriteAllBytes(SharedPath + "employee.pdf", bytes);
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,288 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Malam Malam 121 Reputation points
    2022-02-23T16:33:56.7+00:00

    Never mind!
    I've fixed it using the code below:

    PdfPTable tbfooter = new PdfPTable(3);
                    tbfooter.TotalWidth = document.PageSize.Width - document.LeftMargin - document.RightMargin;
                    tbfooter.DefaultCell.Border = 0;
                    tbfooter.AddCell(new Paragraph());
                    tbfooter.AddCell(new Paragraph());
                    var _cell2 = new PdfPCell(new Paragraph(new Chunk("This is my Footer...", FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.BLUE))));
                    _cell2.HorizontalAlignment = Element.ALIGN_RIGHT;
                    _cell2.Border = 0;
                    tbfooter.AddCell(_cell2);
                    tbfooter.AddCell(new Paragraph());
                    tbfooter.AddCell(new Paragraph());
                    var _celly = new PdfPCell(new Paragraph(writer.PageNumber.ToString()));//For page no.
                    _celly.HorizontalAlignment = Element.ALIGN_RIGHT;
                    _celly.Border = 0;
                    tbfooter.AddCell(_celly);
                    float[] widths1 = new float[] { 20f, 20f, 60f };
                    tbfooter.SetWidths(widths1);
                    tbfooter.WriteSelectedRows(0, -1, document.LeftMargin, writer.PageSize.GetBottom(document.BottomMargin), writer.DirectContent);
    
    0 comments No comments