Search code examples
javaitextitext7

Itext7 java how to Override trimFirst() on IElement


I would like to Override the trimfirst () method of an IElement returned by HtmlConverter (it appears that whitespace is eliminated by the HtmlConverter class).

Assuming you have a string like this:

Hello                  my name is Roger !

after running these lines of code :

List<IElement> lst = HtmlConverter.convertToElements(stringBuilder.toString(),cvProp);
           
for (IElement tagParag : lst) {
    p = (Paragraph) tagParag;
}

The result of text is this :

Hello my name is Roger!

This is the string before passing by the converter :

<p style='font-family:times-roman;font-size:10.5pt'>dalla notifica del presente atto di ingiunzione l'importo dovuto per complessivi Euro                             come qui di seguito specificati:</p>

What happened to the white spaces? There should have been some trim.

In another case I had used this solution https://stackoverflow.com/a/68935671/18323778.

How can I adopt a solution like this on an IElement Object. Also is it the trimFirst () method that you think removes whitespace? Or there is some other method that removes the whitespace?


Solution

  • Use white-space: pre-wrap; CSS property declaration to preserve your whitespaces and avoid collapsing while still wrapping your text over the lines:

    <p style='font-family:times-roman; white-space: pre-wrap; font-size:10.5pt'>dalla notifica del presente atto di ingiunzione l'importo dovuto per complessivi Euro                             come qui di seguito specificati:</p>
    

    Visual result in the PDF:

    result