JasperReportsでPDF出力(Group BandとJRBeanCollectionDataSource)

JasperReports® Library | Jaspersoft Community を使って繰り返し行を含む帳票をPDF出力するための調査。特定のグループごとの小計などを出力する必要に迫られたのだが、どうもGroup Bandを定義すれば良さそう。実験だ。

jrxml 定義

f:id:lmtak:20171118193437p:plain

バンド(Band)は「Column Header」「"Subtotal" Group Header」「Detail」「"Subtotal" Group Footer」「Column Footer」「Page Footer」の6つを使用。

jrxmlのoutline

f:id:lmtak:20171118193432p:plain

Fields として name, price, title を定義。

Group Header/Footer の properties

f:id:lmtak:20171118193426p:plain

Expression として $F{title} を設定し、Keep Together にチェックをしている。

PDF出力結果 (1. Keep Togetherにチェック、New Start Pageは未チェック)

f:id:lmtak:20171118193621p:plainf:id:lmtak:20171118193618p:plain

Group Header/Footer の Expression に設定したように、title が同じ値であるものがまとめられて出力される。Keep Together がチェックされていると、まとめられた単位(Group Header - Detail(s) - Group Footer)がページ分割されないように制御される。

PDF出力結果(2. Keep Togetherは未チェック、New Start Pageは未チェック)

f:id:lmtak:20171118193422p:plainf:id:lmtak:20171118193418p:plain

$F{title} が "title-3" であるものが1ページ目と2ページ目に分かれて表示される。なお、"Reprint Header On Each Page" をチェックすると、2ページ目に分かれた部分にも Group Header が挿入される。

f:id:lmtak:20171118200901p:plain

PDF出力結果(3. Keep Togetherにチェック、New Start Pageにチェック)

f:id:lmtak:20171118193613p:plainf:id:lmtak:20171118193601p:plainf:id:lmtak:20171118193557p:plain

Group Header/Footer の Expression に設定した、title が同じ値であるものがまとめられて出力される。一方、title の値が変わると改ページされている。

ソースファイルなど

PDF出力処理 (Pdf20171118B.java)
package sandbox;

import java.io.File;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
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 sandbox.bean.Bean20171118B;

public class Pdf20171118B {
  public static void main(String[] args) {
    try {
      // read jrxml file
      File jrxmlFile = new File("D:\\users\\lmtak\\JaspersoftWorkspace\\MyReports\\20171118B.jrxml");
      // generate JasperReport instance
      JasperReport jasperReport = JasperCompileManager.compileReport(jrxmlFile.getAbsolutePath());
      
      // for Parameter ($P{xxx})
      HashMap<String,  Object> params = new HashMap<String,  Object>();
      // for Field(s) ($F{xxx})
      List<Bean20171118B> fieldsList = new ArrayList<Bean20171118B>();
      // group band (1)
      for (int ix = 0; ix < 10; ix++) {
        fieldsList.add(new Bean20171118B("title-1", "name" + (10 + ix), "" + (1000 * (ix + 1))));
      }
      // group band (2)
      for (int ix = 0; ix < 15; ix++) {
        fieldsList.add(new Bean20171118B("title-2", "name2" + (10 + ix), "" + (1000 * (ix + 1))));
      }
      // group band (3)
      for (int ix = 0; ix < 20; ix++) {
        fieldsList.add(new Bean20171118B("title-3", "name9" + (10 + ix), "" + (1000 * (ix + 1))));
      }

      // fill parameters and fields
      JasperPrint print = JasperFillManager.fillReport(jasperReport,  params,  new JRBeanCollectionDataSource(fieldsList));
      // generate pdf files
      File pdf = new File("pdf\\output.pdf");
      JasperExportManager.exportReportToPdfFile(print,  pdf.getAbsolutePath());
      
    } catch (Exception ex) {
      System.err.print(ex.getMessage());
    }
  }
}
POJO (Bean20171118B.java)
package sandbox.bean;

public class Bean20171118B {

  private String title = "";
  private String name = "";
  private String price = "";
  
  public Bean20171118B() {
  }

