question

MalamMalam-4042 avatar image
0 Votes"
MalamMalam-4042 asked MalamMalam-4042 answered

Add Footer to a pdf using iTextSharp C#

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);
dotnet-aspnet-webformsdotnet-aspnet-webpages
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

MalamMalam-4042 avatar image
0 Votes"
MalamMalam-4042 answered

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);
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.