0

I'm using code provided here How to Add Page number in Footer in PDF by Itextsharp to create a header and footer with page numbering using iTextSharp. Visual Studio is specifically pointing out 'PageEventHelper' does not contain a definition for 'RunDateFont' and no extension method 'RunDateFont' accepting a first argument of type 'PageEventHelper' could be found (are you missing a using directive or an assembly reference?) What am I missing?

Here is a simplified version of my code:

namespace MyWebApp     {
public partial class _Default : Page
    {

 protected void Button_Click(object sender, EventArgs e)
        {
         using (MemoryStream myMemoryStream = new MemoryStream())
            {
Document document = new iTextSharp.text.Document();
                PdfWriter myPDFWriter = PdfWriter.GetInstance(document, myMemoryStream);
                PageEventHelper pageEventHelper = new PageEventHelper();
                myPDFWriter.PageEvent = pageEventHelper;

                var response = HttpContext.Current.Response;
                response.Clear();
                response.ContentType = "application/pdf";

               // myPDFWriter.PageEvent = new Footer();


                document.Open();

            document.Add(clienttable);
            document.NewPage();//page break OR New Page

            document.Add(new Paragraph("This is a second page."));

            document.Close();

            byte[] content = myMemoryStream.ToArray();

            myPDFWriter.Flush();
            response.OutputStream.Write(myMemoryStream.GetBuffer(), 0, myMemoryStream.GetBuffer().Length);
            response.OutputStream.Flush();
            response.OutputStream.Close();
            response.End();
        }
    }
// some other functions

}


    public class PageEventHelper : PdfPageEventHelper
        {
            PdfContentByte cb;
            PdfTemplate template;

            public override void OnOpenDocument(PdfWriter myPDFWriter, Document document)
            {
                cb = myPDFWriter.DirectContent;
                template = cb.CreateTemplate(50, 50);
            }

            public override void OnEndPage(PdfWriter myPDFWriter, Document document)
            {
                base.OnEndPage(myPDFWriter, document);

                int pageN = myPDFWriter.PageNumber;
                String text = "Page " + pageN.ToString() + " of ";
                float len = this.RunDateFont.BaseFont.GetWidthPoint(text, this.RunDateFont.Size);

                iTextSharp.text.Rectangle pageSize = document.PageSize;

                cb.SetRGBColorFill(100, 100, 100);

                cb.BeginText();
                cb.SetFontAndSize(this.RunDateFont.BaseFont, this.RunDateFont.Size);
                cb.SetTextMatrix(document.LeftMargin, pageSize.GetBottom(document.BottomMargin));
                cb.ShowText(text);

                cb.EndText();

                cb.AddTemplate(template, document.LeftMargin + len, pageSize.GetBottom(document.BottomMargin));
            }

            public override void OnCloseDocument(PdfWriter myPDFWriter, Document document)
            {
                base.OnCloseDocument(myPDFWriter, document);

                template.BeginText();
                template.SetFontAndSize(this.RunDateFont.BaseFont, this.RunDateFont.Size);
                template.SetTextMatrix(0, 0);
                template.ShowText("" + (myPDFWriter.PageNumber - 1));
                template.EndText();
            }
    }
Community
  • 1
  • 1
Maureen
  • 207
  • 1
  • 4
  • 11
  • `PdfPageEventHelper` has no properties, only methods. So the error would be accurate. – Nkosi Mar 30 '16 at 15:55
  • 2
    Take a look at this one [iTextSharp Creating a Footer Page # of #](http://stackoverflow.com/a/1372669/5233410) instead where they don't use that property. – Nkosi Mar 30 '16 at 16:17
  • 1
    `RunDateFont` is just an arbitrary custom class/structure that holds someone's runtime configuration data. There's nothing iText-specific to it except that it happens to expose properties that are iText types. The whole class could just be local variables instead. I hands down recommend making two passes on your document, one for text and one for headers and footers. See the [*EDIT* here](http://stackoverflow.com/a/9845722/231316) which shows how to do that avoiding the need to even use events. – Chris Haas Mar 30 '16 at 18:25

0 Answers0