I am using class iTextSharp.tool.xml.XMLWorkerHelper to add text in HTML format into a pdf file in a ASP .NET C# project. Here's my code :
Paragraph bookingTemplateDescriptionContent = new Paragraph(); desc.Content = desc.Content.Replace("</br>", "<br/>"); //HACK : To avoid exception, replace the br tag in HTML formatted to XHTML formatted ElementList list = XMLWorkerHelper.ParseToElementList(desc.Content, null); foreach (var element in list) { foreach (Chunk chunk in element.Chunks) { chunk.Font = bookingTemplateDescFont; } bookingTemplateDescriptionContent.Add(element); }
But recently I got an exception : Invalid nested tag p found, expected closing tag br.
. After googling I found that the exception is happen because there are a nested tag that does not have a valid closing tag. In my case the tag is <p>
. I know I can do edit the HTML string manually in the database or in the application. But I think there are a better way to solve this. After googling, I found that the problem can be solved by convert the HTML text into XHTML. My question is, is there any recommendation nuGet packages that I can use to convert the HTML text into XHTML before the HTML is written into the PDF file ? Something like JSoup
in Java. Any recommendation is welcome. Thank you. I know there are some tools like HTML2XML
but I want to know if there are any alternative tools or other recommendation tools beside HTML2XML