0

I am doing a pdf document with iTextSharp. I created the header with PageEvent event, but I want that my header to be different in some pages. It works to setting the same header for all pages.

But, I wish for something different.

Example: Page 1=>Header 1 Page 2=>Header 1 Page 3=>Header 2 Page 4=>Header 2

I would solve it, but the troubles come when this happens:

writer.PageEvent=new PDFFooter(params);    
doc.Open();  

The PageEvent event must be created above doc.Open(); when I put it below, it generates an error. I am using C# with Visual Studio Community 2013.

How would I solve this? Thanks!!

  • possible duplicate of [Add Header and Footer for PDF using iTextsharp](http://stackoverflow.com/questions/18996323/add-header-and-footer-for-pdf-using-itextsharp) – stuartd May 08 '15 at 15:57
  • Consider counting the `OnEndPage` calls and acting different in that method depending on the count. – mkl May 08 '15 at 16:07
  • Please read [The Best iText Questions on StackOverflow](http://pages.itextpdf.com/ebook-stackoverflow-questions.html) (a free ebook) and you'll discover that your question is a duplicate of [How to generate a report with dynamic header in PDF using itextsharp?](http://stackoverflow.com/questions/21628429/itextsharp-how-to-generate-a-report-with-dynamic-header-in-pdf-using-itextsharp) – Bruno Lowagie May 08 '15 at 16:57

1 Answers1

0

I solved the problem now.

The solution was this:

PDFFooter events=new PDFFooter();

PdfWriter writer = PdfWriter.GetInstance(doc, Response.OutputStream);

doc.Open();
foreach (var item in ListReporte)
{                    
   events = new PDFFooter();
   writer.PageEvent = events;
   events.Ciudad = item.Ciudad;
   events.OnStartPage(writer,doc);
   //This was the solution
   if (writer.PageEvent != null)
      writer.PageEvent = null;
 }

In each iteration, I assign null to PageEvent property.

But first, I created a PDFFooter object (The class PDFFooter contains OnStartPage and OnEndPages events...), So, in that class I declared some attributes about the data that I wanted to display.

Previously, I couldn't instantiate the PDFFooter class below of doc.Open(). The code was like this:

writer.PageEvent=new PDFFooter();
doc.Open():

So, I tried to create the objet first and then set it to PageEvent property of writer object (I recommend to do this). And It worked.

It feels wonderful.