获取堆栈溢出异常生成PDF [英] Getting a Stack Overflow Exception generating a PDF

查看:142
本文介绍了获取堆栈溢出异常生成PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为我的之前的问题的延续,我有我一直在尝试PDF的页眉和页脚功能。经过一番讨论后,我在PdfPageEventHelper类上更改了很多代码。以下是我的内容:

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);            
    }

然而,当我到达document.Add(tblHeader)行时页面(这是一个相当大的pdf(可能是10页))。我得到一个堆栈溢出异常)。

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).

我在这里做错了什么(如果有的话)?最后一个问题,我问我收到了一个礼貌的RTM,然而,在阅读了大量文档之后,我看不出为什么相对简单的东西会导致堆栈溢出。请帮我理解。

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.

推荐答案

正如官方文档中所述,严格禁止将内容添加到<$页面事件中的c $ c> document 对象。 文档对象不是您在主代码中使用的文档对象。相反,它是一个内部 PdfDocument 实例,为只读目的而传递给该事件。

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.

如果要在页面事件中添加表,则应使用 writeSelectedRows()方法。请下载免费电子书 StackOverflow上的最佳iText问题并查看关于事件的章节。

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 ;-)

这篇关于获取堆栈溢出异常生成PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