Getting an exception that generates a PDF file

As a follow-up to my earlier question , I tried the header and footer functions for my PDF. After a little discussion, I changed quite a lot of code in 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 fairly large pdf (maybe 10 pages)). I am getting an exception).

What am I doing wrong here (if at all)? The last question I asked, I got a polite RTM, however, after reading a lot of documentation I don't understand why something relatively simple is causing a stack overflow. Please help me understand.

+2


source to share


1 answer


As stated in the official documentation, it is strictly forbidden to add content to an object document

in a page event. This object is document

not the object document

you are using in your main code. Instead, it is an internal instance PdfDocument

that is passed to the read-only event.

If you want to add a table to a page event, you must use the writeSelectedRows()

. Download the free eBook Best iText Questions on StackOverflow and watch the chapter on events.

You will find links to the following questions:



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

+1


source







All Articles