生成PDF字节数组

时间:2014-01-30 22:44:55

标签: .net pdf itextsharp filestream memorystream

我有一个像这样的静态方法,我使用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时看到了一些示例,但我不想创建新文件。为什么代码出错?

1 个答案:

答案 0 :(得分:0)

您遇到的基本问题是默认情况下PdfWriter对象将关闭您要写入的流。 PdfWriter.GetInstance()实际上会返回一个对象,您可以设置其他属性,并且您特别想要调用:

writer.CloseStream = false;

您的MemoryStreambyte[]MemoryStreambyte[]让我感到困惑,这只是一种尝试解决上述问题的方法吗?

从iTextSharp 5.0.6开始,DocumentPdfWriter等大多数主要类都实现IDisposable,至少对我来说,using模式使代码更容易阅读和调试。你也不必考虑关闭事情。试一试:

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
    using (Document document = new Document(PageSize.A4, 30, 30, 30, 30)) {
        using (MemoryStream msOutput = new MemoryStream()) {

            // step 2:
            // we create a writer that listens to the document
            // and directs a XML-stream to a file
            using (PdfWriter writer = 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();
            }

            // Return the bytes
            return msOutput.ToArray();
        }
    }
}