I need to pass a parameter to an overridden version of the "OnEndPage" method, when i declare the parameter i don't get any error but when i call the method it tells me that "cannot implicitly convert void to itextsharp.text.pdf.ipdfpageevent"
This is my OnEndPage method:
public class pdfPage : iTextSharp.text.pdf.PdfPageEventHelper
{
public override void OnEndPage(PdfWriter writer, Document doc, int parametro)
{
PdfPTable headerTbl = new PdfPTable(1);
headerTbl.TotalWidth = doc.PageSize.Width;
Image logo = Image.GetInstance("logo.png");
logo.ScalePercent(42);
PdfPCell cell = new PdfPCell(logo);
cell.HorizontalAlignment = Element.ALIGN_LEFT;
cell.PaddingRight = 52;
cell.Border = 0;
headerTbl.AddCell(cell);
headerTbl.WriteSelectedRows(0, -1, 0, (doc.PageSize.Height - 10), writer.DirectContent);
PdfPTable headerrow = new PdfPTable(7);
headerrow.TotalWidth = 570f;
headerrow.LockedWidth = true;
headerrow.AddCell(new Phrase("TIPO DOCUMENTO", new Font(Font.FontFamily.HELVETICA, 6f)));
headerrow.WriteSelectedRows(0, -1, 0, (doc.PageSize.Height - 50), writer.DirectContent);
}
}
and this is how i am calling it:
var doc = new Document(PageSize.A4, 10, 10, 170, 10);
pdfPage page = new pdfPage();
PdfWriter pdfWriter = PdfWriter.GetInstance(doc, new FileStream( "test.pdf", FileMode.Create));
pdfWriter.PageEvent = page.OnEndPage(pdfWriter, doc, 1234);
To further add to what Bruno said, if you want to add additional information to your PdfPageEventHelper
you can just add additional properties:
public class pdfPage : iTextSharp.text.pdf.PdfPageEventHelper
{
public int parametro { get; set; }
public override void OnEndPage(PdfWriter writer, Document doc)
{
//...
}
}
And then:
var page = new pdfPage();
page.parametro = 40;
pdfWriter.PageEvent = page;
Or just:
pdfWriter.PageEvent = new pdfPage() { parametro = 40 };
If you want to change this valid "per page", you're going to need to move your logic into this class instead of the calling body.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With