  public Bean20171118B(String t, String n, String p) {
    this.title = t;
    this.name  = n;
    this.price = p;
  }
  public String getTitle() {
    return title;
  }
  public void setTitle(String title) {
    this.title = title;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String getPrice() {
    return price;
  }
  public void setPrice(String price) {
    this.price = price;
  }
}
帳票テンプレート (20171118B.jrxml)
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.4.3.final using JasperReports Library version 6.4.3  -->
<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="20171118" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="63e91e08-c5f2-4f0c-bd25-9cc8984da45f">
  <queryString>
    <![CDATA[]]>
  </queryString>
  <field name="name" class="java.lang.String"/>
  <field name="price" class="java.lang.String"/>
  <field name="title" class="java.lang.String"/>
  <group name="Subtotal" keepTogether="true">
    <groupExpression><![CDATA[$F{title}]]></groupExpression>
    <groupHeader>
      <band height="20">
        <staticText>
          <reportElement x="180" y="0" width="190" height="20" uuid="daa840a2-c031-452a-b8a6-01fb81862f1f"/>
          <box>
            <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
            <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
            <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
            <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
          </box>
          <textElement textAlignment="Right" verticalAlignment="Middle">
            <font size="14" isItalic="true"/>
          </textElement>
          <text><![CDATA[Group Header Band]]></text>
        </staticText>
        <textField isBlankWhenNull="true">
          <reportElement x="0" y="0" width="180" height="20" uuid="b94d1f3a-2bbd-4a38-b9dc-2b737b25b6aa"/>
          <box leftPadding="2">
            <pen lineWidth="0.5" lineStyle="Solid"/>
          </box>
          <textElement verticalAlignment="Middle"/>
          <textFieldExpression><![CDATA[$F{title}]]></textFieldExpression>
        </textField>
      </band>
    </groupHeader>
    <groupFooter>
      <band height="20">
        <staticText>
          <reportElement x="180" y="0" width="190" height="20" uuid="4304c35b-c9e6-42b2-8d1b-7590022a502d"/>
          <box>
            <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
            <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
            <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
            <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
          </box>
          <textElement textAlignment="Right" verticalAlignment="Middle">
            <font size="14" isItalic="true"/>
          </textElement>
          <text><![CDATA[Group Footer Band]]></text>
        </staticText>
        <staticText>
          <reportElement x="0" y="0" width="90" height="20" uuid="a4acb0a1-6534-45b9-abf9-3c74461c8f4a"/>
          <box rightPadding="8">
            <pen lineWidth="0.5"/>
          </box>
          <textElement textAlignment="Right" verticalAlignment="Middle"/>
          <text><![CDATA[Subtotal]]></text>
        </staticText>
        <textField isBlankWhenNull="true">
          <reportElement x="90" y="0" width="90" height="20" uuid="5f32ea53-06c3-4f18-b88d-4fbfbaf58e1c"/>
          <box rightPadding="2">
            <pen lineWidth="0.5"/>
          </box>
          <textElement textAlignment="Right" verticalAlignment="Middle"/>
          <textFieldExpression><![CDATA["subtotal..."]]></textFieldExpression>
        </textField>
      </band>
    </groupFooter>
  </group>
  <columnHeader>
    <band height="20" splitType="Stretch">
      <staticText>
        <reportElement x="450" y="0" width="100" height="20" uuid="cc643ca0-4a48-4546-84f0-0500a0d45b77"/>
        <text><![CDATA[20171118B.jrxml]]></text>
      </staticText>
      <staticText>
        <reportElement x="180" y="0" width="190" height="20" uuid="daa7584c-703d-4f37-8629-d7375a110f94"/>
        <box>
          <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
          <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
          <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
          <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
        </box>
        <textElement textAlignment="Right" verticalAlignment="Middle">
          <font size="14" isItalic="true"/>
        </textElement>
        <text><![CDATA[Column Header Band]]></text>
      </staticText>
      <staticText>
        <reportElement mode="Opaque" x="0" y="0" width="90" height="20" backcolor="#F0F0F0" uuid="70c6a907-8e3b-4d34-9691-b136fa36f8cf"/>
        <box>
          <pen lineWidth="0.5"/>
        </box>
        <textElement textAlignment="Center" verticalAlignment="Middle">
          <font size="14"/>
        </textElement>
        <text><![CDATA[Name]]></text>
      </staticText>
      <staticText>
        <reportElement mode="Opaque" x="90" y="0" width="90" height="20" backcolor="#F0F0F0" uuid="4f3679e1-8eba-41d8-a811-8b7cd4863b3a"/>
        <box>
          <pen lineWidth="0.5"/>
        </box>
        <textElement textAlignment="Center" verticalAlignment="Middle">
          <font size="14"/>
        </textElement>
        <text><![CDATA[Price]]></text>
      </staticText>
    </band>
  </columnHeader>
  <detail>
    <band height="20" splitType="Stretch">
      <staticText>
        <reportElement x="180" y="0" width="190" height="20" uuid="0e966a44-f669-41b7-bfa6-3e4b46aa7afe"/>
        <box>
          <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
          <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
          <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
          <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
        </box>
        <textElement textAlignment="Right" verticalAlignment="Middle">
          <font size="14" isItalic="true"/>
        </textElement>
        <text><![CDATA[Detail Band]]></text>
      </staticText>
      <textField isBlankWhenNull="true">
        <reportElement x="0" y="0" width="90" height="20" uuid="22e1a798-a7f6-4cc3-a3ba-85128638b4df"/>
        <box leftPadding="2">
          <pen lineWidth="0.5"/>
        </box>
        <textElement verticalAlignment="Middle"/>
        <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
      </textField>
      <textField isBlankWhenNull="true">
        <reportElement x="90" y="0" width="90" height="20" uuid="48e5421c-23c6-46be-bf66-86aa9ef312a6"/>
        <box rightPadding="2">
          <pen lineWidth="0.5"/>
        </box>
        <textElement textAlignment="Right" verticalAlignment="Middle"/>
        <textFieldExpression><![CDATA[$F{price}]]></textFieldExpression>
      </textField>
    </band>
  </detail>
  <columnFooter>
    <band height="20" splitType="Stretch">
      <staticText>
        <reportElement x="180" y="0" width="190" height="20" uuid="bdf57bf9-a065-4a82-886a-a891aaa55162"/>
        <box>
          <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
          <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
          <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
          <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
        </box>
        <textElement textAlignment="Right" verticalAlignment="Middle">
          <font size="14" isItalic="true"/>
        </textElement>
        <text><![CDATA[Column Footer Band]]></text>
      </staticText>
    </band>
  </columnFooter>
  <pageFooter>
    <band height="20">
      <property name="com.jaspersoft.studio.unit.height" value="px"/>
      <staticText>
        <reportElement x="180" y="0" width="190" height="20" uuid="50d51fb4-1eff-4e44-bb38-9e56af47c675"/>
        <box>
          <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
          <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
          <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
          <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
        </box>
        <textElement textAlignment="Right" verticalAlignment="Middle">
          <font size="14" isItalic="true"/>
        </textElement>
        <text><![CDATA[Page Footer Band]]></text>
      </staticText>
      <textField>
        <reportElement x="388" y="0" width="100" height="20" uuid="34e1ed10-9863-4a36-a477-8a3233bcea0e">
          <property name="com.jaspersoft.studio.unit.height" value="px"/>
        </reportElement>
        <textElement textAlignment="Right"/>
        <textFieldExpression><![CDATA["Page " + $V{PAGE_NUMBER}]]></textFieldExpression>
      </textField>
      <textField evaluationTime="Report">
        <reportElement x="488" y="0" width="66" height="20" uuid="1df7f198-ac37-47a6-9ec9-9c735d2078d7">
          <property name="com.jaspersoft.studio.unit.height" value="px"/>
        </reportElement>
        <textElement textAlignment="Left"/>
        <textFieldExpression><![CDATA[" of " + $V{PAGE_NUMBER}]]></textFieldExpression>
      </textField>
    </band>
  </pageFooter>
</jasperReport>