iTextSharp的未显示在PDF HTML表 [英] iTextSharp is not showing HTML table in PDF

查看:161
本文介绍了iTextSharp的未显示在PDF HTML表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么它不工作。
这里是我的code:

I don't understand why it is not working. Here is my code:

Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
string htmlstr = "<html><body><h1>My First Heading</h1><p>My first paragraph.</p><table border=1><tr><td>1st</td><td>2nd</td></tr><tr><td>3rd</td><td>4th</td></tr></table></body></html>";

Panel panel1 = new Panel();
panel1.Controls.Add(new LiteralControl(htmlstr));

panel1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();

生成我的PDF,但该表没有显示...

My PDF is generated but the table is not showing ...

推荐答案

您已经有了一些疯狂的code存在(这似乎是从的这里)。你有一个HTML字符串,但那么你就卸入一个ASP.Net控制,你再有人倾倒到另一个ASP.Net控制。然后你问ASP.Net呈现控件返回HTML。这是三,四线就可以杀死。

You've got some crazy code there (which appears to be from here). You've got an HTML string but then your dumping it into an ASP.Net control which you're further dumping into another ASP.Net control. Then you're asking ASP.Net to render the control back to HTML. That's three or four lines you can kill off.

此外,你写的原始HTTP响应流,然后发送PDF格式相同的流。这是你的更大的问题,其实。我强烈建议从不写入原始流,直到你完全处理完的事情。你也改变了HTTP标头这可能会导致问题,如果有ASP.Net的错误。

Also, you're writing to the raw HTTP response stream and then sending the PDF to the same stream. This is your bigger problem, actually. I strongly suggest never writing to the raw stream until you're absolutely done processing things. You've also changed the HTTP headers which can cause problems if there are ASP.Net errors.

下面code是你有什么返工。我使用表,以确保事情得到清理切换它交给。如果您使用iTextSharp的较旧版本不支持你想切换回那些

The below code is a rework of what you've got. I switched it over to using statements to ensure that things get cleaned up. If you're using an older unsupported version of iTextSharp you'll want to switch those back.

string htmlstr = "<html><body><h1>My First Heading</h1><p>My first paragraph.</p><table border=1><tr><td>1st</td><td>2nd</td></tr><tr><td>3rd</td><td>4th</td></tr></table></body></html>";

//We'll store our final PDF in this
byte[] bytes;

//Read our HTML as a .Net stream
using (var sr = new StringReader(htmlstr)) {

    //Standard PDF setup using a MemoryStream, nothing special
    using (var ms = new MemoryStream()) {
        using (var pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f)) {

            //Bind a parser to our PDF document
            using (var htmlparser = new HTMLWorker(pdfDoc)) {

                //Bind the writer to our document and our final stream
                using (var w = PdfWriter.GetInstance(pdfDoc, ms)) {

                    pdfDoc.Open();

                    //Parse the HTML directly into the document
                    htmlparser.Parse(sr);

                    pdfDoc.Close();

                    //Grab the bytes from the stream before closing it
                    bytes = ms.ToArray();
                }
            }
        }
    }
}

//Assuming that the above worked we can now finally modify the HTTP response
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=BusinessUnit.pdf");
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
//Send the bytes from the PDF
Response.BinaryWrite(bytes);
Response.End();

这篇关于iTextSharp的未显示在PDF HTML表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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