简体   繁体   中英

How to get reference to the sender on itext event handler

How can I get the object sender in iTextSharp event handler OnStarPage?

class _Events : PdfPageEventHelper
{

    public override void OnStartPage(PdfWriter writer, Document document)
    {
        base.OnStartPage(writer, document);
        Paragraph paragraph = new Paragraph("TITULO DE TODOS LOS REPORTES\n\n", FontFactory.GetFont("Arial", 9, iTextSharp.text.Font.BOLD));
        paragraph.Alignment = Element.ALIGN_CENTER;
        document.Add(paragraph);
      //I NEED THE OBJECT HERE

     }
}

Since you can't change the method signature, you'll need another mechanism to show which page sent a document.

One dumb, but fairly effective way to do so is to keep a dictionary in some shared object that maps the page to the document.

    Dictionary<Page, Document>.

How are you calling the code?

Create a parameter in your _Events class. Pass the name of the class to the event handler as a parameter of the constructor.

I finally solve it. If someone need it:

class _Events : PdfPageEventHelper {

public override void OnStartPage(PdfWriter writer, Document document)
{
    base.OnStartPage(writer, document);
    Paragraph paragraph = new Paragraph("GENERAL TITLE\n\n", FontFactory.GetFont("Arial", 9, iTextSharp.text.Font.BOLD));
    paragraph.Alignment = Element.ALIGN_CENTER;
    document.Add(paragraph);

    paragraph = new Paragraph(pintaTitulo(), FontFactory.GetFont("Arial", 9, iTextSharp.text.Font.BOLD));
    document.Add(paragraph);
 }

public virtual string pintaTitulo() { return "000"; }

}

And then:

class _EventsInherited : _Events
{
    public _EventsInherited(){}

    public override string pintaTitulo()
    {
        return "subTitle"; 
    }
}

And suscribe the document class to : _EventsInherited

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM