Recently, I encountered an issue in exporting a PDF format report using Jasper API in which long text was not coming properly in the report. I had to spent quite a some time to resolve the issue. Solution is very simple and worked for me perfectly fine. So, I thought of sharing the same with you all.
Text fields have dynamic content, most of the time you can’t anticipate the exact amount of space to provide for them. If the space you reserve for your text fields is not sufficient, the text content is truncated so that it fits into the available area.
This scenario is not always acceptable, so you can let the reporting engine calculate the amount of space required to display the entire content of the text field at run-time, and automatically adjust the size of the report element. To do this, set “
isStretchWithOverflow” to true for the particular text field elements you are interested in. By doing this, you’ll ensure that if the specified height for the text field is not sufficient, it will automatically be increased in order to be able to display the entire text content.
This might work fine in case of export format like RTF, HTML or XLS etc., however, in case of PDF export, it might happen that word wrap does not work properly or portions of text to disappear at the end of longer text paragraphs. In the Figure-1, you can see that the generated report (in PDF format) is looking distorted –
To resolve this issue, the following jasper report configuration property –
net.sf.jasperreports.export.pdf.force.linebreak.policy need to set to
true as follows in the JRXML file –
<property name="net.sf.jasperreports.export.pdf.force.linebreak.policy" value="true"/>
To set the property through iReport, follow the steps mentioned below –
1. Right Click on the report in the “Report Inspector” panel and select the “Properties” option in the opened context menu as shown in Figure-2
2. Report properties dialog box will appear. Again, select “Properties” and click on the small button next to it in right side. This will pop up the configuration properties dialog as shown in Figure-3
3. Click on Add button. This will pop up a dialog box where you can add a new property and its value as shown in Figure-4.
4. Click OK button to save the newly added property. You can see the newly added property in the report configuration property dialog as shown in Figure-5.
5. Click the OK button.
Regenerate the report with the updated report template. This will resolve the word wrap/ line break issue and now the generated report should look as shown in Figure-6 which earlier was looking as shown in Figure-1