Is it possible to attach multiple layout events to a PdfPCell?

I'm not sure if it's possible to set multiple events. I would like to separate different cell options in separate events based on my business logic. Sometimes I want to draw an ellipse in it, sometimes a square (or anything else). It would be nice if I could simply attach the events that I need.

Posted on StackOverflow on Mar 18, 2015 by Robert

Yes, you can add multiple cell events to a cell. This is the Java code of the setCellEvent() method:

public void setCellEvent(PdfPCellEvent cellEvent) {
    if (cellEvent == null) {
        this.cellEvent = null;
    } else if (this.cellEvent == null) {
        this.cellEvent = cellEvent;
    } else if (this.cellEvent instanceof PdfPCellEventForwarder) {
        ((PdfPCellEventForwarder) this.cellEvent).addCellEvent(cellEvent);
    } else {
        PdfPCellEventForwarder forward = new PdfPCellEventForwarder();
        this.cellEvent = forward;

If you pass null, then all existing events are removed from the cell. If no cell event was present, a new cell event is added. If there is already a cell event present, a PdfPCellEventForwarder is created. This is a class that stores different cell events and that eventually will execute all these events one by one.

I found the answer thanks to your response. The PdfPCellEventForwarder class is publicly available in iTextSharp. An instance of this can be set to the CellEvent property of a PdfPCell. On the instance, one can call the AddCellEvent() method.

iTextSharp (C#) is kept in sync with iText (Java), so the Java functionality also works for iTextSharp:

virtual public IPdfPCellEvent CellEvent {
    get {
        return this.cellEvent;
    set {
        if (value == null) this.cellEvent = null;
        else if (this.cellEvent == null) this.cellEvent = value;
        else if (this.cellEvent is PdfPCellEventForwarder)
        else {
            PdfPCellEventForwarder forward = new PdfPCellEventForwarder();
            this.cellEvent = forward;

There is no need to create your own PdfPCellEventForwarder (although you may do so if you want to), iTextSharp will take care of creating a PdfPCellEventForwarder in your place if you add multiple events to a PdfPCell.

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