JasperReports: Prevent textField to split on space or hyphen

霸气de小男生 提交于 2020-02-15 05:23:51

问题


I have a JasperReports template that contains a textField element that will contain a variable length of strings. If the string is too long to fit the width, it'll be split which is fine except when the string contains a hyphen or a space char. In this case the string is split from that char. Below are some example of what's the input, observed outcome and wanted outcome + summary to make my point easier to understand.

input      observed   wanted   summary
_____________________________________
overflow  -> overfl -> overfl : OK
over flow -> over   -> over f : NOT OK
over-flow -> over-  -> over-f : NOT OK

Setting the reportElement to have isStretchWithOverflow="true" attribute will split the text on two lines, but this is not wanted behaviour.

Is there anyway to fix this?

Thanks.

EDIT: The input data comes from an external source, so I cannot directly change that. I ran some tests and noticed that using non-breaking space will do for spaces. non-breaking hyphen on the other hand is not printed at all, i.e. text 'over-flow' becomes 'overflow'. Not quite what is wanted.

Despite the input source not in my control, I could fix this problem by writing a Scriptlet that'll change spaces to non-breaking spaces and hyphens to non-breaking hyphens, only if those darn non-breaking hyphens would be printed.

Printing to PDF by the way, in case that gives some hints of the problem.


回答1:


Type in the Text field ''Expression'' like this:

String.join("\uFEFF", $F{field1}.split("(?!^)"))



回答2:


It's a font issue. Non-breaking hyphen works(\u2011) fine, when the font supports it. See fonts sample on how load other than the default font (DejaVu Sans for example).

If anyone has a better option for the input source modification than the Scriptlet, please let me know.

(Marking my own answer as correct in order to get this topic closed.)

EDIT: Have to wait for two days in order to mark this answer correct.



来源:https://stackoverflow.com/questions/12312899/jasperreports-prevent-textfield-to-split-on-space-or-hyphen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!