Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating a pdf file using itextsharp. I want to add footer for each page in the pdf document Which Display Footer Page Number Like "Page 1 of 3". Can anyone tell me how can I do this?
Posted

1 solution

Have a look at the following blog: How to add header and footer on pdf file using iTextSharp 5.1[^]

It says:
1. Create a class that in inherited by PdfPageEventHelper
2. Create table in this class and write footer content.
C#
public partial class Footer : PdfPageEventHelper
{
public override void OnEndPage(PdfWriter writer, Document doc)
{
Paragraph footer= new Paragraph("THANK YOU", FontFactory.GetFont(FontFactory.TIMES, 10, iTextSharp.text.Font.NORMAL));
footer.Alignment = Element.ALIGN_RIGHT;
PdfPTable footerTbl = new PdfPTable(1);
footerTbl.TotalWidth = 300;
footerTbl.HorizontalAlignment = Element.ALIGN_CENTER;
PdfPCell cell = new PdfPCell(footer);
cell.Border = 0;
cell.PaddingLeft = 10;
footerTbl.AddCell(cell);
footerTbl.WriteSelectedRows(0, -1, 415, 30, writer.DirectContent);
}
}

3. After this
C#
Document document = new Document(PageSize.A4, 50, 50, 25, 25);
var output = new FileStream(Server.MapPath("Demo.pdf"), FileMode.Create);
PdfWriter writer = PdfWriter.GetInstance(document, output);
// Open the Document for writing
document.Open();
//using footer class
writer.PageEvent = new Footer();.
Paragraph welcomeParagraph = new Paragraph("Hello, World!");
document.Add(welcomeParagraph);
document.Close();
 
Share this answer
 
Comments
bhavesh002 1-Feb-13 2:51am    
Thanks For Giving Useful Solution i try to apply this solution and apply successfully and get footer but i wan to a page number in place of word "ThankYou" i need to Paging Like "Page 1 of 1" same as in word document footerpager control so any modification at that place to get paging like this ???
Member 13346755 19-Nov-21 4:34am    
Thank you. Your sharing helped me a lot. To replace "ThankYou" with the page number, I just had to replace "ThankYou" with "writer.PageNumber.ToString()" in the OnEndPage event

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900