0

I need to convert a String to fill an element of itext like a paragraph. I do not want to create a new document on the conversion, I just want it to receive a string and return something I could add as part of a pdf document. An example of the input I need to convert is:

<h1>Hello<h1></br>
<h3>This is a test to demonstrate a simple html code I just need to convert</h3>
mkl
  • 90,588
  • 15
  • 125
  • 265
Bryan Mora
  • 11
  • 5

1 Answers1

0

You can parse your HTML string to elements and add these elements to a paragraph (by the way, I repaired your html string for the tests):

String html = "<h1>Hello</h1><br/>\n" + 
        "<h3>This is a test to demonstrate a simple html code I just need to convert</h3>";
String css = null;
ElementList elements = XMLWorkerHelper.parseToElementList(html, css);
Paragraph paragraph = new Paragraph();
for (Element element : elements) {
    paragraph.add(element);
}

(from CreatePdf test testHtmlToParagraph)

After adding that paragraph to an iText Document you get:

screen shot

With a slightly more interesting css:

String css = "h1 { background-color: lightblue; font-size: 20pt} h3 {font-family: verdana; text-align: center; color: red;}";

you get

screen shot

mkl
  • 90,588
  • 15
  • 125
  • 265
  • ¿what versions of itext and xmlworker did you use?, because i get an error with itext 5.5.13 and xmlworker 5.5.13 when i use your method....Caused by: java.lang.NoSuchMethodError: com.itextpdf.tool.xml.html.pdfelement.NoNewLineParagraph.setMultipliedLeading(F)V at com.itextpdf.tool.xml.html.AbstractTagProcessor.currentContentToParagraph(AbstractTagProcessor.java:333) at com.itextpdf.tool.xml.html.Span.end(Span.java:76) at – Bryan Mora Apr 04 '19 at 15:20
  • I originally used the current 5.5.14-SNAPSHOT. After your comment, though, I switched to 5.5.13, but even with that version the code ran without issue. Concerning the method in question, by the way, the `setMultipliedLeading` method has not been changed since 5.4.5. Thus, I assume you additionally have a pre-5.4.5 version of itext or xmlworker in your classpath which is seen first by Java. – mkl Apr 04 '19 at 15:41
  • By the way, if you run across `NoSuchMethodError` outputs, you can count on your pre-compiled dependencies having non-matching versions. – mkl Apr 04 '19 at 15:43
  • Maybe I had a dependecies problem, but I still cant make it work. I get the error: Caused by: java.lang.NoSuchMethodError: com.itextpdf.tool.xml.XMLWorkerHelper.parseToElementList(Ljava/lang/String;Ljava/lang/String;)Lcom/itextpdf/tool/xml/ElementList; – Bryan Mora Apr 05 '19 at 15:05
  • com.itextpdf itextpdf ${itext.version} com.itextpdf itext-pdfa ${itext.version} com.itextpdf itext-xtra ${itext.version} – Bryan Mora Apr 05 '19 at 15:06
  • By the way I have a jee project where Im using jsf 2.2 y primefaces 6.2 – Bryan Mora Apr 05 '19 at 15:07
  • Do you have only those dependencies or others, too? – mkl Apr 06 '19 at 18:49
  • i also have: org.primefaces primefaces 6.2 javax.faces javax.faces-api 2.2 provided – Bryan Mora Apr 08 '19 at 14:55
  • com.itextpdf.tool xmlworker 5.5.13 jar javax javaee-web-api 7.0 provided – Bryan Mora Apr 08 '19 at 14:55
  • org.projectlombok lombok 1.18.6 provided – Bryan Mora Apr 08 '19 at 14:55
  • Both Maven itself and most IDEs have features that allow to determine the effective dependencies you have and the cause for each of them, try those. Furthermore, you might have unexpected libraries in your JRE's lib folders or in some endorsed library folder. All I can say is that with matching, unmanipulated itext and xmlworker versions one does not get the `NoSuchMethodError`. – mkl Apr 14 '19 at 07:49