Html iTextSharp异常:无法访问关闭的流

Html iTextSharp异常:无法访问关闭的流,html,exception,pdf,itextsharp,Html,Exception,Pdf,Itextsharp,我正在使用iTextSharp库,遇到了一个问题。 当我阅读html代码时,我生成了以下异常: “例外情况:不允许任何其他人进入该区域” 异常:无法访问已关闭的序列 这是我使用的代码: Byte[] bytes; //Boilerplate iTextSharp setup here //Create a stream that we can write to, in this case a MemoryStream using (var ms = new MemoryStream()) {

我正在使用iTextSharp库,遇到了一个问题。 当我阅读html代码时,我生成了以下异常: “例外情况:不允许任何其他人进入该区域” 异常:无法访问已关闭的序列

这是我使用的代码:

Byte[] bytes;

//Boilerplate iTextSharp setup here
//Create a stream that we can write to, in this case a MemoryStream
using (var ms = new MemoryStream())
{

    //Create an iTextSharp Document which is an abstraction of a PDF but **NOT** a PDF
    using (var doc = new Document())
    {

        //Create a writer that's bound to our PDF abstraction and our stream
        using (var writer = PdfWriter.GetInstance(doc, ms))
        {

            //Open the document for writing
            doc.Open();
            var img = iTextSharp.text.Image.GetInstance("http://" + Request.ServerVariables["SERVER_NAME"] + "/styles/imgs/noticias_ambitos/170.png");
           // var img = iTextSharp.text.Image.GetInstance("http://" + Request.ServerVariables["SERVER_NAME"] + "/styles/imgs/Fondo-Plantilla-apaisado-cabecera-pdf.jpg");
            img.ScaleAbsolute(300f, 70f);
            //Hacemos que se pueda escribir encima de la imagen.
            img.Alignment = iTextSharp.text.Image.UNDERLYING;

            doc.Add(img);

            //Create a new HTMLWorker bound to our document
            using (var htmlWorker = new iTextSharp.text.html.simpleparser.HTMLWorker(doc))
            {

                //HTMLWorker doesn't read a string directly but instead needs a TextReader (which StringReader subclasses)
                using (var sr = new StringReader(Noticia))
                {

                    //Parse the HTML
                    htmlWorker.Parse(sr);

                }
            }


            doc.Close();
        }
    }

    //After all of the PDF "stuff" above is done and closed but **before** we
    //close the MemoryStream, grab all of the active bytes from the stream
    bytes = ms.ToArray();
}

//Now we just need to do something with those bytes.
//Here I'm writing them to disk but if you were in ASP.Net you might Response.BinaryWrite() them.
//You could also write the bytes to a database in a varbinary() column (but please don't) or you
//could pass them to another function for further PDF processing.
var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.pdf");
System.IO.File.WriteAllBytes(testFile, bytes);
变量“Noticia”包含HTML中的标记以及标记图像。问题是我输入了一个图像HTML标记

如何引入一个包含html中各种标记的变量来将其转换为PDF

谢谢

这个。哪一行具体抛出该异常?此外,正如您从中获得该示例的帖子中所述,除非您有没有任何CSS的非常简单的HTML,否则您应该真正切换到
XMLWorker
,因为
HTMLWorker
不再被维护或支持!!!