简体   繁体   中英

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?

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:

结果

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM