Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>That code was <a href="http://itext-general.2136553.n4.nabble.com/HeaderFooter-class-td3027962.html" rel="nofollow">deprecated and removed many years ago</a> but still lives on in comments in the source code unfortunately.</p> <p>What you want to do is subclass the <code>iTextSharp.text.pdf.PdfPageEventHelper</code> class and handle the <code>OnEndPage</code> method which will get called once for every page in your document:</p> <pre><code>public class MyPageEventHandler : iTextSharp.text.pdf.PdfPageEventHelper { public override void OnEndPage(PdfWriter writer, Document document) { //Create a simple ColumnText object var CT = new ColumnText(writer.DirectContent); //Bind it to the top of the document but take up the entire page width CT.SetSimpleColumn(0, document.PageSize.Height - 20, document.PageSize.Width, document.PageSize.Height); //Add some text CT.AddText(new Phrase("This is a test")); //Draw our ColumnText object CT.Go(); } } </code></pre> <p>To use this you just bind a new instance of it to your <code>PdfWriter</code>'s <code>PageEvent</code> property:</p> <pre><code>writer.PageEvent = new MyPageEventHandler(); </code></pre> <p>Below is a full working C# 2010 WinForms app targeting iTextSharp 5.1.2.0 that shows this:</p> <pre><code>using System; using System.IO; using System.Windows.Forms; using iTextSharp.text; using iTextSharp.text.pdf; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //Test file to create string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf"); //Standard PDF file stream creation using (FileStream output = new FileStream(outputFile, FileMode.Create,FileAccess.Write,FileShare.None)){ using (Document document = new Document(PageSize.LETTER)) { using (PdfWriter writer = PdfWriter.GetInstance(document, output)) { //Bind our custom event handler to the PdfWriter writer.PageEvent = new MyPageEventHandler(); //Open our PDF for writing document.Open(); //Add some text to page 1 document.Add(new Paragraph("This is page 1")); //Add a new page document.NewPage(); //Add some text to page 2 document.Add(new Paragraph("This is page 2")); //Close the PDF document.Close(); } } } this.Close(); } } public class MyPageEventHandler : iTextSharp.text.pdf.PdfPageEventHelper { public override void OnEndPage(PdfWriter writer, Document document) { //Create a simple ColumnText object var CT = new ColumnText(writer.DirectContent); //Bind it to the top of the document but take up the entire page width CT.SetSimpleColumn(0, document.PageSize.Height - 20, document.PageSize.Width, document.PageSize.Height); //Add some text CT.AddText(new Phrase("This is a test")); //Draw our ColumnText object CT.Go(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload