How can I add header and footer for each page in the pdf.
Headed will contain just a text Footer will contain a text and pagination for pdf (Page : 1 of 4)
H
For iTextSharp 4.1.6, the last version of iTextSharp that was licensed as LGPL, the solution provided by Alvaro Patiño is both simple and effective.
Because documentation is somewhat scarse I'd like to extend his answer with this code snippet that can be used to change the appearance of the header and footer. By default they have a rather large font-size and a thick border, which many people will want to change.
// Parameters passed on to the function that creates the PDF
String headerText = "Your header text";
String footerText = "Page";
// Define a font and font-size in points (plus f for float) and pick a color
// This one is for both header and footer but you can also create seperate ones
Font fontHeaderFooter = FontFactory.GetFont("arial", 8f);
fontHeaderFooter.Color = Color.GRAY;
// Apply the font to the headerText and create a Phrase with the result
Chunk chkHeader = new Chunk(headerText, fontHeaderFooter);
Phrase p1 = new Phrase(chkHeader);
// create a HeaderFooter element for the header using the Phrase
// The boolean turns numbering on or off
HeaderFooter header = new HeaderFooter(p1, false);
// Remove the border that is set by default
header.Border = Rectangle.NO_BORDER;
// Align the text: 0 is left, 1 center and 2 right.
header.Alignment = 1;
// add the header to the document
document.Header = header;
// The footer is created in an similar way
// If you want to use numbering like in this example, add a whitespace to the
// text because by default there's no space in between them
if (footerText.Substring(footerText.Length - 1) != " ") footerText += " ";
Chunk chkFooter = new Chunk(footerText, fontHeaderFooter);
Phrase p2 = new Phrase(chkFooter);
// Turn on numbering by setting the boolean to true
HeaderFooter footer = new HeaderFooter(p2, true);
footer.Border = Rectangle.NO_BORDER;
footer.Alignment = 1;
document.Footer = footer;
// Open the Document for writing and continue creating its content
document.Open();
For more info check Creating PDFs with iTextSharp and iTextSharp - Adding Text with Chunks, Phrases and Paragraphs. The source code on GitHub may also be useful.
Just add this line before opening the document (must be before):
document.Header = new HeaderFooter(new Phrase("Header Text"), false);
document.Open();
Easy codes that work successfully:
protected void Page_Load(object sender, EventArgs e)
{
.
.
using (MemoryStream ms = new MemoryStream())
{
.
.
iTextSharp.text.Document doc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 36, 36, 54, 54);
iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(doc, ms);
writer.PageEvent = new HeaderFooter();
doc.Open();
.
.
// make your document content..
.
.
doc.Close();
writer.Close();
// output
Response.ContentType = "application/pdf;";
Response.AddHeader("Content-Disposition", "attachment; filename=clientfilename.pdf");
byte[] pdf = ms.ToArray();
Response.OutputStream.Write(pdf, 0, pdf.Length);
}
.
.
.
}
class HeaderFooter : PdfPageEventHelper
{
public override void OnEndPage(PdfWriter writer, Document document)
{
// Make your table header using PdfPTable and name that tblHeader
.
.
tblHeader.WriteSelectedRows(0, -1, page.Left + document.LeftMargin, page.Top, writer.DirectContent);
.
.
// Make your table footer using PdfPTable and name that tblFooter
.
.
tblFooter.WriteSelectedRows(0, -1, page.Left + document.LeftMargin, writer.PageSize.GetBottom(document.BottomMargin), writer.DirectContent);
}
}