Adding Word Wrap Javascript Function to BIRT

On IBM developerWorks site, a forum question was asked about how to wrap a text field in BIRT that does not contain any spaces.

I would like to know how to wrap data for a field in BIRT report , the wrapping has been set to AUTO . I tried to use the below function in expression in data binding of that particular field. by max072014

I game a quick answer of just using a Dynamic Text field, but realized later that day that wouldn't work. I had run into a similar case and instead of wrapping the text, I would trim the extended text gracefully. Poking around the inter webs a bit, I found a similar solution on the Actuate forums - How do I word wrap in BIRT.

The original javascript was good, but incomplete. In the event a value didn't exist for a given field, the javascript would flake out. So now if the field is empty (null), BIRT will print an empty string. Otherwise the report will wrap the field as expected. 

Enter wrapped element in BIRT

  1. Make sure the field that needs to be wrapped exists in your Dataset. In this example I'm using a field labeled description.
  2. Insert a new Data element and set the datatype to string.
  3. Paste the following code into the expression builder for the element.

    if (dataSetRow["description"]== null) { '' } 
    else {
        function wrap(longStr,width)
        { length = longStr.length; if(length <= width) return longStr; return (longStr.substring(0, width) 
        + "\n" + wrap(longStr.substring(width, length), width)); } 
        + wrap( dataSetRow["description"], 15 );
  4. Save the report and preview it.

The field will now wrap at every 15 characters. Adjust the integer value on the last line to make the wrap shorter or longer.