How to check for an event and remove it?

Is there a way to check to see if a page event has already been added to a PdfWriter object? If so, can you also remove that page event?

Posted on StackOverflow on Apr 15, 2014 by IyaTaisho

There is an IEventHandler interface in iText 7. You should add it’s implementation (say, MyEventHandler) to the PdfDocument instance like this:

PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
MyEventHandler handler = new MyEventHandler();
pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE, handler);

You can check if the document has any type of event (PdfDocumentEvent.START_PAGE, PdfDocumentEvent.INSERT_PAGE, PdfDocumentEvent.REMOVE_PAGE, PdfDocumentEvent.END_PAGE) using hasEventHandler(String type) method.

To remove an existing page event, you need removeEventHandler(String type, IEventHandler handler) method. If you want to delete all page events, feel free to invoke removeAllHandlers().

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

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

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