0

I tried to add Header in all the pages of my PDF using iText package in .NET C#, but it's not working.

Note: I'm using iText version 7

I tried the following Stack Overflow answers, they demonstrated only for Footer (i.e., END_PAGE) not for START_PAGE

My Code:

class Program {
    static void Main(string[] args) {

        var writer = new PdfWriter("E:/pdfSample/bala.pdf");
        var pdf = new PdfDocument(writer);
        var document = new Document(pdf,PageSize.A4);
        document.SetMargins(120,40,100,40);

        // Create a PdfFont
        var font = PdfFontFactory.CreateFont(StandardFonts.TIMES_ROMAN);

        List list = new List();

        // Add ListItem objects
        list.Add(new ListItem("Never gonna give you up"))
            .Add(new ListItem("Never gonna let you down"))    
            .Add(new ListItem("Never gonna run around and desert you"))
            .Add(new ListItem("Never gonna make you cry"))
            .Add(new ListItem("Never gonna say goodbye"))
            .Add(new ListItem("Never gonna tell a lie and hurt you"));
        // Add the list
        document.Add(list);

        // Add a Paragraph
        document.Add(new Paragraph("iText is:").SetFont(font));


        document.Add(logo);
        var tree = new Image(ImageDataFactory.CreateJpeg(new Uri("E:/pdfSample/RelatednessTree.jpg")));
        // document.Add(tree);

        pdf.AddEventHandler(PdfDocumentEvent.START_PAGE,new Paragraph("HEADER TEXT"));
        pdf.AddEventHandler(PdfDocumentEvent.END_PAGE,new Paragraph("FOOTER TEXT"));

        document.Close();

    }
}


public class TableFooter : IEventHandler {
    private Paragraph para;

    public TableFooter(Paragraph paraObj) {
        para = paraObj;
    }

    public void HandleEvent(Event @event) {
        PdfDocumentEvent docEvent = @event as PdfDocumentEvent;
        PdfDocument doc = docEvent.GetDocument();
        PdfPage page = docEvent.GetPage();
        PdfCanvas canvas = new PdfCanvas(page.NewContentStreamBefore(),page.GetResources(),doc);

        new Canvas(canvas,doc, new Rectangle(20, -20, page.GetPageSize().GetWidth() - 80,80)).Add(para);
    }
}

Kindly assist me how to add Header (i.e., START_PAGE) and whats the configuration is required to display the Header in all the PDF pages.

mkl
  • 90,588
  • 15
  • 125
  • 265
Sreedharan
  • 83
  • 1
  • 3
  • 8
  • 1
    You're confused on what `START_PAGE` and `END_PAGE` represent. They do not stand for the location of the content, but the trigger for when the related `IEventHandler` should be fired. I.e `START_PAGE` is triggered when a new page is started, and `END_PAGE` is triggered when a page has been filled (with some variation). The coordinates in this the rectangle constructor- `Rectangle(20,20,page.GetPageSize().GetWidth() - 80,80)` - determine where the content is placed, and therefore whether it will be visualized as a header or footer. – Jon Reilly Oct 17 '18 at 18:42
  • Sreedharan - Have you solved the issue using @Jon's comment? He could have made that an answer instead of a comment because it's all you need to know to fix your problem. – mkl Oct 23 '18 at 08:33

2 Answers2

0

I have did this in past using PdfEventHandler by overriding methods like this.

class PdfEvents : PdfPageEventHelper
    {

        public override void OnStartPage(PdfWriter writer, Document document)
        {

        }

        public override void OnEndPage(PdfWriter writer, Document document)
        {  

        }
    }

and then you can write your content by getting exact area in pdf using :

table.WriteSelectedRows(2, -1, 0, -1, 36, 806, canvas);
Tech Yogesh
  • 427
  • 4
  • 18
0

I created two classes in my code, one for header and one for footer, so i call the two elements like this:

pdf.AddEventHandler(PdfDocumentEvent.START_PAGE,new HeaderClass("HEADER TEXT"));
pdf.AddEventHandler(PdfDocumentEvent.END_PAGE,new FooterClass("FOOTER TEXT"));

In the HandleEvent for the header class i changed this line:

PdfCanvas canvas = new PdfCanvas(page.NewContentStreamBefore(),page.GetResources(),doc);

with this:

PdfCanvas canvas = new PdfCanvas(page.NewContentStreamAfter(),page.GetResources(),doc);

leaving the structure you have coded, but, obviously I changed the page coordinates of canvas.

Rotondof
  • 203
  • 1
  • 3
  • 8