Getting a Stack Overflow Exception generating a PDF [duplicate]

怎甘沉沦 提交于 2019-12-20 03:36:13

问题


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.


回答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:

  • "'System.StackOverflowException" in "OnEndPage" event handler (which what you are asking; reading the book would have helped you)
  • Creating table with 2 rows in pdf footer using itext
  • How to add a table as a header?

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



来源:https://stackoverflow.com/questions/30602850/getting-a-stack-overflow-exception-generating-a-pdf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!