Date formatting part of text in Jasper / ireports - jasper-reports

how do I format a date field (database field) displayed part of a text in Jasper / iReports (4.5.1)
Displayed via a text field in the report... (using Groovy please)
"Sub total for this date: " + $F(DEPOSIT_DATE)
I have tried (new SimpleDateFormat("MM/dd/yyyy")).parse($F{DEPOSIT_DATE})and I am getting error message:
net.sf.jasperreports.engine.fill.JRExpressionEvalException: 
Error evaluating expression : Source text : (new SimpleDateFormat("MM/dd/yyyy")).parse($F{BANK_DATE}) 
What I want to display in my report is as follows...
Sub total for this date: MM/DD/YYYY - format...

Try this:
new SimpleDateFormat("MM/dd/yyyy").format($F{BANK_DATE})

I agree with Mateusz, textField with pattern perfrormes faster than new SimpleDateFormat("someFormat").format("jasperField"). It's important when you have deal with huge reports. This is my example
<textField pattern="MM/dd/yyyy" isBlankWhenNull="true">
...
<textFieldExpression class="java.util.Date"><![CDATA[$F{certIssueDate}]]></textFieldExpression>
</textField>

It seems, that you try to parse instead of formatting (as stated above).
You could also use Pattern in the textfield properties tab to pretty-print the date, or manually change the pattern in the jrxml:
<textField pattern="MM/dd/yyyy">
<!-- here comes other generated data-->
<textFieldExpression><![CDATA[$F{BANK_DATE}]]></textFieldExpression>
</textField>

if the Date field is a String value, say : "2014-11-20"
<field name="dateField" class="java.lang.String"/>
then you can do this
<variable name="THE_DATE" class="java.util.Date">
<variableExpression>
<![CDATA[new java.text.SimpleDateFormat("yyyy-mm-dd").parse($F{dateField})]]>
</variableExpression>
</variable>
<textField pattern="dd/MM/yyyy" isBlankWhenNull="true">
<reportElement x="0" y="0" width="88" height="20" uuid="47b41787-a8fd-44ea-bf96-7e5484e477fb"/>
<textElement textAlignment="Left" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[ $V{THE_DATE} ]]></textFieldExpression>
</textField>
You can set the pattern by
right clicking the field -> click field pattern -> Select Date -> Choose a Date Pattern
you may also do this
<textField isBlankWhenNull="true">
<reportElement x="0" y="0" width="88" height="20" uuid="47b41787-a8fd-44ea-bf96-7e5484e477fb"/>
<textElement textAlignment="Left" verticalAlignment="Middle"/>
<textFieldExpression class="java.util.Date"><![CDATA[ new java.text.SimpleDateFormat("dd/MM/yyyy").format(new java.text.SimpleDateFormat("yyyy-mm-dd").parse($F{dateField})) ]]></textFieldExpression>
</textField>
However,If the DateField is of type Date
then doing the below is just fine.
<field name="dateField" class="java.util.Date"/>
<textField pattern="dd/MM/yyyy" isBlankWhenNull="true">
<reportElement x="0" y="0" width="88" height="20" uuid="47b41787-a8fd-44ea-bf96-7e5484e477fb"/>
<textElement textAlignment="Left" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[ $F{dateField} ]]></textFieldExpression>
</textField>

Related

How to combine two textFields into one box in RTF generated by Jasper reports?

I use Jasper Reports to generate RTF file.
In RTF each textField is represented as separate box:
I tried to use one frame for textFields:
<frame>
<reportElement positionType="Float" stretchType="RelativeToTallestObject" x="220" y="45" width="406" height="75"/>
<textField>
<reportElement positionType="Float" x="0" y="0" width="406" height="60"/>
<textElement>
<font fontName="Arial" size="8"/>
</textElement>
<textFieldExpression><![CDATA["Address:"]]></textFieldExpression>
</textField>
<textField hyperlinkType="Reference" hyperlinkTarget="Blank">
<reportElement x="0" y="57" width="200" height="15" key="textWithStyle" style="LinkField"/>
<textElement>
<font fontName="Arial" size="8" isUnderline="true"/>
</textElement>
<textFieldExpression><![CDATA["here"]]></textFieldExpression>
<hyperlinkReferenceExpression><![CDATA["http:adress"]]></hyperlinkReferenceExpression>
</textField>
</frame>
But textFields also in the different boxes.
Is it possible to combine several textFields into one box like the following?
I know that I can add each text to one textField, but sometimes I need two separate textFields that I can manage inside one box.

Serial Number Repeating in Jasper Reports

