TextBox controls are not Working with Export To PDF(iTextSharp)

▼魔方 西西 提交于 2019-12-24 01:47:33

问题


I have an HTML form, which contains lables and textboxes.

After filling this form, it is exported to PDF.

All the label Texts are exported. But the textbox text is not exported to PDF.

Code

protected void btnExportPDF_Click(object sender, EventArgs e)
{
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=DecForm.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    this.divToPdf.RenderControl(hw);
    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.A4, 10, 10, 2, 10);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End();
}

Why are the textbox text not exported through to PDF?


回答1:


I think that when you're rendering divToPDF you are getting a fresh cut of the html and it does not have the values there were populated on the page. You may want to look at using the divToPDF is you'll want to look at accessing the InnerHtml or OuterHtml property and use that.




回答2:


Try this.

List<IElement> elements = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(htmlString), null);
//htmlString is your form html
foreach (IElement el in elements)
{
    pdfDoc.add(el);
}


来源:https://stackoverflow.com/questions/14850880/textbox-controls-are-not-working-with-export-to-pdfitextsharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!