3

I'm using the iTextSharp library and have a problem. When I read html code I generated this exception: "EXCEPCION: No se puede obtener acceso a una secuencia cerrada" EXCEPTION: Can not access a closed sequence

This is the code I use:

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

The variable "Noticia" contains tag in HTML and also tag image. The problem is caused me to enter an image HTML tag.

How could introduce a variable that contains all kinds of tags in html to convert it to PDF?

Thank you !!

Chris Haas
  • 53,986
  • 12
  • 141
  • 274
Daniel
  • 43
  • 1
  • 5
  • This [looks like some sample code that I wrote](http://stackoverflow.com/q/25164257/231316). Which line specifically is throwing that exception? Also, as stated in the post that you got that sample from, unless you have very simple HTML without any CSS you should really switch to `XMLWorker` since `HTMLWorker` is no longer maintained or supported. – Chris Haas Dec 03 '14 at 14:19
  • In line "htmlWorker.Parse(sr);" thanks!!! – Daniel Dec 15 '14 at 10:47

0 Answers0