How to make a client receive a copy of a form they filled in, as a PDF in ASP.NET + C#

两盒软妹~` 提交于 2020-03-26 05:31:12

问题


I'm creating a form in ASP.NET and want the customer to receive the PDF copy of the form when they have filled it in and submitted it; i.e. I need to create a PDF copy of the HTML form, with the customer information filled in.

I've tried different sources, but many that i have seen are just a set of tables; I haven't seen one that has a custom form with the information that a customer has just filled in.

How can I create a PDF from an existing form and a set of customer data?

Here is my code so far:

protected void btnExport_Click(object sender, EventArgs e)
    {
        string ubARSpecials = "";
        if (CheckBox_Specials.Checked)
        {
            ubARSpecials = "1";
        }
        else
        {
            ubARSpecials = "0";
        }

        string Query = "Insert into NewClient_Information(Name, Trading_As, ubARSpecials) values ('" + Txtbox_CompanyN.Text.Replace("'", "''") + "', '" + Txtbox_TrandingAs.Text.Replace("'", "''") + "', '" + ubARSpecials + "'); ";
        SqlCommand cmd = new SqlCommand(Query, conn);

        try
        {
            conn.Open();
            myReader = cmd.ExecuteReader();

            MessageBox.Show(this.Page, "Submitted Succesfully");

            Txtbox_TrandingAs.Text = "";
            Txtbox_CompanyN.Text = "";
            CheckBox_Specials.Checked = false;
        }


        catch (Exception ex)
        {
            MessageBox.Show(this.Page, ex.Message);
        }

        conn.Close();



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

来源:https://stackoverflow.com/questions/51633255/how-to-make-a-client-receive-a-copy-of-a-form-they-filled-in-as-a-pdf-in-asp-ne

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