Indentation in generated PDF using JasperReports - jasper-reports

I have a piece of HTML stored in a database as:
<ul>
<li>Pretend you're talking to a busy colleague and have to sum up your entire question in one sentence: what details can you include that will help someone identify and solve your problem?</li>
<li>Spelling, grammar and punctuation are important! Remember, this is the first part of your question others will see - you want to make a good impression. If you're not comfortable writing in English, ask a friend to proof-read it for you. </li>
<li>If you're having trouble summarizing the problem, write the title last - sometimes writing the rest of the question first can make it easier to describe the problem. </li>
</ul>
I am displaying this piece of HTML in a PDF using a JasperReports textField, the above HTML should display like this in the generated PDF.
Pretend you're talking to a busy colleague and have to sum up your entire question in one sentence: what details can you include that will help someone identify and solve your problem?
Spelling, grammar and punctuation are important! Remember, this is the first part of your question others will see - you want to make a good impression. If you're not comfortable writing in English, ask a friend to proof-read it for you.
If you're having trouble summarizing the problem, write the title last - sometimes writing the rest of the question first can make it easier to describe the problem.
But this HTML is showing as :
The snippet from jrxml file:
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement positionType="Float" x="7" y="47" width="501" height="15" isRemoveLineWhenBlank="true" forecolor="#283234"/>
<textElement markup="html">
<font size="10"/>
</textElement>
<textFieldExpression><![CDATA[$F{description}]]></textFieldExpression>
</textField>
HTML is feeded in description variable.
Any idea how can I align text?

My solution shows the plain JRXML which is the desired result independent from the tools someone is using, e.g. iReport GUI, dynamic reports or java code designing Jasper reports.
First define a style, which corrects the indentation pulling the first line some pixels to the left and pushes the whole box the same width to the right:
<style name="hanging-indentation-style">
<box leftPadding="23"/>
<paragraph firstLineIndent="-23"/>
</style>
Second, this style is applied to the reportElement of the textField:
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement style="hanging-indentation-style" positionType="Float" mode="Transparent" x="0" y="0" width="555" height="20" isRemoveLineWhenBlank="true"/>
<textElement markup="html"/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{description}]]></textFieldExpression>
</textField>
Depending on your font size you may vary the style values to fit your needs.
I adapted input from Aligning Bullets in Jasper Reports, where dynamic reports api is used, and Jasper Report HTML bullet hanging indent, where it is shown through the GUI, which was not possible in my case using iReport Designer 4.5.1, because there is no option to apply padding directly to a textField.

Related

How to styling a static text partially according to a global style in a Jasper Report

Question
I have a simple text field like this:
<staticText>
<reportElement x="0" y="0" width="120" height="10" />
<text>Hello world!</text>
</staticText>
Also in my document I have a style like this:
<style name="highlight" forecolor="#FF3333" />
How can I apply the style highlight to just the word "world" in my text field?
What I have tried so far
I checked whether I can just use multiple text fields (in this case three, one for "Hello ", one for "world" and one for "!"). In this case I could just use <reportElement ... style="highlight" /> on the element for "world". But it looks like there is no way to align the three text field vertically automatically. Since in my case the text is static I could of course align the text fields manually, however this is quite cumbersome to do and as soon as someone wants to change the font or the font size or exchange just a single letter in the text I need to realign everything.
Another option is to use styled text. This actually works:
<textField>
<reportElement x="0" y="0" width="120" height="10" />
<textElement markup="styled" />
<textFieldExpression><![CDATA[
"Hello <style forecolor='#FF3333'>world</style>!"
]]></textFieldExpression>
</textField>
This works fine by itself, but I cannot refer to my highlight-style - I have to repeat the color here which is not maintenance friendly either.

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.

How to relocate summaryband in jasperstudio

I am appending the contentn of my table of content like here: Append an In-Report ToC
Now I have my TOC at the summary band. How can I move it to the second page? Is there any better way to create table of content in jasperstudio?
As recommended in coments i used jasper book. The problem is in the table of content i get dots instead of real text which should be there.
but as you can seen in the picture by hovering the mouse on the dots the real text is shown although without page number!
Here is the XML generated with jasperstudio:
<textField evaluationTime="Group" evaluationGroup="id" hyperlinkType="Reference" bookmarkLevel="2">
<reportElement isPrintRepeatedValues="false" x="-4" y="37" width="534" height="20" printWhenGroupChanges="id" uuid="f6d9376f-7106-4292-b491-7229b297ce04"/>
<textElement markup="html"/>
<textFieldExpression><![CDATA[$F{id}.toString()}]]></textFieldExpression>
<anchorNameExpression><![CDATA[$F{id}.toString()]]></anchorNameExpression>
<hyperlinkReferenceExpression><![CDATA[$F{id}.toString()]]></hyperlinkReferenceExpression>
<hyperlinkTooltipExpression><![CDATA[$F{id}.toString()]]></hyperlinkTooltipExpression>
</textField>

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 create a multi language report?

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.