How to remove margin from first page using Jasper reports? - jasper-reports

I am creating PDF report using JasperReport(jrxml). I am new in JasperReport so I have no idea for how to remove margin from first page.
My requirements is for in PDF report, first page is for cover page where I am showing company logo and company details and after cover page I am showing all required data.
please see my jrxml code, I am not showing all code just showing where I am using margin code
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="rpt_survey_analysis" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="df013db5-f76e-44d3-b0df-bcbc46d93160">
</jasperReport>
in this code, I set topMargin, bottomMargin, leftMargin, rightMargin but this margin is affected to all page. But i want to affect this margin except cover page. So how can do this?

You need to set 0 margin on your report, you can not have different margins for different bands
From Understanding Bands
A band is as wide as the page width (right and left margins excluded).
Once you have zero margin, a few solution could be:
Position your elements in other bands to create a "fake" margin example x="20"
Use a frame component with x position at margin and add your other components to this. example
<frame>
<reportElement x="20" y="0" width="550" height="40" uuid="0d4ab012-9161-4f37-b8e0-c6ddcdd3d550"/>
<staticText>
<reportElement x="0" y="0" width="550" height="30" uuid="3dc51fb0-0bc3-46f6-9259-fb8d9fd1f884"/>
<text><![CDATA[Text at position 0 in a frame]]></text>
</staticText>
</frame>
Use a subreport with margin.
main.jrxml, no margin
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="NoMargins" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="595" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" isTitleNewPage="true" uuid="4e2c1a7a-cbfd-4d60-9fe7-61aea15cc321">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<queryString>
<![CDATA[]]>
</queryString>
<title>
<band height="79" splitType="Stretch">
<rectangle>
<reportElement x="0" y="0" width="595" height="51" backcolor="#0015FF" uuid="e6616fe4-fed9-44aa-a8a6-2c296c6ab508">
<property name="com.jaspersoft.studio.unit.width" value="pixel"/>
</reportElement>
</rectangle>
</band>
</title>
<summary>
<band height="53" splitType="Stretch">
<subreport>
<reportElement x="0" y="0" width="595" height="50" uuid="b654e952-f1e1-4b90-a4ca-2cf3abe9f6f4"/>
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
<subreportExpression><![CDATA["with margins.jasper"]]></subreportExpression>
</subreport>
</band>
</summary>
</jasperReport>
sub report, with margin
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="with margins" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" whenResourceMissingType="Error" uuid="cccf2b18-4f7c-464c-bd9c-13dbb62af3b0">
<queryString>
<![CDATA[]]>
</queryString>
<title>
<band height="35" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="555" height="30" uuid="66542837-c420-4e7c-a760-dd55ccff2d17">
<property name="com.jaspersoft.studio.unit.width" value="pixel"/>
</reportElement>
<text><![CDATA[My report with margin]]></text>
</staticText>
</band>
</title>
</jasperReport>
Output
I would probably prefer solution 1 or 2 in a simple report, in more complex reports in can be an advantage to have first in separate report

Related

Subreport variable always return null

I need to put a variable in the master report which value came from a subreport. I followed instructions from the web, but I've always got null. For testing, I put "18" in the expression of subreport variable, but returns null.
Can someone help me? I will put the JRXML below.
I created a variable var1 in master report:
Name: var1
Calculation: System
Reset type: Report
Increment type: None
In the subreport, I created the variable returnValue
Calculation: No Calculation Function
Expression: 18
Increment type: None
Reset type: Report
In the master, I configured the return value:
From Variable: returnValue
To Variable: var1
Calculation Type: Nothing
Following the two xml files
MASTER
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 4.0.2 -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="master" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<variable name="var1" class="java.lang.Integer" calculation="System"/>
<title>
<band height="79" splitType="Stretch">
<staticText>
<reportElement x="153" y="26" width="100" height="30"/>
<text><![CDATA[MASTER]]></text>
</staticText>
</band>
</title>
<summary>
<band height="246" splitType="Stretch">
<subreport>
<reportElement x="80" y="10" width="360" height="50"/>
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
<returnValue subreportVariable="returnValue" toVariable="var1"/>
<subreportExpression><![CDATA["sub1.jasper"]]></subreportExpression>
</subreport>
<textField>
<reportElement x="149" y="147" width="100" height="30"/>
<textFieldExpression><![CDATA[$V{var1}]]></textFieldExpression>
</textField>
</band>
</summary>
</jasperReport>
SUBREPORT
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 4.0.2 -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="sub1" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<variable name="returnValue" class="java.lang.Integer">
<variableExpression><![CDATA[18]]></variableExpression>
</variable>
<title>
<band height="79" splitType="Stretch">
<staticText>
<reportElement x="212" y="24" width="100" height="30"/>
<text><![CDATA[SUB 1]]></text>
</staticText>
</band>
</title>
</jasperReport>
One problem is that the sub report is never getting executed since its main dataset does not return any row. So change the When No Data Type of sub report from <NULL> to All Sections No Detail -
Secondly the variable returnValue in Sub report have Reset type as None -
Finally the variable var1 rendered using TextField should have Evaluation Time as Report -
Below are updated JRXMLs -
MASTER
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.15.0.final using JasperReports Library version 6.15.0-dd49bfb94918336b8321d5507193f0169ead4e95 -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="master" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="d1a326d7-0db4-41f6-9791-6c0abc454731">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<variable name="var1" class="java.lang.Integer" calculation="System"/>
<title>
<band height="79" splitType="Stretch">
<staticText>
<reportElement x="153" y="26" width="100" height="30" uuid="ccc1c45e-e056-4a8e-8adc-cebefd388bc9"/>
<text><![CDATA[MASTER]]></text>
</staticText>
</band>
</title>
<summary>
<band height="246" splitType="Stretch">
<subreport>
<reportElement x="80" y="10" width="360" height="50" uuid="e251bfe7-5e81-4b79-9bc6-5f0e6c1d9555"/>
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
<returnValue subreportVariable="returnValue" toVariable="var1"/>
<subreportExpression><![CDATA["sub.jasper"]]></subreportExpression>
</subreport>
<textField evaluationTime="Report">
<reportElement x="149" y="147" width="100" height="30" uuid="24c3289e-f7a5-4137-9254-1fa2605f6741"/>
<textFieldExpression><![CDATA[$V{var1}]]></textFieldExpression>
</textField>
</band>
</summary>
</jasperReport>
SUBREPORT
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.15.0.final using JasperReports Library version 6.15.0-dd49bfb94918336b8321d5507193f0169ead4e95 -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="sub1" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="97bfa906-b658-45f6-aff8-3bfa505251b3">
<variable name="returnValue" class="java.lang.Integer" resetType="None">
<variableExpression><![CDATA[18]]></variableExpression>
</variable>
<title>
<band height="79" splitType="Stretch">
<staticText>
<reportElement x="212" y="24" width="100" height="30" uuid="9332a235-2823-4b53-b6f8-0a7c56d58aee"/>
<text><![CDATA[SUB 1]]></text>
</staticText>
</band>
</title>
</jasperReport>

