Skip to content
Fix Code Error

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

June 11, 2021 by Code Error
Posted By: Anonymous

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.

enter image description here

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.

Solution

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.

Answered By: Anonymous

Related Articles

  • npm install error in vue
  • How to properly do JSON API GET requests and assign output…
  • How to parse JSON with XE2 dbxJSON
  • Azure Availability Zone ARM Config
  • SQLException: No suitable Driver Found for…
  • The 'compilation' argument must be an instance of…
  • Event Snippet for Google only shows one event while testing…
  • Search match multiple values in single field in…
  • NullpointerException error while working with choiceBox and…
  • Avoid creating new session on each axios request laravel

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Is there a free version for HangFire with Redis?

Next Post:

What is this strange expression in GCC/Clang?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Get code errors & solutions at akashmittal.com
© 2022 Fix Code Error