简体   繁体   中英

Header of Page with iTextSharp

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!!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM