2

As a continuation to my earlier question I have been trying out the header and footer functions for my PDF. After a little discussion I have changed quite a lot of code on the PdfPageEventHelper class. Below is what I have:

public class ReportHeaderFooter : PdfPageEventHelper
{
    public string HeaderTitle { get; set; }
    public IReportsAccessor ReportsAccessor { get; set; }
    private Image footerImg;
    private Image headerImg;
    private BaseColor headerColor;
    private PdfPTable tblHeader;
    public ReportHeaderFooter(IReportsAccessor reportsAccessor)
    {
        this.ReportsAccessor = reportsAccessor;
        var rootPath = ConfigurationManager.AppSettings["SaveFileRootPath"];
        headerColor = new BaseColor(System.Drawing.ColorTranslator.FromHtml("#ffffff")); // Not really but I don't want to give away the color

    }

    public override void OnOpenDocument(PdfWriter writer, Document document)
    {
        base.OnOpenDocument(writer, document);
        // Set the initial header image...
        var headerImgInfo = ReportsAccessor.GetImage(4);
        headerImg = Image.GetInstance(headerImgInfo.ReportImage);

        // Set the initial footer image...
        var footerImgInfo = ReportsAccessor.GetImage(2);
        footerImg = Image.GetInstance(footerImgInfo.ReportImage);

        // Create the header table...
        tblHeader = new PdfPTable(2)
        {
            TotalWidth = document.Right,
        };
        tblHeader.SetWidths(new float[2] { document.Right - 70f, 70f });
        PdfPCell titleCell = new PdfPCell(new Phrase(HeaderTitle))
        {
            BackgroundColor = headerColor
        };
        tblHeader.AddCell(titleCell);
        PdfPCell imgCell = new PdfPCell(headerImg)
        {
            BackgroundColor = headerColor,
            HorizontalAlignment = PdfPCell.ALIGN_RIGHT,
        };
        tblHeader.AddCell(imgCell);
    }

    public override void OnEndPage(PdfWriter writer, Document document)
    {
        base.OnEndPage(writer, document);
        // Add the header table to the tops of the documents...
        document.Add(tblHeader);

        // Create the image at the footer.
        footerImg.SetAbsolutePosition(0, document.Bottom);
        document.Add(footerImg);            
    }

However, when I get to the document.Add(tblHeader) line on one of the pages (this is a reasonably large pdf (probably 10 pages)). I get a stack overflow exception).

What am I doing wrong here (if anything)? Last question that I asked I received a polite RTM, however, having read a lot of documentation, I can't see why something relatively simple would be causing a stack overflow. Please help me to understand.

Community
  • 1
  • 1
Daniel Casserly
  • 3,552
  • 2
  • 29
  • 60
  • Without running your code, my guess is that your `document.Add(tblHeader);` is causing a new page to be added which causes `OnEndPage` to be called which causes a new page to be added which causes `OnEndPage` to be called which causes a new page to be added which causes `OnEndPage` to be called which causes a new page to be added which causes `OnEndPage` to be called which causes a new page to be added which causes `OnEndPage` to be called which causes a new page to be added which causes `OnEndPage` to be called which causes a new page to be added which causes `OnEndPage` to be called... – Chris Haas Jun 02 '15 at 19:20
  • With other words... whenever `OnEndPage` is called, the page already is full, at this time you cannot add content for automatic layouting on the page anymore. Thus, calling `document.Add` in that method can only fail. Look at the myriad examples using iText page events, especially those on stackoverflow or the iText web site. – mkl Jun 02 '15 at 19:57
  • So you're saying that a two cell table is likely too big for the page. I will have a look (if I remove the footer and run it what occurs) and report back here. I'm not really sure what the myriad of examples is going to do as I have already checked them. – Daniel Casserly Jun 03 '15 at 07:42
  • The most important part of those examples is that they'll almost always add content at specific locations instead of relying on the abstraction layer to figure things out. I would look into wither `ColumnText` or `PdfPTable.WriteSelectedRows()` – Chris Haas Jun 03 '15 at 19:30

1 Answers1

1

As mentioned in the official documentation, it is strictly forbidden to add content to the document object in a page event. That document object is not the Document object you are using in your main code. Instead, it is an internal PdfDocument instance that is passed to the event for read only purposes.

If you want to add a table in a page event, you should use the writeSelectedRows() method. Please download the free ebook The Best iText Questions on StackOverflow and take a look at the chapter about events.

You'll find references to the following questions:

Reading the documentation that is available for free can save you (and us) plenty of time ;-)

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • 1
    *As mentioned in the official documentation, it is strictly forbidden to add content to the document object in a page event.* - you should start throwing exceptions there. – mkl Jun 06 '15 at 19:01