0

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?

Alexey Subach
  • 11,903
  • 7
  • 34
  • 60
DropVid
  • 35
  • 8
  • You are starting with HTML (otherwise you wouldn't use the `HtmlConverter`) and HTML already implies that multiple space characters collapse to a single one for displaying, doesn't it? – mkl Aug 24 '22 at 13:35
  • Please tag the language of your code for better question discoverability. – slindenau Aug 24 '22 at 14:02
  • Oh my god it's true !!! I copied the piece of hml in an .html file, in fact the browser eliminates the spaces, as you say. So the problem is hmtl ... solutions?!?! – DropVid Aug 24 '22 at 14:03
  • I tried using the ' ' for tabulator. In the browser it works but Itext HtmlConverter ignores this. – DropVid Aug 24 '22 at 14:14
  • *"So the problem is hmtl ... solutions?!?!"* - well, I would have voted for not using HTML to start with. Using Alexey's answer to enforce a different interpretation of consecutive spaces is easier for you to use, though. ;) – mkl Aug 24 '22 at 14:58

1 Answers1

1

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

Alexey Subach
  • 11,903
  • 7
  • 34
  • 60
  • Thanks Alexey Subach your answer is the right solution. I changed the code and it works great. So the problem is not Itext .... I apologize to the developers :-) – DropVid Aug 24 '22 at 14:27