I have used $V{REPORT_COUNT} TO DISPLAY SERIAL NUMBER OF THE RECORDS.For Each Record that is fetched from the database the serial number is displayed correctly.
However if one record that is printed has data more then it can accommodate on a page, it is displayed on the next page. I am fine with that too, however the serial number is again shown for that page Eg. 3 is the serial number and if 3 could not be accommodated on the same page the record is written in the next page and the order of serial number is 1 2 3 (First Page) 3 4 5 etc(second Page).
I want 3 to be displayed only once in the first page and not in the second page. I have uploaded a image of my layout and I do not want to show the circled part I am Using Ireport to design the page
Text Field Properties
This is for the REPORT_COUNT TEXTFIELD
<reportElement
mode="Opaque"
x="0"
y="0"
width="15"
height="30"
backcolor="#D3D3D3"
key="textField-9"
stretchType="RelativeToBandHeight"
positionType="Float"
isPrintRepeatedValues="false"
isRemoveLineWhenBlank="true"
isPrintWhenDetailOverflows="true"/>
<box topBorder="Thin" topBorderColor="#000000" leftBorder="Thin" leftBorderColor="#000000" rightBorder="Thin" rightBorderColor="#000000" bottomBorder="Thin" bottomBorderColor="#000000"/>
<textElement textAlignment="Center" verticalAlignment="Top">
<font fontName="Times New Roman" pdfFontName="Times-Bold" size="10" isBold="true"/>
</textElement>
<textFieldExpression class="java.lang.Integer"><![CDATA[$V{REPORT_COUNT}]]></textFieldExpression>
</textField>
This properties for the right hand side text box which displays data:
<textField isStretchWithOverflow="true" isBlankWhenNull="true" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
mode="Opaque"
x="12"
y="0"
width="523"
height="13"
forecolor="#000000"
backcolor="#D3D3D3"
key="textField-2"
positionType="Float"
isRemoveLineWhenBlank="true"/>
<box topBorder="Thin" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="Thin" rightBorderColor="#000000" bottomBorder="Thin" bottomBorderColor="#000000"/>
<textElement verticalAlignment="Middle">
<font fontName="SansSerif" pdfFontName="Times-Bold" size="8" isBold="true"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[" "+$F{vesselName}+($F{type} == null ? "" : " ("+$F{type}+")")]]></textFieldExpression>
</textField>
Can you just tell us the properties you have set for the text fields?
I have just made a rough design of your scenario and I did not come across your issue. The row_count value is not getting repeated.
Refer below image.
Set a few properties to your variable:
<variable name="RECORD_COUNT" class="java.lang.String" incrementType="Column" calculation="Count">
<initialValueExpression><![CDATA[1]]></initialValueExpression>
</variable>

How to sum all values in a column in Jaspersoft iReport Designer?

