PageEvent正在抛出无法访问已关闭的Stream

时间:2016-03-01 20:51:15

标签: c# itextsharp

当添加PageEvent以生成页眉和页脚时,它似乎是在关闭后尝试写入流。我不确定在哪里放置页面事件。如果我删除了页面事件,它会生成pdf罚款。

p.Parse(html);是“无法访问已关闭的流”失败的地方。删除writer.PageEvent将导致Parse工作,但它不会生成页眉/页脚。

我使用this页面作为页眉/页脚代码的参考。

代码:

try {
  using(var stream = new MemoryStream()) {
    using(var document = new Document(PageSize.LETTER, 20 f, 20 f, 5 f, 5 f)) {
      using(var writer = PdfWriter.GetInstance(document, stream)) {
        HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
        writer.PageEvent = new HeaderFooter(Header, Footer); // if I remove this it generates fine but then I don't get a header/footer.

        var tagProcessors = (DefaultTagProcessorFactory) Tags.GetHtmlTagProcessorFactory();
        tagProcessors.RemoveProcessor(HTML.Tag.IMG); // remove the default processor
        tagProcessors.AddProcessor(HTML.Tag.IMG, new CustomImageTagProcessor()); // use our new processor
        htmlContext.SetTagFactory(tagProcessors);

        ICSSResolver CssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(true);
        CssResolver.AddCss(Css, true);
        IPipeline pipeline =
         new CssResolverPipeline(CssResolver,
          new HtmlPipeline(htmlContext,
           new PdfWriterPipeline(document, writer)));

        XMLWorker worker = new XMLWorker(pipeline, true);

        XMLParser p = new XMLParser(worker);

        document.Open();

        using(var html = new StringReader(HtmlData)) {
         p.Parse(html);  // <-- ERROR thrown here
        }
       // close document   tried docoment.Close();  same issue
      }
    }

  // get bytes from stream  
  pdf = stream.ToArray();
 }
}

以下是页面事件:

public class HeaderFooter: PdfPageEventHelper {
 protected ElementList header;
 protected ElementList footer;
 private string _footerText;
 private string _headerText;

 public HeaderFooter(string headerText, string footerText) {
  header = XMLWorkerHelper.ParseToElementList(headerText, null);
  footer = XMLWorkerHelper.ParseToElementList(footerText, "table { font-size: 8px; font-style: italic; }");
  _footerText = footerText;
  _headerText = headerText;
 }

 public override void OnEndPage(PdfWriter writer, Document document) {

  try {

   footer = XMLWorkerHelper.ParseToElementList(_footerText.Replace("@PageNumber", writer.PageNumber.ToString()), "table { font-size: 8px; font-style: italic; }");
   ColumnText ct = new ColumnText(writer.DirectContent);
   ct.SetSimpleColumn(new Rectangle(36, 832, 559, 810));
   foreach(IElement e in header) {
    ct.AddElement(e);
   }
   ct.Go();
   ct.SetSimpleColumn(new Rectangle(36, 10, 559, 32));

   foreach(IElement e in footer) {
    ct.AddElement(e);
   }
   ct.Go();



  } catch (DocumentException de) {
   throw new Exception();
  }
 }
}

2 个答案:

答案 0 :(得分:1)

在将PdfWriter放入using声明之前,先看到类似的问题。这是一个基于您的问题的简化示例,您可以测试以重现该问题:

// ObjectDisposedException: Cannot access a closed Stream.
using (var stream = new MemoryStream())
{
    using (var document = new Document())
    {
        using (var writer = PdfWriter.GetInstance(document, stream))
        {
            document.Open();
            document.Add(new Chunk("test"));
        }
    }
    pdf = stream.ToArray();
}

该代码段会引发ObjectDisposedException无法访问已关闭的流。,如您所述。

相反,如果PdfWriter Dispose()是这样的:

PdfWriter writer = null;
try
{
    using (var stream = new MemoryStream())
    {
        using (var document = new Document())
        {
            writer = PdfWriter.GetInstance(document, stream);
            document.Open();
            document.Add(new Chunk("test"));
        }
        pdf = stream.ToArray();
    }
}
finally { writer.Dispose(); }
一切都很好。 iTextSharp v5.5.8。

答案 1 :(得分:0)

结果证明这是正在使用的html的一个问题。虽然页面在没有标题的情况下完全生成PDF,但在添加标题时会失败。使用和退货很好。