hncl
hncl

Reputation: 2305

Error caused by special characters in ITextSharp

I have been using itextsharp, it worked well until I changed the editor to Telerik MVC editor, if I enter special characters such as < or > I get the following error:

Object reference not set to an instance of an object.

The way my program works is to read a list of recommendations from SQL Server table, using the following script:

string BPRecommendation = "<span style='font-size:10;'>";
for (int i = 0; i < this.selectedVisit.Recommendations.Count; i++)
{
    if (i > 0) BPRecommendation += "<br />";
    BPRecommendation += this.selectedVisit.Recommendations[i].FullName + 
        " (" + this.selectedVisit.Recommendations[i].UserType + "):<br />";
    BPRecommendation += this.selectedVisit.Recommendations[i].Comments + 
        ".<br /><br />";
}
BPRecommendation += "</span>";

List<IElement> htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker
    .ParseToList(new StringReader(BPRecommendation), null);

//add the collection to the document
for (int k = 0; k < htmlarraylist.Count; k++)
{
    paragraph.Add((IElement)htmlarraylist[k]);
}
doc.Add(paragraph);

Upvotes: 0

Views: 1114

Answers (1)

Akos Lukacs
Akos Lukacs

Reputation: 2047

Use HtmlEncoding.

if (i > 0) BPRecommendation += "<br />";
BPRecommendation += HttpUtility.HtmlEncode(this.selectedVisit.Recommendations[i].FullName) + 
    " (" + HttpUtility.HtmlEncode(this.selectedVisit.Recommendations[i].UserType) + "):<br />";
BPRecommendation += HttpUtility.HtmlEncode(this.selectedVisit.Recommendations[i].Comments) + 
    ".<br /><br />";

Upvotes: 1

Related Questions