1

With iTextSharp you could add header/footer to a PDF by attaching an event to the PDF like explained in this SO answer: https://stackoverflow.com/a/19004392

How can I do the same thing with iText 7?

This link has Java code example but does not seem like its using page events.

joym8
  • 4,014
  • 3
  • 50
  • 93
  • why do you _need_ to use Page Events to add Header and Footer? You can find the .NET version of those examples here https://github.com/itext/i7ns-samples/tree/develop/itext/itext.samples/itext/samples/sandbox/stamper – André Lemos Jan 09 '20 at 07:55
  • you can also look into https://stackoverflow.com/questions/52855077/how-to-create-header-in-pdf-generation-using-c-sharp-itext7 – André Lemos Jan 09 '20 at 09:58
  • 1
    @AndréLemos you're right, I don't need to use Page Events. They just look more elegant than `for` loop. That's what I might have to use now. Thanks! – joym8 Jan 09 '20 at 15:07

2 Answers2

6

The iText 7 .Net sample TextFooter.cs illustrates how to automatically add headers and footers by means of events:

public class TextFooter
{
    public static readonly String DEST = "results/sandbox/events/text_footer.pdf";

    public static void Main(String[] args)
    {
        FileInfo file = new FileInfo(DEST);
        file.Directory.Create();

        new TextFooter().ManipulatePdf(DEST);
    }

    protected void ManipulatePdf(String dest)
    {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
        pdfDoc.AddEventHandler(PdfDocumentEvent.END_PAGE, new TextFooterEventHandler(doc));

        for (int i = 0; i < 3; i++)
        {
            doc.Add(new Paragraph("Test " + (i + 1)));
            if (i != 2)
            {
                doc.Add(new AreaBreak());
            }
        }

        doc.Close();
    }

    private class TextFooterEventHandler : IEventHandler
    {
        protected Document doc;

        public TextFooterEventHandler(Document doc)
        {
            this.doc = doc;
        }

        public void HandleEvent(Event currentEvent)
        {
            PdfDocumentEvent docEvent = (PdfDocumentEvent) currentEvent;
            Rectangle pageSize = docEvent.GetPage().GetPageSize();
            PdfFont font = null;
            try {
                font = PdfFontFactory.CreateFont(StandardFonts.HELVETICA_OBLIQUE);
            }
            catch (IOException e) 
            {
                Console.Error.WriteLine(e.Message);
            }

            float coordX = ((pageSize.GetLeft() + doc.GetLeftMargin())
                             + (pageSize.GetRight() - doc.GetRightMargin())) / 2;
            float headerY = pageSize.GetTop() - doc.GetTopMargin() + 10;
            float footerY = doc.GetBottomMargin();
            Canvas canvas = new Canvas(docEvent.GetPage(), pageSize);
            canvas
                .SetFont(font)
                .SetFontSize(5)
                .ShowTextAligned("this is a header", coordX, headerY, TextAlignment.CENTER)
                .ShowTextAligned("this is a footer", coordX, footerY, TextAlignment.CENTER)
                .Close();
        }
    }
}
mkl
  • 90,588
  • 15
  • 125
  • 265
  • `AddEventHandler` is not showing up as a method for `Document` per IntelliSense. What assembly/namespace do I need to include? – joym8 Jan 09 '20 at 15:04
  • @joym8 *"`AddEventHandler` is not showing up as a method for `Document` per IntelliSens"* - that's not surprising as it's a method of `PdfDocument`, not `Document`. See the quoted example, `pdfDoc.AddEventHandler` for `PdfDocument pdfDoc` is called... – mkl Jan 09 '20 at 15:36
1

Adding one of the code samples in the Github as an answer (it adds the word "Copy" as a Header to an existing PDF file).

 protected void ManipulatePdf(String dest) 
    {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
        Document doc = new Document(pdfDoc);

        Paragraph header = new Paragraph("Copy")
                .SetFont(PdfFontFactory.CreateFont(StandardFonts.HELVETICA))
                .SetFontSize(14)
                .SetFontColor(ColorConstants.RED);

        for (int i = 1; i <= pdfDoc.GetNumberOfPages(); i++) 
        {
            Rectangle pageSize = pdfDoc.GetPage(i).GetPageSize();
            float x = pageSize.GetWidth() / 2;
            float y = pageSize.GetTop() - 20;
            doc.ShowTextAligned(header, x, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0);
        }

        doc.Close();
    }
André Lemos
  • 837
  • 6
  • 18