我有一个 aspx 表单,用户在其中进行调查并将调查保存到数据库中。保存到数据库后,将显示用户给出的调查回复,以便用户可以将回复保存为 pdf。
这是保存为pdf的功能
//Save as PDF
protected void SaveAsPDF_Click(object sender, EventArgs e)
{
Random nRandom = new Random(DateTime.Now.Second);
string strFileName = nRandom.Next().ToString() + ".pdf";
string target = Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["NewResponsePDF"].ToString() + strFileName);
string filepath = target;
string filename = Path.GetFileName(filepath);
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + filename);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.Page.Form.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();
}
它工作正常,一切都很好。表单有一些文本框。用户可以在文本框中输入文本。有时,用户输入诸如 <、>、= 之类的数学符号。为了确保将文本框值保存到数据库中,ValidateRequest="false"
在 aspx 文件中进行了以下操作。但是,SaveAsPDF 会引发错误,因为它无法解析这些符号。
我很确定主要是这些特殊符号导致了问题
htmlparser.Parse(sr)
方法
因为如果文本框中没有特殊符号,则可以正常生成 PDF。
你能帮我看看我能做什么吗?/任何指针。
谢谢你。