简体   繁体   English

Itext7 java 如何在 IElement 上覆盖 trimFirst()

[英]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).我想覆盖由 HtmlConverter 返回的 IElement 的 trimfirst () 方法(似乎 HtmlConverter 类消除了空格)。

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 .在另一种情况下,我使用了这个解决方案https://stackoverflow.com/a/68935671/18323778

How can I adopt a solution like this on an IElement Object.如何在 IElement Object 上采用这样的解决方案。 Also is it the trimFirst () method that you think removes whitespace?也是您认为删除空格的 trimFirst () 方法吗? Or there is some other method that removes the whitespace?还是有其他方法可以删除空格?

Use white-space: pre-wrap;使用white-space: pre-wrap; CSS property declaration to preserve your whitespaces and avoid collapsing while still wrapping your text over the lines: CSS 属性声明以保留空格并避免折叠,同时仍将文本换行:

<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: PDF 中的视觉结果:

结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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