How to introduce multiple PdfPageEventHelper instances?

I have to generate a large amount of different types of documents using the iTextSharp library, all have things in common, some have common headers, page counts, watermarks my initial thought was to

I have to generate a large amount of different types of documents using the iTextSharp library, all have things in common, some 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

Page events can be cumulative. In iText (Java), you can do this:

writer.setPageEvent(watermarkevent);
writer.setPageEvent(headerevent);
writer.setPageEvent(footerevent);

Internally, a PdfPageEventForwarder is created. This object will make sure that each event is triggered in the order you added them.

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

writer.setPageEvent(null);

In your case, you could create your own PdfPageEventForwarder instances, creating different combinations of page events.

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

That is the solution I was looking for, thanks. The PageEvent thing is not very intuitive. In the c# version of the library it's a property and not a method so one expects to be holding a reference of a IPdfPageEvent instance.


Share this article

Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial
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