0

I'm working on a form where I need a client to receive a copy of the form in a PDF format after they have submitted the form, I created a button to get the PDF form however the issue lies in where the PDF form shows JavaScript, CSS, jquery code, and the form has a drop down lists of the Cities, Regions the problem there is the drop downs show all the information that it contains not the one that the client chose. My aim is for PDF form to show the data that the client filled in.

 protected void btn_PDF_Click(object sender, EventArgs e)
    {


        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=The_Client_Information.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        this.Page.RenderControl(hw);
        StringReader sr = new StringReader(sw.ToString());
        WebClient client = new WebClient(); String html = client.DownloadString("http://localhost:50389/The_Client_Information.aspx");
        Regex regSimple = new Regex(@"(?i)<img\b[^>]*?style\s*=(['""]?)(?<style>[^'""]+)\1[^>]*?>");
        if (regSimple.IsMatch(html))
        {
            html = html.Replace(Convert.ToString(regSimple.Match(html)), Convert.ToString(regSimple.Match(html)).Replace("style=\"", "").Replace("height:", "height=\"").Replace("\"width:", "width=\""));

        }
        Document pdfDoc = new Document(iTextSharp.text.PageSize.A4.Rotate(), 10, 10, 10, 10);
        pdfDoc.SetPageSize(iTextSharp.text.PageSize.A4);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        htmlparser.Parse(sr);
        pdfDoc.Close();
        Response.Write(pdfDoc);
        Response.End();

    }

This is how it appears on a PDF

Amedee Van Gasse
  • 7,280
  • 5
  • 55
  • 101
  • Your question is an exact copy of the question asked in the post [Converting HTML to PDF using iText](https://stackoverflow.com/questions/47895935/) where there's a screen shot that shows that using `HTMLWorker` indeed results in the CSS being displayed in the PDF instead of the styles being applied to the content. Also be aware that iText doesn't interpret content generated through JavaScript (jQuery) and that support of forms is limited. See https://stackoverflow.com/questions/51633255 – Bruno Lowagie Aug 02 '18 at 07:47
  • If you want to implement the functionality that you describe, you need more than just iText. You'd need something like [iText DITO](http://dito.online/) to design the input form and the output form. DITO supports JavaScript (e.g. you can define a field that is the sum of a series of other fields, etc.). DITO also allows you to define a data-binding between the fields in the form and your data (e.g. JSON data). – Bruno Lowagie Aug 02 '18 at 07:50

0 Answers0