【发布时间】:2014-01-30 22:44:54
【问题描述】:
我有一个像这样的静态方法,我正在使用 ITextSharp 生成 PDF..
public static byte[] createPDF(string htmlstr) {
var html = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<!DOCTYPE html
PUBLIC ""-//W3C//DTD XHTML 1.0 Strict//EN""
""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"">
<html xmlns=""http://www.w3.org/1999/xhtml"" xml:lang=""en"" lang=""en"">
<head>
<title>Minimal XHTML 1.0 Document with W3C DTD</title>
</head>
<body>
" + htmlstr + "</body></html>";
// step 1: creation of a document-object
Document document = new Document(PageSize.A4, 30, 30, 30, 30);
MemoryStream msOutput = new MemoryStream();
// step 2:
// we create a writer that listens to the document
// and directs a XML-stream to a file
PdfWriter.GetInstance(document, msOutput);
// step 3: we create a worker parse the document
HTMLWorker worker = new HTMLWorker(document);
// step 4: we open document and start the worker on the document
document.Open();
worker.StartDocument();
// step 5: parse the html into the document
worker.Parse(new StringReader(html));
// step 6: close the document and the worker
worker.EndDocument();
worker.Close();
document.Close();
byte[] buffer = new byte[msOutput.Length];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = msOutput.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
msOutput.Close();
return ms.ToArray();
}
}
我在调试的时候,经过worker.Parse(new StringReader(html))之后,MemoryStream.Length就不行了。
我已经看到了一些使用FileStream 的示例,但我不想创建新文件。为什么代码会出错?
【问题讨论】:
标签: .net pdf itextsharp filestream memorystream