How to introduce multiple PdfPageEventHelper instances?

I have to generate a large amount of different types of documents using the iTextSharp library that all have things in common.

31st May 2016
admin-marketing

Some pages have common headers, page counts, watermarks my initial thought was to have different PdfPageEventHelper subclasses for example WatermarkPdfPageEventHelper, OrderHeaderPdfPageEventHelper, PageNumberPdfPageEventHelper, and so on, and apply them when necessary to compose the documents but PageEvent is not really an event but an instance of only one IPdfEvent, what is the correct way to implement this?

Posted on StackOverflow on Mar 6, 2013 by Marc

In iText 7 (Java), you can do this:

pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE, watermarkeventhandler);
pdfDoc.addEventHandler(PdfDocumentEvent.START_PAGE, headereventhandler);
pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE, footereventhandler);

Internally a EventDispatcher is created, which determine the handling of events. The possible types of PdfDocumentEvent are next: START_PAGE, INSERT_PAGE, REMOVE_PAGE and END_PAGE.

When you want to remove the events, you just need to do this:

pdfDoc.removeAllHandlers();

I'm pretty sure you can do the same thing in iTextSharp although there may be slight differences in the class and method names.

Click this link if you want to see how to answer this question in iText 5.



Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now