How to convert HTML to XHTML conversion to generate closed tags in iText C#

独自空忆成欢 提交于 2019-12-23 05:41:32

问题


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

来源:https://stackoverflow.com/questions/50558712/how-to-convert-html-to-xhtml-conversion-to-generate-closed-tags-in-itext-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!