我正在使用iTextSharp库并遇到问题。 当我读取HTML代码时,我生成了这个异常: “EXCEPCION:没有一个puede obtener加入una secuencia cerrada” 例外:无法访问已关闭的序列
这是我使用的代码:
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?
谢谢!