“'System.StackOverflowException” in “OnEndPage” event handler

风格不统一 提交于 2019-11-27 04:57:33

问题


In the code below, you can see that I overrode OnEndPage event and tried to add a paragraph to the document. However, I get an "System.StackOverflowException" error when attempting to run the code. Does anyone have any idea why this is happening and how can I fix it?

public override void OnEndPage(PdfWriter writer, Document document)
{
    base.OnEndPage(writer, document);
    Paragraph p = new Paragraph("Paragraph");
    document.Add(p);
}

回答1:


It is forbidden to use document.Add() in a page event. The document object passed as a parameter is actually a PdfDocument object. You should use it for read-only purposes only. This is documented in my book iText in Action - Second Edition.

If you want to add content in the OnEndPage method, you need the writer, for instance writer.DirectContent.



来源:https://stackoverflow.com/questions/24285547/system-stackoverflowexception-in-onendpage-event-handler

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