Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a parameter to an overridden OnEndPage method

Tags:

c#

itextsharp

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);
like image 371
Mariano Avatar asked Dec 11 '22 04:12

Mariano


1 Answers

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.

like image 161
Chris Haas Avatar answered Jan 01 '23 13:01

Chris Haas