Creating reports using Jasper, I had a requirement to add a stack trace into the report if something failed. The problem I ran into was that the data in a stack trace tends to be really long with no spaces. The Jasper report would not display the data that went outside of the confines of the document and it couldn't wrap it without any spaces in it.
A quick and easy solution is to insert unicode Zero Width Space between each character so that Jasper can wrap it where ever it wants..
/**Insert zero width space in to a string so that solid words (like stack trace will wrap nicely
*
* @param string
* @return
*/
public static String addZeroWidthSpace(String string) {
char[] chars = string.toCharArray();
StringBuilder builder = new StringBuilder(string.substring(0, 1));
for (int i = 1; i < chars.length; i++) {
builder.append("\u200B");
builder.append(chars[i]);
}
return builder.toString();
}
Pass your string through this method and turn Jasper Field markup to HTML and allow the field to wrap.
Happy Coding.
No comments:
Post a Comment