jasperreports subreport with multiple columns does not print properly

I am new to Jasper reports. I have a master report that needs to print text in 3 columns. This text is dynamic and very long (about 2 pages). By set columnCount="3", the report is print as I expected (2 pages with 3 columns in each page). However I need to insert title on top of these 3 columns of first page only. If I just add textField on top of them. The title would be overlapped by text from 2nd column. So, I add subreport under title instead and set columnCount="3" in subreport instead. However, when I generate report this time it print everything in first column of every page. Please suggest what is wrong with my configuration. Thanks in advance.
master report
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Master" pageWidth="612" pageHeight="792" columnWidth="552" leftMargin="30" rightMargin="30" topMargin="0" bottomMargin="10">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<parameter name="Title"/>
<parameter name="Content"/>
<queryString>
<![CDATA[]]>
</queryString>
<background>
<band splitType="Stretch"/>
</background>
<detail>
<band height="782" splitType="Stretch">
<textField>
<reportElement x="0" y="20" width="180" height="25"/>
<textFieldExpression><![CDATA[$P{Title}]]></textFieldExpression>
</textField>
<subreport>
<reportElement x="0" y="50" width="552" height="732"/>
<subreportParameter name="Content">
<subreportParameterExpression><![CDATA[$P{Content}]]></subreportParameterExpression>
</subreportParameter>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource()]]></dataSourceExpression>
<subreportExpression><![CDATA["subreport.jasper"]]></subreportExpression>
</subreport>
</detail>
</jasperReport>
subreport
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Subreport" pageWidth="552" pageHeight="732" columnCount="3" columnSpacing="6" columnWidth="180" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0"whenNoDataType="AllSectionsNoDetail">
<parameter name="Content"/>
<summary>
<band height="732" splitType="Stretch">
<textField textAdjust="StretchHeight">
<reportElement x="0" y="0" width="180" height="20"/>
<textFieldExpression><![CDATA[$P{Content}]]></textFieldExpression>
</textField>
</summary>
</jasperReport>

The subreport with static text is not showing

I have created a very simple sub report "dummy_subreport.jrxml", which prints a static text.
dummy_subreport.jasper is included in the report "Calling_report.jrxml", which also prints a static text and includes the subreport.
However, the subreport is not getting displayed when the main report is invoked.
Subreport code is
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="dummy_subreport" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<queryString>
<![CDATA[]]>
</queryString>
<pageHeader>
<band height="50">
<staticText>
<reportElement x="90" y="0" width="286" height="30"/>
<text><![CDATA[From sub report]]></text>
</staticText>
</band>
</pageHeader>
<detail>
<band height="125" splitType="Stretch"/>
</detail>
</jasperReport>
The calling report is
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Calling_report" pageWidth="595" pageHeight="842" columnWidth="555">
<queryString>
<![CDATA[]]>
</queryString>
<detail>
<band height="133" splitType="Stretch">
<staticText>
<reportElement x="60" y="12" width="410" height="30"/>
<text><![CDATA[HELLO WORLD IN THE COVERING REPORT]]></text>
</staticText>
<subreport>
<reportElement x="261" y="58" width="200" height="52"/>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource()]]></dataSourceExpression>
<subreportExpression><![CDATA["dummy_subreport.jasper"]]></subreportExpression>
</subreport>
</band>
</detail>
</jasperReport>
Any help in this regard is greatly appreciated.

