How to create a multi language report? - jasper-reports

I want to create a document, which prints in more than one language, based on the Locale.
I have created 2 resource bundles, one in English and one in Chinese, but I am not sure how to use them.

Here is the sample of how to implement internationalization support for JasperReports.
The main idea is to use special expression $R{} for localizing text and images.
The sample for images:
<image scaleImage="Clip">
<reportElement positionType="Float" x="20" y="20" width="100" height="50"/>
<imageExpression class="java.lang.String"><![CDATA[$R{image.flag}]]></imageExpression>
</image>
The samples for text (the $R{} syntax):
<textField isBlankWhenNull="true">
<reportElement x="20" y="100" width="530" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$R{sampleString}]]></textFieldExpression>
</textField>
or (the msg() method):
text.message=The program picked up {0} as a random number.
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement x="20" y="210" width="530" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[msg($R{text.message}, $P{number})]]></textFieldExpression>
</textField>

Since the document generator may be part of your application, you should somewhere have a language selector menu-item, check-box or combo-box which is already preselected.
So, why don't you just add an if statement that reads the Locale, or the needed language before the report generation, and load the appropriate report accordingly to the locale.
This way you will need to keep one jrxml file for every language. It will be fairly easy to just translate the headers and labels manually.
Your data should be already translated in your database, where you have to keep the relevant attribute values multilingual anyway.
You will need to modify the SQL query for the appropriate language, but since the Query is part of your jrxml it will be executed automatically.

Related

Jasper Report assign a variable value from a DTO to the backcolor tag [duplicate]

I am using Jasper Report for designing report. I have a report where there is a static text. I want to set the background color of it by parameter value. In the XML it generates for the text like this:
<reportElement mode="Opaque" x="434" y="0" width="121" height="12" backcolor="#A6A6A6" uuid="e088bd9f-a0ac-4f34-9375-df765c829ec2"/>.
Now I need to set the backcolor from a parameter which will come from database. for here like #A6A6A6.
What can I try next? I have researched it, but no luck.
You can do that with net.sf.jasperreports.style.* element level properties (for which you can have expressions as values).
In your case you would need
<textField>
<reportElement ...>
<propertyExpression name="net.sf.jasperreports.style.backcolor">$P{someColor}</propertyExpression>
</reportElement>
...

How to specify custom units format in pattern when exporting to excel?

