間違いなく、少なくとも現在のバージョンの Jasper ライブラリが利用可能であれば、それを達成する直接的な方法はないようです。ただし、同様の要件で機能する非常に大雑把な回避策を次に示します。今朝、コードのフッター管理部分だけをサンプル プロジェクトに分離したところ、問題なく動作しました。
ノート:
- これが複雑なレポートで機能するかどうかはわかりません (特に、columnFooterOffsetY 値を手動で変更するため、列フッターを印刷する必要がある場合)。タイトル、詳細、およびフッター バンドのみを含む単純なレポートを生成する必要がありました。
- 重要なのは、このソリューションを複雑な要件に使用することはお勧めしません。長期的には保守性の問題を引き起こす可能性があるためです (特に、レポート入力ロジックが変更された可能性がある将来の jasperreports ライブラリの新しいバージョンに移行する場合)。
- テストには JasperReports ライブラリ バージョン 5.0.0 を使用しました。
サンプルの作成方法:
new Boolean($V{PAGE_NUMBER}.intValue() == 1
ページ フッター バンドのprintwhen 式を使用して、iReport で JRXML を作成しました。
- JRVerticalFiller クラスを拡張して、カスタム レポート フィラー インスタンスを作成しました。
使用されるサンプル ソース XML: (レポートが複数のページに折り返されるように、XML に行を追加します)
<?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>
iReport で作成された JRXML (フッター バンドに printwhen 式を使用):
<?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>
カスタム レポート フィラーの実装: (これにより、高さの再設定ロジックが処理され、詳細バンドが 2 ページ目以降に長く伸びるようになります)
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();
}
}
}
}
}
カスタム フィラーを実際に使用して PDF をエクスポートする Java コードは次のとおりです。
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>);