Print footer on first page only - jasper-reports

I would like to have a footer appear only on the first page of jasper reports.
After searching the forums, I have seen this question asked and improperly answered several times here. As many have suggested I have tried putting a print when expression on the footer band to prevent it from printing when it isn't the first page like:
new Boolean($V{PAGE_NUMBER}.intValue() == 1)
This does not work though. The result is that none of the attributes of the page footer print, but the footer block still prints and takes up space preventing the detail from using the whole page. Effectively, you have a footer with data on the first page and a footer with no data on every following page.
Does anyone know a trick how to actually make this work?

Definitely there does not seem to be a direct way of achieving it, at least with current version of Jasper libraries available. However here's a very crude workaround that worked for us in a similar requirement. I separated out only the footer management part of the code into a sample project this morning and it works fine.
Notes:
Not sure if this works with complex reports (especially if column-footer needs to be printed, as we manually modify the columnFooterOffsetY value). We had to generate a simple report with only title, detail and footer bands.
Importantly, i do not recommend using this solution for complex requirements as it may induce maintainability issues in long run (especially if you migrate to a later version of jasperreports library in future which might have modified the report-filling logic).
I have used JasperReports library version 5.0.0 for the testing.
How the example is built:
Created JRXML with iReport, with a printwhenexpression, new Boolean($V{PAGE_NUMBER}.intValue() == 1, for the page-footer band.
Created a Custom report-filler instance, extending the JRVerticalFiller class.
Sample source XML used: (add additional lines to the XML so that the report wraps to multiple pages)
<?xml version="1.0" encoding="UTF-8"?>
<employees>
<employee id="1001" name="AAA" email="aaa#somecorp.com" salary="20500.125"/>
<employee id="1002" name="BBB" email="bbb#somecorp.com" salary="10000.500"/>
<employee id="1003" name="CCC" email="ccc#somecorp.com" salary="12275.750"/>
<employee id="1004" name="DDD" email="ddd#somecorp.com" salary="10750.750"/>
</employees>
JRXML created with iReport (with a printwhenexpression for the footer band):
<?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="jr_footer_display_test" pageWidth="792" pageHeight="288" orientation="Landscape" columnWidth="752" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="27ac3ae2-27da-484b-b088-b4d79aa973cc">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString language="xPath">
<![CDATA[//employee]]>
</queryString>
<field name="email" class="java.lang.String">
<fieldDescription><![CDATA[./#email]]></fieldDescription>
</field>
<field name="id" class="java.lang.String">
<fieldDescription><![CDATA[./#id]]></fieldDescription>
</field>
<field name="name" class="java.lang.String">
<fieldDescription><![CDATA[./#name]]></fieldDescription>
</field>
<field name="salary" class="java.lang.String">
<fieldDescription><![CDATA[./#salary]]></fieldDescription>
</field>
<background>
<band splitType="Stretch"/>
</background>
<columnHeader>
<band height="50">
<staticText>
<reportElement uuid="c3d42e71-672e-402f-9fbb-4889be2a269b" x="29" y="2" width="100" height="20"/>
<textElement/>
<text><![CDATA[ID]]></text>
</staticText>
<staticText>
<reportElement uuid="a4c42dc4-4276-485a-b5a6-b4e6bd2bc217" x="136" y="2" width="100" height="20"/>
<textElement/>
<text><![CDATA[Name]]></text>
</staticText>
<staticText>
<reportElement uuid="157e527b-7ad5-46bf-a06d-2fa0a2686b1e" x="253" y="2" width="100" height="20"/>
<textElement/>
<text><![CDATA[Email]]></text>
</staticText>
<staticText>
<reportElement uuid="4d87c542-7057-4bc1-9a7e-fbd6a554f33a" x="386" y="2" width="100" height="20"/>
<textElement/>
<text><![CDATA[Salary]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="21" splitType="Stretch">
<textField>
<reportElement uuid="31d09543-a128-469a-be38-3d8987ba781b" x="29" y="0" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="ce5c11f8-68da-4efd-93fa-e1f1b5ce407f" x="136" y="0" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="300dcc3b-8a2a-489d-8518-7283c95b2f88" x="253" y="0" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{email}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="a37f2df9-2459-446d-bc47-488a336aa60e" x="386" y="0" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{salary}]]></textFieldExpression>
</textField>
</band>
</detail>
<pageFooter>
<band height="40" splitType="Stretch">
<printWhenExpression><![CDATA[new Boolean($V{PAGE_NUMBER}.intValue() == 1)]]></printWhenExpression>
<textField>
<reportElement uuid="3d9beff7-69b8-44d9-af80-2962b9262368" x="29" y="12" width="80" height="20"/>
<textElement textAlignment="Left"/>
<textFieldExpression><![CDATA["Page: "+$V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
</band>
</pageFooter>
</jasperReport>
Custom report-filler implementation: (this handles the height resetting logic to ensure detail band stretches longer from page 2 onwards)
public class CustomVerticalFiller extends JRVerticalFiller {
private JRFillBand detailBand = null;
private int pageNumber = -1;
protected CustomVerticalFiller(JasperReportsContext jasperReportsContext, JasperReport jasperReport) throws JRException {
super(jasperReportsContext, jasperReport);
detailBand = detailSection.getFillBands()[0];
}
// this method gets called after each detail band row is filled
protected void resolveBandBoundElements(JRFillBand band, byte evaluation) throws JRException {
if(band == detailBand) {
if((detailBand.getBreakHeight() > columnFooterOffsetY - offsetY) && (columnIndex == columnCount - 1)) {
// we have reached end of a page
pageNumber++;
// we reset the offset when we are at the end of page 2, so that jasper continues to fill data
if(pageNumber == 1) {
columnFooterOffsetY += pageFooter.getHeight();
}
}
}
}
}
And the Java code that actually uses the custom filler to export a PDF:
InputStream inputStream = new FileInputStream(new File(<my jrxml file path>));
JRDataSource dataSource = new JRXmlDataSource(new File(<my source xml file path>), "//employee");
Map parameters = new HashMap();
JasperDesign jasperDesign = JRXmlLoader.load(inputStream);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
CustomVerticalFiller customVerticalFiller = new CustomVerticalFiller(DefaultJasperReportsContext.getInstance(), jasperReport);
JasperPrint jasperPrint = customVerticalFiller.fill(parameters, dataSource);
JasperExportManager.exportReportToPdfFile(jasperPrint, <target pdf file path>);

You should put this code on Band Properties Print when expression of PageFooter
new Boolean( $V{PAGE_NUMBER}.intValue() == 1 )

Unfortunately #emecheon answer doesn't work with current jasper version ( I use 6.7.0), but cure is pretty much the same.
I change columnFooterOffsetY after first page.
This way you can have footer of different size for each page.
public class CustomVerticalFiller extends JRVerticalFiller {
private int pageNumber = 0;
public CustomVerticalFiller(JasperReportsContext jasperReportsContext, JasperReport jasperReport) throws JRException {
super(jasperReportsContext, jasperReport);
}
#Override
protected void addPage(JRPrintPage page) {
super.addPage(page);
pageNumber++;
if (pageNumber == 2) {
columnFooterOffsetY += pageFooter.getHeight();
}
}
}

The expression I use to print a footer only on the first page is the following:
In the Print When Expression field
IF($V{PAGE_NUMBER}.intValue()==1,TRUE( ),FALSE( ))
It checks whether this is the first page and if so it returns true and prints the band. If not, it does not print anything.
If you would like to print one footer on the first page and a different one on the following pages. I use this code as the expression for the subreport properties. (not print when expression)
IF($V{PAGE_NUMBER}.intValue()==1,"subreportFooter1.jasper","subreportFooter2.jasper")

Related

Why so much unused space, engine is skipping to a new page even if a lot of data could fit on the current page

I use jasper studio 6.17 and jasper library 6.17 and I have too much unused white space at the end of every page. I placed a image down to show the problem. So after record 21 there is a lot of free space that could easily fit records 22,23 and 24 but the space is not used, these records are displayed directly on page 2.
This is the jrxml:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.17.0.final using JasperReports Library version 6.17.0-6d93193241dd8cc42629e188b94f9e0bc5722efd -->
<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="results" pageWidth="595" pageHeight="842" columnWidth="595" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" isFloatColumnFooter="true">
<field name="text" class="java.lang.String"/>
<field name="image" class="java.awt.Image"/>
<detail>
<band height="130" splitType="Stretch">
<textField isBlankWhenNull="true">
<reportElement x="0" y="0" width="595" height="29" isRemoveLineWhenBlank="true"/>
<box padding="0">
<pen lineWidth="1.25" lineStyle="Solid" lineColor="#030303"/>
</box>
<textElement>
<font fontName="DejaVu Sans" isBold="true"/>
<paragraph lineSpacingSize="0.0"/>
</textElement>
<textFieldExpression><![CDATA[$F{text}]]></textFieldExpression>
</textField>
<image>
<reportElement x="0" y="29" width="190" height="100" isRemoveLineWhenBlank="true"/>
<box>
<pen lineWidth="2.0" lineColor="#030303"/>
</box>
<imageExpression><![CDATA[$F{image}]]></imageExpression>
</image>
</band>
</detail>
</jasperReport>
And this is the full java code:
public class JasperBAM {
public static void main(String[] args) {
try {
List<BAMResult> bhs = BAMResult.getBAMResults();
JasperPrint jasperPrint = JasperFillManager.fillReport("JasperReports/results.jasper", null, new JRBeanCollectionDataSource(bhs));
OutputStream outputStream = new FileOutputStream(new File("BAM.pdf"));
JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
} catch (Exception ex) {
Logger.getLogger(JasperBAM.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public class BAMResult {
private String text;
private BufferedImage image;
public void settext(String text){this.text=text;}
public String gettext(){return text;}
public void setimage(){
try {
image=ImageIO.read(new File("image.png"));
} catch (IOException ex) {
Logger.getLogger(BAMResult.class.getName()).log(Level.SEVERE, null, ex);
}
}
public BufferedImage getimage() {
return image;
}
public static List<BAMResult> getBAMResults() {
try {
List<BAMResult> brs = new ArrayList<>();
for(int i=1; i<100; i++) {
BAMResult nt = new BAMResult();
nt.settext("record "+i);
if (i % 20==0){
nt.setimage();
}
brs.add(nt);
}
return brs;
} catch (Exception ex) {
Logger.getLogger(JasperBAM.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}
}
UPDATE 1
I tried to set the the "split type" of the detail band to "Immediate" but the space still appears.
UPDATE 2
The problem is not related to the image field, I tried to put a text field in the place of the image and the problem is the same.
The decreasing of band height (that you have set to 130) is something that only happens in newer versions of jasper reports. The old layout concept was that you can not decrease the band height you can only increase it. Hence in older versions of jasper report every record would have had a minimum height of 130 (blank space under every record when image is not present)
I think what you are seeing is a "bug" when they are calculating the avviabile space for the detail band before page break, hence they are not considering that your band can dynamically decrease since element can be removed inside the band when rendered.
My suggestion is to always use the "old" design idea, only let band height increase.
You can easily achieve this by either using a frame or multiple detail bands
The frame solution
The idea is to put objects in frame that you set to minimum height so that you can reduce the detail band heights to this. The frame can then overflow and with that stretch the detail band when necessary.
<?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="results" pageWidth="595" pageHeight="842" columnWidth="595" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" isFloatColumnFooter="true" uuid="176d8296-c530-48d6-85dc-11c41dce9f06">
<field name="text" class="java.lang.String"/>
<field name="image" class="java.awt.Image"/>
<detail>
<band height="30" splitType="Stretch">
<frame>
<reportElement x="0" y="0" width="595" height="29" uuid="7b3d35fe-eddb-4d8d-8016-6496b706950b">
<property name="com.jaspersoft.studio.unit.x" value="px"/>
<property name="com.jaspersoft.studio.unit.y" value="px"/>
</reportElement>
<textField isBlankWhenNull="true">
<reportElement x="0" y="0" width="595" height="29" isRemoveLineWhenBlank="true" uuid="92b46a19-42c0-42f2-846a-3c7a7aaf0e2a"/>
<box padding="0">
<pen lineWidth="1.25" lineStyle="Solid" lineColor="#030303"/>
</box>
<textElement>
<paragraph lineSpacingSize="0.0"/>
</textElement>
<textFieldExpression><![CDATA[$F{text}]]></textFieldExpression>
</textField>
<image>
<reportElement x="0" y="29" width="190" height="100" isRemoveLineWhenBlank="true" uuid="1dc15e42-01a4-413f-b04e-30b8d0437e36"/>
<box>
<pen lineWidth="2.0" lineColor="#030303"/>
</box>
<imageExpression><![CDATA[$F{image}]]></imageExpression>
</image>
</frame>
</band>
</detail>
</jasperReport>
Multiple detail band solution
Use print expression on detail band to print or not print a band, in your case print it only when the image is aviaibile.
<?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="results" pageWidth="595" pageHeight="842" columnWidth="595" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" isFloatColumnFooter="true" uuid="176d8296-c530-48d6-85dc-11c41dce9f06">
<field name="text" class="java.lang.String"/>
<field name="image" class="java.awt.Image"/>
<detail>
<band height="30" splitType="Stretch">
<property name="com.jaspersoft.studio.unit.height" value="px"/>
<textField isBlankWhenNull="true">
<reportElement x="0" y="0" width="595" height="29" isRemoveLineWhenBlank="true" uuid="92b46a19-42c0-42f2-846a-3c7a7aaf0e2a"/>
<box padding="0">
<pen lineWidth="1.25" lineStyle="Solid" lineColor="#030303"/>
</box>
<textElement>
<paragraph lineSpacingSize="0.0"/>
</textElement>
<textFieldExpression><![CDATA[$F{text}]]></textFieldExpression>
</textField>
</band>
<band height="100">
<printWhenExpression><![CDATA[new Boolean($F{image}!=null)]]></printWhenExpression>
<image>
<reportElement x="0" y="0" width="190" height="100" uuid="1dc15e42-01a4-413f-b04e-30b8d0437e36"/>
<box>
<pen lineWidth="2.0" lineColor="#030303"/>
</box>
<imageExpression><![CDATA[$F{image}]]></imageExpression>
</image>
</band>
</detail>
</jasperReport>
The two solution will both render the result you are asking for, the are some difference with the page break, in the frame solution you can force the page break if both does not fit in page, while in the two band solution you will have a solution where band 1 can be on one page and band 2 on another.
The multiple detail band solution is also a bit cleaner to view in the design view, since in frame solution the image does appear to be outside of the band.

Jasper report unicode font not showing in PDF

I am using jasper report with Spring MVC. I have generated font.jar from jaspersoft studio 6.6.0 and add it to my application classpath. I am trying to use vrinda.ttf and sutonnyOMJ.ttf.
While the report is rendered properly in jasper studio, when I am using it from my application no unicode fonts are showing for PDF export. For excel export all values are showing properly but pdf is not working.
Font Family from my custom font.jar
<?xml version="1.0" encoding="UTF-8"?>
<fontFamilies>
<fontFamily name="vrinda">
<normal><![CDATA[fonts/vrinda/vrinda.ttf]]></normal>
<pdfEncoding><![CDATA[Identity-H]]></pdfEncoding>
<pdfEmbedded><![CDATA[true]]></pdfEmbedded>
<exportFonts>
<export key="net.sf.jasperreports.html">vrinda</export>
<export key="net.sf.jasperreports.xhtml">vrinda</export>
</exportFonts>
</fontFamily>
<fontFamily name="sutonny">
<normal><![CDATA[fonts/sutonny/SutonnyOMJ.ttf]]></normal>
<bold><![CDATA[fonts/sutonny/SutonnyMJ-Bold.ttf]]></bold>
<italic><![CDATA[fonts/sutonny/SutonnyMJ-Italic.ttf]]></italic>
<boldItalic><![CDATA[fonts/sutonny/SutonnyMJ-BoldItalic.ttf]]></boldItalic>
<pdfEncoding><![CDATA[Identity-H]]></pdfEncoding>
<pdfEmbedded><![CDATA[true]]></pdfEmbedded>
<exportFonts>
<export key="net.sf.jasperreports.html">SutonnyOMJ</export>
<export key="net.sf.jasperreports.xhtml">SutonnyOMJ</export>
</exportFonts>
</fontFamily>
</fontFamilies>
JRXML:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.6.0 -->
<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="myName" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="a43ee182-4df8-4ea4-b26f-b8b21078c9d1">
<property name="com.jaspersoft.studio.data.sql.tables" value=""/>
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="dbname"/>
<queryString language="SQL">
<![CDATA[Query]]>
</queryString>
<field name="ClientName" class="java.lang.String">
<property name="com.jaspersoft.studio.field.label" value="ClientName"/>
<property name="com.jaspersoft.studio.field.tree.path" value="Table"/>
</field>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch">
<textField>
<reportElement x="20" y="5" width="500" height="30" uuid="f4900f30-da32-4cec-9142-10b111debcf3"/>
<textElement>
<font fontName="Vrinda" size="26"/>
</textElement>
<textFieldExpression><![CDATA["\u09AA\u09CD\u09B0\u09A4\u09BF\u09AC\u09C7\u09A6\u09A8"]]></textFieldExpression>
</textField>
</band>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch">
<staticText>
<reportElement x="82" y="0" width="100" height="30" uuid="b35f8d0e-5c47-46c7-b71c-c63dd7706724">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="9609ebcc-b3ae-4997-92b2-9aa48843434a"/>
</reportElement>
<textElement>
<font size="26"/>
</textElement>
<text><![CDATA[ClientName]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="125" splitType="Stretch">
<textField>
<reportElement x="82" y="64" width="308" height="30" uuid="ba9cab6a-2688-4601-bedb-3b62913a2cfb">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="9609ebcc-b3ae-4997-92b2-9aa48843434a"/>
</reportElement>
<textElement>
<font fontName="Vrinda" size="26"/>
</textElement>
<textFieldExpression><![CDATA[$F{ClientName}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
Java Code:
JasperReport jasperReport = JasperCompileManager.compileReport(reportSourceFile);
JasperPrint print = JasperFillManager.fillReport(jasperReport, params, reportRequestDTO.getConnection());
JRPdfExporter exporter = new JRPdfExporter();
ExporterInput exporterInput = new SimpleExporterInput(print);
exporter.setExporterInput(exporterInput);
OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(path);
exporter.setExporterOutput(exporterOutput);
SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
exporter.setConfiguration(configuration);
exporter.exportReport();
pom.xml
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.5.1</version>
</dependency>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports-fonts</artifactId>
<version>6.0.0</version>
</dependency>
Excel Image for header unicode (Which is not showing in PDF)

List component displays only the first row

In my JasperReport's report only the first row of my collection gets displayed. Here is the relevant code.
The entity
public class LegendEntity implements Serializable{
private String label;
private Image bufferedImage;
public LegendEntity() {
}
public LegendEntity(String label) {
this.label = label;
}
public LegendEntity(String label,Image bufferedImage) {
this.label = label;
this.bufferedImage = bufferedImage;
}
//getters-setters
Preparing the datasource:
List<MyEntity> myEntitiesList = new ArrayList<>();
//filling the list
JRBeanCollectionDataSource entityDS= new JRBeanCollectionDataSource(myEntitiesList ,false);
report.getReportParameters().put("ENTITY_DATASOURCE", entityDS);
The jrxml:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport ... name="SampleReport" printOrder="Horizontal" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="595" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0">
<subDataset name="LegendDataset" uuid="e0d72aca-6fd5-4935-b57f-ff5a436f2afb">
<field name="label" class="java.lang.String">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="bufferedImage" class="java.awt.Image"/>
</subDataset>
<parameter name="P_MAP_SCALE_STR" class="java.lang.String"/>
<parameter name="ENTITY_DATASOURCE" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
<detail>
<band height="842" splitType="Stretch">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.FreeLayout"/>
<frame>
<reportElement x="298" y="635" width="298" height="206" uuid="e807b35a-857c-43ba-a080-13f422eb1456"/>
<componentElement>
<reportElement x="11" y="11" width="275" height="186" uuid="d6f579d3-75de-4745-8f94-c974d2e697a0"/>
<jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
<datasetRun subDataset="LegendDataset" uuid="ee194811-e7e5-4102-83ff-b150901d73c9">
<dataSourceExpression><![CDATA[$P{ENTITY_DATASOURCE}]]></dataSourceExpression>
</datasetRun>
<jr:listContents height="186" width="275">
<staticText>
<reportElement x="10" y="10" width="130" height="20" uuid="4260f10d-ee62-4cf6-8023-d0dc2266f4dd"/>
<textElement textAlignment="Center"/>
<text><![CDATA[ENTITY LABEL]]></text>
</staticText>
<staticText>
<reportElement x="150" y="10" width="100" height="20" uuid="88135c50-3c17-4b0f-b7e5-b05987f98b02"/>
<textElement textAlignment="Center"/>
<text><![CDATA[ENTITY SYMBOL]]></text>
</staticText>
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement x="10" y="31" width="130" height="18" uuid="30885d06-38db-4b1c-a312-616a60ee1c42"/>
<textFieldExpression><![CDATA[$F{label}]]></textFieldExpression>
</textField>
<image>
<reportElement x="150" y="31" width="100" height="18" uuid="0020adca-acad-4915-9f0d-88d75e4897c7"/>
<imageExpression><![CDATA[$F{bufferedImage}]]></imageExpression>
</image>
</jr:listContents>
</jr:list>
</componentElement>
</frame>
<staticText>
<reportElement x="30" y="600" width="80" height="18" uuid="6c1afd65-a8d4-4e3f-9a56-d09abe7ec904"/>
<textElement textAlignment="Right">
<font fontName="DejaVu Sans" size="9" isBold="true"/>
</textElement>
<text><![CDATA[Ölçek: 1/]]></text>
</staticText>
<textField>
<reportElement positionType="Float" x="110" y="600" width="100" height="18" uuid="6360a545-63af-48cc-987d-d828c24a3b2a"/>
<textElement>
<font fontName="DejaVu Sans" size="9" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$P{P_MAP_SCALE_STR}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
And on the report only the first entity's label and symbol is displayed. What am I missing?
Debugging already done and I am sure that 1+ entities are present in the list.
The whole JRXML here .
You can try to put variables in white zone in table. Not in blue/header zone.
It works for me.
I have noticed at least 2 problems in your main JRXML:
Setting printOrder="Horizontal" at the report level may prevent proper elements overflow. Try reverting to printOrder="Vertical" or remove the attribute completely.
There is too much whitespace inside the list element that would force overflow. You should not have white space after the last elements.

jasper report textfield height is not dynamic [duplicate]

I am using jasper report as reporting tool in my application. And I am wondering how can i wrap the long text by expanding the reportElement dynamically into vertical direction ( expanding the row size, not column width). Or Is there any way to achieve this? My approach below is truncating the long text "Some very long name". Can you please give me some suggestions?
Output :
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.view.JasperViewer;
public class JrUtils {
public static void showJrReport(List objectList, String fileName, String title, Map parameters) {
try {
File f = new File(fileName);
JasperDesign jasperDesign = JRXmlLoader.load(f.getAbsolutePath());
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(objectList);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, ds);
JasperViewer jv = new JasperViewer(jasperPrint, false);
jv.setTitle(title);
jv.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
List<Person> pList = new ArrayList<Person>();
Person p1 = new Person();
p1.setPersonName("Some Name ");
p1.setAddress("Nepal - Address Fits Here");
Person p2 = new Person();
p2.setPersonName("Some very long name");
p2.setAddress("Nepal - Address Fits Here");
pList.add(p1);
pList.add(p2);
showJrReport(pList, "testReport.jrxml", "Test Report", new HashMap<Object, Object>());
}
}
Jasper Report JrXML file - testReport.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="report name" pageWidth="250" pageHeight="400" columnWidth="210" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="ireport.zoom" value="2.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<field name="personName" class="java.lang.String"/>
<field name="address" class="java.lang.String"/>
<columnHeader>
<band height="23" splitType="Stretch">
<rectangle>
<reportElement x="0" y="0" width="88" height="23"/>
</rectangle>
<rectangle>
<reportElement x="88" y="0" width="122" height="23"/>
</rectangle>
<staticText>
<reportElement x="0" y="0" width="88" height="23"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Name]]></text>
</staticText>
<staticText>
<reportElement x="88" y="0" width="122" height="23"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Address]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="21" splitType="Stretch">
<rectangle>
<reportElement x="0" y="0" width="88" height="21"/>
</rectangle>
<textField>
<reportElement x="0" y="0" width="88" height="21"/>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{personName}]]></textFieldExpression>
</textField>
<rectangle>
<reportElement x="88" y="0" width="122" height="21"/>
</rectangle>
<textField>
<reportElement x="88" y="0" width="122" height="21"/>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{address}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
I found the answer myself :
I did some extra research about the properties of textField and rectangle components. And found that I need to set the following properties.
For rectangle :
<rectangle>
<reportElement stretchType="RelativeToBandHeight" ... />
</rectangle>
For textField :
<textField isStretchWithOverflow="true">
...
</textField>
Output as expected :
The <detail> ...</detail> section after correction :
<detail>
<band height="21" splitType="Stretch">
<rectangle>
<reportElement stretchType="RelativeToBandHeight" x="0" y="0" width="88" height="21"/>
</rectangle>
<textField isStretchWithOverflow="true">
<reportElement x="2" y="0" width="84" height="21"/>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{personName}]]></textFieldExpression>
</textField>
<rectangle>
<reportElement stretchType="RelativeToBandHeight" x="88" y="0" width="122" height="21"/>
</rectangle>
<textField isStretchWithOverflow="true">
<reportElement x="90" y="0" width="118" height="21"/>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{address}]]></textFieldExpression>
</textField>
</band>
</detail>
UPDATE
You can also set property net.sf.jasperreports.print.keep.full.text to true to achieve that across your all reports.
Also you can make the truncation elegant by using the following properties set to the textfield. Check this sample
net.sf.jasperreports.text.truncate.at.char
net.sf.jasperreports.text.truncate.suffix
net.sf.jasperreports.print.keep.full.text
Go to the Text Field Properties then set Text Adjust to StretchHeight.

PieChart: When and how we should use Increment Type?

The code of my servlet:
JasperCompileManager.compileReportToFile(jrxmlSourcePathMain,
jrxmlDestPathMain);
InputStream isRef = new FileInputStream(new File(jrxmlDestPathMain));
ServletOutputStream sosRef = response.getOutputStream();
response.setContentType("application/pdf");
JasperRunManager.runReportToPdfStream(isRef, sosRef, new HashMap(),
new JRBeanCollectionDataSource(buyBookInfoList));
sosRef.flush();
sosRef.close();
The snippet of jrxml file:
<summary>
<band height="265" splitType="Stretch">
<pieChart>
<chart isShowLegend="true" renderType="svg" theme="default">
<reportElement uuid="c6a09cc9-bd15-4b09-8657-05868a148f18" x="0" y="0" width="554" height="265"/>
<chartTitle position="Top" color="#FF0000">
<font fontName="宋体" size="18" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
<titleExpression><![CDATA["报表演示"]]></titleExpression>
</chartTitle>
<chartSubtitle color="#0000FF">
<font fontName="宋体" size="14" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
<subtitleExpression><![CDATA["子标题"]]></subtitleExpression>
</chartSubtitle>
<chartLegend textColor="#33FF33" backgroundColor="#6666FF" position="Right">
<font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
</chartLegend>
</chart>
<pieDataset>
<keyExpression><![CDATA[$F{username}]]></keyExpression>
<valueExpression><![CDATA[$F{buyBookNum}]]></valueExpression>
</pieDataset>
<piePlot isShowLabels="true" isCircular="false" labelFormat="姓名为:{0}">
<plot orientation="Horizontal" labelRotation="180.0"/>
<itemLabel/>
</piePlot>
</pieChart>
</band>
</summary>
I set Increment Type property for PieChart value with different values: none, report, page. But I did not notice any difference in generated reports.
So my question is how to use the PieChart's Increment Type property?
What is a purpose of this property?
The increment type allows you to tell the pie chart when to select values to use. For example, if you want to create a pie chart that shows sales in the USA, Canada, and Mexico, you might increment over a "country" group.