0

I have a sharepoint list column which contains rich text and I used Itextsharp to convert the content of the column to a pdf.

The problem is that html tags are showing up in the pdf when I open it. How to remove them. I am pasting the code I used below, Please suggest necessary changes.

               foreach(SPListItem oitem in oListCollection)
                    {
                        string _strPDF = Convert.ToString(oitem["Description"]);
                        string _strPDFTitle = Convert.ToString(oitem["Title"]);
                        FileStream fs = new FileStream(@"C:\Users\Administrator\" +  _strPDFTitle + ".pdf", FileMode.Create, FileAccess.Write, FileShare.None);

                        Document doc = new Document();
                        PdfWriter oPdfwriter = PdfWriter.GetInstance(doc, fs);
                        doc.Open();
                        doc.Add(new Paragraph(_strPDF));
                        doc.Close();
                    }
1

I'm not completely convinced this is a 100% SharePoint question, but a more general How to remove HTML tags in iTextSharp?, the query "itextsharp remove html" seems to be a popular on Google.

But that aside I would suggest you use HtmlAgilityPack to parse the markup, and then call htmldoc.DocumentNode.InnerText as used here.

Also I hope you have noticed the license on iTextSharp, I can't comment on it myself, but I know there are a lot of discussions about it. There exists alternatives as well.

|improve this answer|||||
0

Is your Description field a plain text field, or one of the formatted types?

See HTML from Rich Text Field as Plain Text to strip formatting and get the value as plain text.

Also, you do not need to call Convert.ToString() on Title and Description since both are already string

|improve this answer|||||
  • The link you gave is for Microsoft.SharePoint.Client, and not SSOM as OP has. But I believe one can do something like list.Fields.GetFieldByInternalName("Description").GetFieldValueAsText(oItem) – eirikb Apr 28 '14 at 5:40
  • Uh. Right. It's late :P But it comes down to just formatting strings as you suggested, not really specific to SharePoint – Louis Apr 28 '14 at 5:50
  • Well, perhaps, but the GetFieldValueAsText is a SharePoint-specific method. If you change your answer to use that I will upvote you and so you would have the correct answer. I will not delete mine as HtmlAgilityPack can make it easier to do more complicated alterations beside just html->plain string. – eirikb Apr 28 '14 at 5:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.