Jasper reports 为什么有这么多未使用的空间,引擎会跳转到一个新页面,即使当前页面上可以容纳大量数据
我使用jasper studio 6.17和jasper library 6.17,在每一页的末尾都有太多未使用的空白。我放了一张图片来显示这个问题。因此,在记录21之后,有大量的可用空间可以很容易地容纳记录22、23和24,但这些空间没有被使用,这些记录直接显示在第2页上 这是jrxml:Jasper reports 为什么有这么多未使用的空间,引擎会跳转到一个新页面,即使当前页面上可以容纳大量数据,jasper-reports,Jasper Reports,我使用jasper studio 6.17和jasper library 6.17,在每一页的末尾都有太多未使用的空白。我放了一张图片来显示这个问题。因此,在记录21之后,有大量的可用空间可以很容易地容纳记录22、23和24,但这些空间没有被使用,这些记录直接显示在第2页上 这是jrxml: <?xml version="1.0" encoding="UTF-8"?> <!-- Created with Jaspersoft Studi
<?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;
}
}
}
公共类JasperBAM{
公共静态void main(字符串[]args){
试一试{
List bhs=BAMResult.getBAMResults();
JasperPrint-JasperPrint=JasperFillManager.fillReport(“jaspereports/results.jasper”,null,新的JRBeanCollectionDataSource(bhs));
OutputStream OutputStream=新文件OutputStream(新文件(“BAM.pdf”);
jaspeexportmanager.exportReportToPdfStream(jasperPrint,outputStream);
}捕获(例外情况除外){
Logger.getLogger(JasperBAM.class.getName()).log(Level.SEVERE,null,ex);
}
}
}
公共类结果{
私有字符串文本;
私有缓冲图像;
public void settext(字符串文本){this.text=text;}
公共字符串gettext(){return text;}
public void setimage(){
试一试{
image=ImageIO.read(新文件(“image.png”);
}捕获(IOEX异常){
Logger.getLogger(BAMResult.class.getName()).log(Level.SEVERE,null,ex);
}
}
公共缓冲区映像getimage(){
返回图像;
}
公共静态列表getBAMResults(){
试一试{
List brs=new ArrayList();
对于(int i=1;i波段高度的降低(您已设置为130)仅在较新版本的jasper reports中发生。旧的布局概念是,您不能降低波段高度,只能增加波段高度。因此,在较旧版本的jasper report中,每条记录的最小高度为130(图像不存在时,每条记录下的空白)
我认为,当他们在分页符之前计算细节标注栏的可用空间时,您看到的是一个“bug”,因此他们没有考虑到您的标注栏可以动态减少,因为渲染时可以在标注栏中删除元素
我的建议是始终使用“旧”的设计理念,只允许带高度增加
可以通过使用一个框架或多个详图标注栏轻松实现这一点
框架解
这样做的目的是将对象放置在设置为最小高度的框架中,以便将细节条带高度降低到该高度。然后,框架可以溢出,并在必要时拉伸细节条带
<?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可以在另一个页面上
多细节标注栏解决方案在设计视图中的视图也更清晰,因为帧内解决方案中的图像看起来确实在标注栏之外。您是否只有在pdf输出时才遇到此问题?报表在JSS预览模式下是否更好看?问题出现在两个位置-pdf输出和Jasper Studio预览中。谢谢Petter,我试过了,效果很好。在这里发布之前,我也试过一些旧版本,但似乎还不够老。:)Jasper的开发人员同意你的观点,随着时间的推移,我决定也在Jasper论坛上发布。