ITextSharp exception: Cannot access closed stream
I am using the iTextSharp library and have a problem. When I read the html code, I threw this exception: "EXCEPTION: No, I don't know what you like" EXCEPTION: Can't access private sequence
This is the code I am using:
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 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 a tag in HTML as well as a tag. The problem is that I entered the HTML image tag.
How can one inject a variable containing all types of tags in html to convert it to PDF?
Thank!
+3
source to share
No one has answered this question yet
See similar questions:
or similar: