简体   繁体   English

iTextSharp没有在PDF中显示HTML表

[英]iTextSharp is not showing HTML table in PDF

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

My PDF is generated but the table is not showing ... 我的PDF已生成,但表格未显示...

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. 您有一个HTML字符串,然后将其转储到ASP.Net控件中,然后将其进一步转储到另一个ASP.Net控件中。 Then you're asking ASP.Net to render the control back to HTML. 然后,您要ASP.Net将控件呈现回HTML。 That's three or four lines you can kill off. 您可以杀死三到四行。

Also, you're writing to the raw HTTP response stream and then sending the PDF to the same stream. 另外,您正在写入原始HTTP响应流,然后将PDF发送到同一流。 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. 您还更改了HTTP标头,如果出现ASP.Net错误,这可能会导致问题。

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. 我将其切换为using语句,以确保情况得到清理。 If you're using an older unsupported version of iTextSharp you'll want to switch those back. 如果您使用的是不受支持的旧版本iTextSharp,则需要将其切换回原来的状态。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM