Search code examples
asp.netasp.net-mvcitextpdf-generationhtml-target

Convert html tagged text to string for a pdf itext7


I am working on a project in asp.net MVC which involves printing various information that the user enters previously.

I use the TinyMCE for comment type fields. As we know Tiny allows you to format text using different types of fonts, adding bold words, etc. The string it produces will have tags that allow reading the style chosen for the word or phrase in HTML.

I use Itext7 to produce pdfs, when I print the fields I mentioned above, obviously all the HTML tags are shown, for example:

<p> Hello World! </p>

Is there a way to tell Itext7 that when these tags are present, it must use an associated style for that tag?


Solution

  • I created "Hello World" here and used bold, italic and underline. Copy the source code via "Tools" and just use the following code to convert it via iText7:

    String htmlString = "<p><span style=\"text-decoration: underline;\"><em><strong>Hello World</strong></em></span></p>";
    HtmlConverter.convertToPdf(htmlString, new PdfWriter(destinationFolder + "test.pdf"));
    

    The resultanting PDF:

    enter image description here