return value from sub report to main report

I have googled and prepared my sample report as per the given steps.
Steps:
Create sub report with variable retval_sub [kept default value to the variable as Hai]
Create main report variable retval_main.
Sub report properties --> added sub report variable as retval_sub and corresponding main report variable is retval_main.
I tried many times but it is not working i kept calculation type as System and Nothing also but it is not working for both.
Main report:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="testing_ret_val" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="5c1d1561-8eed-47a8-89c8-3cd1946ce07a">
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
<defaultValueExpression><![CDATA["E:\\ilaiyaraja\\"]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[select * from my_table]]>
</queryString>
<field name="id" class="java.lang.Integer"/>
<variable name="retval_main" class="java.lang.String" calculation="System"/>
<detail>
<band height="43" splitType="Stretch"/>
</detail>
<summary>
<band height="226" splitType="Stretch">
<textField>
<reportElement x="31" y="173" width="100" height="20" uuid="4e5ed8fa-e4f6-42bc-8651-6888d79f01b5"/>
<textFieldExpression><![CDATA[$V{retval_main}]]></textFieldExpression>
</textField>
<subreport>
<reportElement x="15" y="16" width="200" height="100" uuid="2bd63504-b57e-4561-b527-62f6ffe46a30"/>
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
<returnValue subreportVariable="retval_sub" toVariable="retval_main"/>
<subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "testing_ret_val_subreport1.jasper"]]></subreportExpression>
</subreport>
</band>
</summary>
</jasperReport>
Sub Report:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="testing_ret_val_subreport1" language="groovy" pageWidth="555" pageHeight="802" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="822d1131-8494-45f3-a9ab-39fc9a05072f">
<queryString>
<![CDATA[select id from my_table]]>
</queryString>
<field name="id" class="java.lang.Integer"/>
<variable name="retval_sub" class="java.lang.String">
<variableExpression><![CDATA["Hai"]]></variableExpression>
</variable>
<detail>
<band height="45" splitType="Stretch">
<textField>
<reportElement x="107" y="17" width="100" height="20" uuid="f8d1612a-06be-4082-b915-0df82b3faa42"/>
<textFieldExpression><![CDATA[$V{retval_sub}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
Please suggest
<subreport>
<reportElement x="-6" y="0" width="570" height="59" uuid="ac54a12b-8022-49a1-bc90-90cff664e3a1"/>
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("footer")]]></dataSourceExpression>
<subreportExpression><![CDATA["footer.jrxml"]]></subreportExpression>
</subreport>
add this to your code, it will work

Text in columnFooter doesn't stretch

I have a problem printing the content of a textfield. The report is an invoice and in the column footer I want to print some variable text depending of the invoice data.
I defined a textfield in Column Footer Band which gets his content from a parameter send via jasperstarter.
Problem:
The textfield cuts lines if they don't fit in the textfield even if I set "Stretch with overflow". If I make the textfield bigger, the text is displayed.
jrxml
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="StandardInvoice002" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="53084288-6a86-4b4d-a942-fa8965b8d117">
<queryString language="SQL">
<![CDATA[$P!{query}]]>
</queryString>
<columnFooter>
<band height="196" splitType="Stretch">
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement positionType="Float" stretchType="RelativeToTallestObject" isPrintRepeatedValues="false" x="30" y="165" width="500" height="21" isRemoveLineWhenBlank="true" uuid="27681503-6210-41cc-b444-5b9c0d720f4b"/>
<textElement markup="html"/>
<textFieldExpression><![CDATA["this is a very long text (1)<br>this is a very long text (2)<br>this is a very long text (3)<br>this is a very long text (4)"]]></textFieldExpression>
</textField>
</band>
</columnFooter>
</jasperReport>
Textfield contains 4 lines but only 2 are displayed
The columnFooter is of fixed size (as is the pageFooter), hence it will not stretch, so you need to change your design.
Probably what your are looking for is a groupFooter that shows on each page and is stacked at bottom.
Example
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="StandardInvoice002" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="53084288-6a86-4b4d-a942-fa8965b8d117">
<queryString language="SQL">
<![CDATA[]]>
</queryString>
<group name="pageGroup" footerPosition="StackAtBottom">
<groupExpression><![CDATA[$V{PAGE_NUMBER}]]></groupExpression>
<groupFooter>
<band height="142">
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement positionType="Float" isPrintRepeatedValues="false" x="0" y="121" width="555" height="21" isRemoveLineWhenBlank="true" uuid="27681503-6210-41cc-b444-5b9c0d720f4b"/>
<textElement markup="html"/>
<textFieldExpression><![CDATA["this is a very long text (1)<br>this is a very long text (2)<br>this is a very long text (3)<br>this is a very long text (4)"]]></textFieldExpression>
</textField>
</band>
</groupFooter>
</group>
</jasperReport>
Output
Just adjust the groupExpression to get the output on the page you like