生成PDF字节数组 [英] Generating PDF byte array

查看:169
本文介绍了生成PDF字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的静态方法,并且我正在使用ITextSharp生成PDF.

I have a static method like this and I am using ITextSharp to generate 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不起作用.

When I was debugging, after I pass worker.Parse(new StringReader(html)), the MemoryStream.Length does not work.

我已经看到一些使用FileStream的示例,但是我不想创建一个新文件.为什么代码会出错?

I have seen some examples out there using FileStream but I do not want to create a new file. Why is the code erroring?

推荐答案

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

The basic problem that you are running into is that by default the PdfWriter object will close the stream that you are writing to. PdfWriter.GetInstance() actually returns an object that you can set additional properties and you specifically want to call:

writer.CloseStream = false;

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

Your MemoryStream to byte[] to MemoryStream to byte[] confuses me, was that just a way to try to work around the above problem?

从iTextSharp 5.0.6开始,大多数主要类(例如DocumentPdfWriter)都实现了IDisposable,至少对我而言,它与using模式一起使代码更易于阅读和调试.您也不必考虑关闭太多事情.试一试:

Beginning with iTextSharp 5.0.6 most of the primary classes such as Document and PdfWriter all implement IDisposable which, at least for me, along with the using pattern makes code much easier to read and debug. You also don't have to think about closing things as much. Give this a shot:

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

这篇关于生成PDF字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