有特殊字符iTextSharp的 [英] iTextSharp with special characters

查看:433
本文介绍了有特殊字符iTextSharp的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有哪些用户需要一个调查,调查被保存到数据库中一个aspx形式。保存到数据库后,由用户提供的调查答复显示,使用户可以保存响应为PDF。

I have an aspx form in which user takes a survey and the survey is saved to the database. After saving to the database, the survey responses given by the user are displayed so the user can save the response as pdf.

这是保存为PDF格式功能

This is the function to save as 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因为它无法解析这些符号抛出了一个错误。

It works fine and everything is well. The form has some text boxes. The user can enter text into the textbox. Sometimes, the user enters mathematical symbols like <, >, =. To make sure that the textbox value is saved to the database, the following ValidateRequest="false" has been done in the aspx file. But, the SaveAsPDF throws up an error as it is unable to parse these symbols.

我是pretty确保它主要是造成这一问题的这些特殊符号

I am pretty sure it is mostly these special symbols causing the issue with the

htmlparser.Parse(SR)

,因为如果没有特殊符号中的文本框,则生成的PDF细

because if there are no special symbols in the text boxes, PDF is generated fine.

你能不能帮我出去我能做些什么?/任何指针。

Could you help me out on what I can do?/ Any pointers.

感谢您。

推荐答案

您应该先带code输入文本通过处理文本和执行之前,使用的 HttpUtility.HtmlEn code 保存。

You should first encode the input text by using HttpUtility.HtmlEncode before processing the text and performing the save.

http://msdn.microsoft.com/en-us/library/ 73z22y6h.aspx

这篇关于有特殊字符iTextSharp的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