为什么有这么多未使用的 space,即使当前页面可以容纳大量数据,引擎也会跳到新页面

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

我使用 jasper studio 6.17 和 jasper library 6.17,每页末尾都有太多未使用的白色 space。我放了一张图片来显示问题。所以在第21条记录之后有很多空闲的space可以很容易地容纳第22,23和24条记录但是space没有被使用,这些记录直接显示在第2页

这是 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>

这是完整的 java 代码:

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;
        }
    }
}

更新 1 我试图将细节带的“拆分类型”设置为“立即”,但 space 仍然出现。

更新 2 问题与图像字段无关,我尝试在图像的地方放置一个文本字段,问题是一样的。

band 高度的降低(您已设置为 130)仅在较新版本的 jasper 报告中发生。旧的布局概念是您不能降低 band 高度,只能增加它。因此,在旧版本的 jasper 报告中,每条记录的最小高度为 130(当图像不存在时,每条记录下的空白 space)

我认为当他们在分页前计算详细信息带的可用 space 时,您看到的是一个“错误”,因此他们没有考虑您的带可以动态减少,因为元素可以渲染时在带内移除。

我的建议是一直沿用“”的设计思路,只让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="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>

多细节带解决方案

在细节带上使用打印表达式来打印或不打印带,在您的情况下,仅当图像可用时才打印它。

<?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>

这两种解决方案都会呈现您要求的结果,与分页符有一些区别,在框架解决方案中,如果两者都不适合页面,您可以强制分页,而在两个带中解决方案您将有一个解决方案,其中带 1 可以在一页上,带 2 可以在另一页上。

多细节波段解决方案在设计视图中看起来也更清晰一些,因为在框架解决方案中,图像确实出现在波段之外。