first we create a class that in inherited by PdfPageEventHelper. and i create table in this class and write footer content.
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);
}
}
after this
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();
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);
}
}
after this
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();
Good Work... Working Fine... Thnx :)
ReplyDeletefor Header The Code will be same but Position will be:
"headerTbl.WriteSelectedRows(0, -1, 0, (doc.PageSize.Height - 10), writer.DirectContent);"
it's print html tag on pdf.
ReplyDeleteThanks Gopal. It is really helpful. I was searching on web for so long for this particular issue.
ReplyDeleteThanks a lot! There is not a lot of documentation on iTextSharp. This really helped! Thanks!
ReplyDeleteI am using this code i am getting last page printing footer value i need every page please give me solution..
ReplyDeleteput the "writer.PageEvent = new Footer();" before the "document.open()"
DeleteHi Gopal,
ReplyDeleteI am trying create pdf using html template,Please can you share me the code.
Hi Gopal
ReplyDeletei am trying code but it add footer only in first page!
can help me to add footer on all pdf pages that created by itextsharp \c#
thanks
put the "writer.PageEvent = new Footer();" before the "document.open()"
DeleteThis comment has been removed by the author.
ReplyDeleteI have used the same code in my application. But the footer is coming only in the last page. How to solve this issue
ReplyDeleteput the "writer.PageEvent = new Footer();" before the "document.open()"
Delete