I have below similar report in Jaspersoft iReport Designer, but not able to figure out how to sum all values present in vertical column "Doctor Payment" to get a total of "1601"? Length of this columns is variable (i.e. no of rows vary according to size of database & its update frequency ).
Is there any variable like $V{COLUMN_COUNT} (actually gives no of rows, here 5),
which gives sum of all values in a column? If no, how to do sum?
Doctor ID Doctor Payment
A1 123
B1 223
C2 234
D3 678
D1 343
Total 1601
It is quite easy to solve your task. You should create and use a new variable for summing values of the "Doctor Payment" column.
In your case the variable can be declared like this:
<variable name="total" class="java.lang.Integer" calculation="Sum">
<variableExpression><![CDATA[$F{payment}]]></variableExpression>
</variable>
the Calculation type is Sum;
the Reset type is Report;
the Variable expression is $F{payment}, where $F{payment} is the name of a field contains sum (Doctor Payment).
The working example.
CSV datasource:
doctor_id,payment
A1,123
B1,223
C2,234
D3,678
D1,343
The template:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport ...>
<queryString>
<![CDATA[]]>
</queryString>
<field name="doctor_id" class="java.lang.String"/>
<field name="payment" class="java.lang.Integer"/>
<variable name="total" class="java.lang.Integer" calculation="Sum">
<variableExpression><![CDATA[$F{payment}]]></variableExpression>
</variable>
<columnHeader>
<band height="20" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="100" height="20"/>
<box leftPadding="10"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="10" isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Doctor ID]]></text>
</staticText>
<staticText>
<reportElement x="100" y="0" width="100" height="20"/>
<box leftPadding="10"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="10" isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Doctor Payment]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="100" height="20"/>
<box leftPadding="10"/>
<textElement/>
<textFieldExpression><![CDATA[$F{doctor_id}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="100" y="0" width="100" height="20"/>
<box leftPadding="10"/>
<textElement/>
<textFieldExpression><![CDATA[$F{payment}]]></textFieldExpression>
</textField>
</band>
</detail>
<summary>
<band height="20">
<staticText>
<reportElement x="0" y="0" width="100" height="20"/>
<box leftPadding="10"/>
<textElement>
<font isBold="true"/>
</textElement>
<text><![CDATA[Total]]></text>
</staticText>
<textField>
<reportElement x="100" y="0" width="100" height="20"/>
<box leftPadding="10"/>
<textElement>
<font isBold="true" isItalic="true"/>
</textElement>
<textFieldExpression><![CDATA[$V{total}]]></textFieldExpression>
</textField>
</band>
</summary>
</jasperReport>
The result will be:
You can find a lot of info in the JasperReports Ultimate Guide.
iReports Custom Fields for columns
(sum, average, etc)
Right-Click on Variables and click Create Variable
Click on the new variable
a. Notice the properties on the right
Rename the variable accordingly
Change the Value Class Name to the correct Data Type
a. You can search by clicking the 3 dots
Select the correct type of calculation
Change the Expression
a. Click the little icon
b. Select the column you are looking to do the calculation for
c. Click finish
Set Initial Value Expression to 0
Set the increment type to none
Leave Incrementer Factory Class Name blank
Set the Reset Type (usually report)
Drag a new Text Field to stage (Usually in Last Page Footer, or Column Footer)
Double Click the new Text Field
Clear the expression “Text Field”
Select the new variable
Click finish
Put the new text in a desirable position 

Printing serial number in jasper report detail

I have created a jasper report. In that report in detail areaI have "serialNumber" column. That column wants to be auto incrementive and stats with "1". I am using hibernate for query.
Sample code is :
<detail>
<band height="17" splitType="Stretch">
<textField isBlankWhenNull="true">
<reportElement x="12" y="0" width="27" height="15"/>
<textElement/>
<textFieldExpression class="java.lang.Integer"><![CDATA[serialNumber]]>
</textFieldExpression>
</textField>
<textField>
<reportElement x="51" y="0" width="37" height="15"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{date}]]>
</textFieldExpression>
</textField>
<textField>
<reportElement x="138" y="0" width="75" height="15"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{time}]]>
</textFieldExpression>
</textField>
</band>
</detail>
Can anyone help to print serial number in jasper report.
Using variable we can achieve that.
Sample code :
<variable name="seraialNumber" class="java.lang.Integer" resetType="None"
calculation="Count"/>
Depends on the requirement we have to change expression
You have to bind the column to a bean which returns incrementing numbers.
You Can use alternate solution for this problem that build in Variable $V{REPORT_COUNT}.
This variable will return the row count in Integer format.
Sample Expression :
<textFieldExpression class="java.lang.Integer"><![CDATA[$V{REPORT_COUNT}]]></textFieldExpression>.
<variable name="serial number" class="java.lang.Integer" resetType="Column" calculation="Count">
<variableExpression><![CDATA[0]]></variableExpression>
</variable>
I will show an image for better understanding

stretch a row to fit the data in jasper reports using iReport

How do i stretch a text field to fit the data, If data exceeds the band height the text field doesn't stretch. I have added the text field tag in my jrxml...
The example:
<textField isStretchWithOverflow="true" pattern="" isBlankWhenNull="true" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
style="dNew"
mode="Opaque"
x="200"
y="0"
width="200"
height="19"
key="value-2"
stretchType="RelativeToTallestObject"
positionType="Float"
isPrintInFirstWholeBand="true"
isPrintWhenDetailOverflows="true"/>
<box></box>
<textElement textAlignment="Center" verticalAlignment="Top">
<font fontName="Arial" pdfFontName="Helvetica"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$F{DATA2}]]></textFieldExpression>
</textField>
Some bands do not stretch, but if you are talking about the detail band you can do something like this:
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement style="base" positionType="Float"
isPrintRepeatedValues="false" x="0" y="3"
width="380" height="26" isRemoveLineWhenBlank="true"/>
<textElement/>
<textFieldExpression class="java.lang.String">
<![CDATA[$P{information}]]></textFieldExpression>
</textField>
That is pasted from auto generated XML so there is a lot of extra stuff, but the isStretchWithOverflow="true" should work for you. This will make the field stretch down as the text fills it up.
I ususally use iReport to build my reports and it works quiet nicely. You can switch to an XML view in there too.