How can I replace a character with an unicode imag

2019-04-13 08:47发布

问题:

How can I replace a - character in a text which comes from a data base into jasper field, with an image? The goal is as the illustration bellow:

Something like: $F{KN_Zusatzinfo_DV_Einleitungstext}.replaceAll("- ", "[\\x254]") where the x254 is ascii code for a red square.

But the above code writes the ascii code as is in the text and does not produce the image.

回答1:

The correct syntax to render a unicode symbol is \uXXXX

For example this expression:

$F{listItem}.replaceAll("-", "\u2588")

Will render this

Now you like the dot to be red so we need to apply some style, lets set markup="html" on textElement and change the replace to this

$F{listItem}.replaceAll("-", "<font color=\"red\">\u2588</font>")

It will render:

Note: You need to be careful with regex in replaceAll, I probably would us ^-, hence starts with - (to avoid replacing other - in text), furthermore the normal way would be to just add a red rectangle element on each row. Also take care of font-extensions if you are exporting to pdf, so that your font is render correctly