Using jasper reports I want to generate an excel report that has the option of having units/currencies in the column.
Like column 1: 5,74 €/h and column 2: 24,7 kg.
In excel I can enter this pattern as #.##0,00" €/h"
However using same pattern in jrxml
<textField isBlankWhenNull="true">
<reportElement height="125" style="detail">
<property name="net.sf.jasperreports.export.xls.pattern" value="#,##0" €/h""/>
</reportElement>
<textFieldExpression><![CDATA[$F{attributeWithUnit}]]></textFieldExpression>
</textField>
does not work, excel cannot understand it and therefore deformate the whole column or sheet and gives error.
How can/should I format this jasper export for excel to understand how to format it?
The issue you have is that to the export engine you actually need to pass #,##0" €/h" and not #,##0" €/h" and since attributes is parsed it's converted to #,##0" €/h"
before you start be sure to enable xls.detect.cell.type
<property name="net.sf.jasperreports.export.xls.detect.cell.type" value="true"/>
and make sure you have a font that can display the Euro symbol correctly.
With jasper-reports versione 6.4 o above
Use the CDATA tag
<textField>
<reportElement x="0" y="0" width="180" height="30" uuid="a5698aa9-a36e-444d-b590-3340250059a1">
<property name="net.sf.jasperreports.export.xls.pattern">
<![CDATA[#,##0.0" €/h"]]>
</property>
</reportElement>
<textFieldExpression><![CDATA[1235]]></textFieldExpression>
</textField>
Versione previous to 6.4
You need to do escape the & so it is correctly & when passed to export hence the format string becomes #,##0.0&quot; €/h&quot;
<textField>
<reportElement x="0" y="0" width="180" height="30" uuid="a5698aa9-a36e-444d-b590-3340250059a1">
<property name="net.sf.jasperreports.export.xls.pattern" value="#,##0.0&quot; €/h&quot;"/>
</reportElement>
<textFieldExpression><![CDATA[1236]]></textFieldExpression>
</textField>

What happens if I have same duplicate UUID in report?

In my jasper report, I am using elements something like this:
<textField isStretchWithOverflow="true">
<reportElement style="alternateStyle" positionType="Float" stretchType="RelativeToTallestObject" x="292" y="0" width="85" height="30" uuid="b93b4e99-d6fb-4679-836d-9b198cb5fe1c"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="SMCustomFont" size="9" isBold="false"/>
<paragraph leftIndent="3"/>
</textElement>
<textFieldExpression><![CDATA[($F{SOMEEXP}]]></textFieldExpression>
</textField>
Here the uuid is "b93b4e99-d6fb-4679-836d-9b198cb5fe1c".
My question is What happens if I keep the UUID same for some other textField element in the same JRXML file and what is the significance of UUID?
When I tried, the report is generating fine.
Jasper reports use the UUID in the engine to identify elements, see for example SortElementJsonHandler.getCurrentSortField.
A Friendly User (staff) at the Jasper Community forum states
UUID is used mostly for the interactive elements like tables and crosstabs to identify the elements for sorting/filtering/etc. It's used in the viewer and on the engine level, I am not sure there is useful API available to interact with elements using UUID.
Also as far as I can see, in most simple cases like two textField there is no problem having same UUID, but you can get strange result if you have a more complex design with multiple similar components and all of them have same UUID
Use the IDE and you don't need to worry about it.
If you copy and past elements/components in the raw jrxml, either change a number or remove the UUID, when saved in JasperSoft Studio it will auto-generate it for you if it does not exists. Overall I would suggest that you use the IDE for all actions (copy and past etc), personally I only use the code/jrxml view if I need to do some quick replace of something etc.

Removing trailing 0 xml Ireport

I'm currently having trouble when producing an iReport, when I run the report it's giving me a weight of 29000.0kg but I require just 29000kg. I've tried different formats but I understand it might need changing in XML.
If anyone could point me in the right direction or assist me in changing this please comments
Currently this is the XML code that is against fields:
<textFieldExpression><![CDATA[$F{EquipmentTareWeightKg} +"kg"]]></textFieldExpression>
Any ideas on how to include this?
I've figured this out, it doesn't require hardcoding but merely changed in the properties window within ireport designer, however if you see below, you change the pattern value to remove decimal places:
</textElement>
<textFieldExpression><![CDATA[$F{EquipmentLengthMm} /1000]]></textFieldExpression>
</textField>
<textField pattern="###0" isBlankWhenNull="true">
<reportElement x="399" y="0" width="32" height="20"/>
<textElement>
<font size="12"/>
</textElement>

How to prevent repeating data in JasperReports TextField

I am using JasperReports and have some problem with textField data continue on next page.
I have 3 textField in detail band.
Band splitType="Stretch".
Every textfiled has borders and isPrintWhenDetailOverflows param set "true".
When data in textfield does not fit on page it continues on next. So i need to print other textfileds of same band (because i need to print borders of every textfield)
But isPrintWhenDetailOverflows="true also leads to repeating data in other textfields on second page though their contents fits on first page.
Illustration:
The jasper report documentation says:
isPrintWhenDetailOverflows()
If this is set to true, the element will be reprinted on the next page if the band does not fit in the current page.
So I think it might be a problem. Remove isPrintWhenDetailOverflows="true". The two things isStretchWithOverflow="true" and stretchType="RelativeToTallestObject" are working for me in this case. So your textField will be like:
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement style="cell" stretchType="RelativeToTallestObject" x="266" y="0" width="266" height="15"/>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font fontName="Arial" size="9" isBold="false" pdfEncoding="Cp1251" isPdfEmbedded="true"/>
<paragraph leftIndent="2"/>
</textElement>
<textFieldExpression><![CDATA[$F{structure}]]></textFieldExpression>
</textField>