[英]How to get reference to the sender on itext event handler
How can I get the object sender in iTextSharp event handler OnStarPage? 如何在iTextSharp事件处理程序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. 在
_Events
类中创建一个参数。 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 { 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"; 公共虚拟字符串pintaTitulo(){返回“ 000”; }
}
} }
And then: 接着:
class _EventsInherited : _Events
{
public _EventsInherited(){}
public override string pintaTitulo()
{
return "subTitle";
}
}
And suscribe the document class to : _EventsInherited 并将文档类预订为:_EventsInherited
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.