Generating report as txt file - How to set the exact positions and sizes for texts in Column Header and Detail bands

Suraj picture Suraj · Oct 26, 2013 · Viewed 16.7k times · Source

We have a requirement where text reports are to be generated using JasperReports.
The precision has to be accurate, sample format is as follows

enter image description here

  1. Header user name has to start at column 0.
  2. Header last accessed must start at column 23.
  3. Header is active must start at column 34.
  4. Line separator must have 8 characters (---) and start at column 22
  5. Name filed must be left aligned and start at column 1
  6. Points field must be right aligned and end in column 45, with precision to 1 place.

The JasperReports, exportToText method does not satisfy the above criteria and repeated adjustment of the .jrxml is not serving the purpose.
We adjusted the character width and character height as well, but still there is incorrect spacing between rows and columns.

Does JasperReports API support such precision in text formats?
Is there any other API which can help us in achieving this format?

Answer

Alex K picture Alex K · Oct 26, 2013

Yes, you can achieve your design with JasperReports.

Steps in solving task.

  1. The first step. We should the numbers of characters in row and the number of rows on page.
    We can use pair of net.sf.jasperreports.export.text.page.width and net.sf.jasperreports.export.text.page.height report's properties for this purpose. You can also use net.sf.jasperreports.export.text.character.width and net.sf.jasperreports.export.text.character.height pair of report's properties instead of the first one.

  2. The second step. We known the page format size (for example in pixels. For A4 without margins it is: 555x802) and we set the number of characters in row and rows at page. We can calculate and set the exact values of staticText and textField elements positions (x, y coordinates and the width and height properties of element),

The sample

Sample datasouce

I've used the csv datasource (points.csv file):

USER_NAME,LAST_ACCESSED,IS_ACTIVE,POINTS
John Doe,10/26/2013,Y,87.9
Sarah Connor,10/23/2013,Y,80.5
Viktor Navorski,10/14/2013,Y,95.5
Forrest Gump,10/25/2013,Y,97.0
Raymond Babbitt,10/24/2013,Y,88.5
Thomas Crown,10/24/2013,Y,88.0
Danny Ocean,09/30/2013,Y,90.5

The report design

I've remove the margin and set the A4 report's format (555x802 pixels).

enter image description here

The jrxml file

<?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="text_output" language="groovy" pageWidth="555" pageHeight="802" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="9a0b1db2-1ed0-44e1-960b-4626cea0097d">
    <property name="net.sf.jasperreports.export.text.page.width" value="80"/>
    <property name="net.sf.jasperreports.export.text.page.height" value="40"/>
    <field name="USER_NAME" class="java.lang.String"/>
    <field name="LAST_ACCESSED" class="java.lang.String"/>
    <field name="IS_ACTIVE" class="java.lang.String"/>
    <field name="POINTS" class="java.lang.String"/>
    <columnHeader>
        <band height="43" splitType="Stretch">
            <staticText>
                <reportElement uuid="d065f8ed-0c05-47d0-a679-a392c9cfabe5" x="0" y="0" width="152" height="20"/>
                <textElement/>
                <text><![CDATA[USER_NAME]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="573550a9-5fc9-41ee-ae4f-7b550868c75d" x="152" y="0" width="143" height="20"/>
                <textElement/>
                <text><![CDATA[LAST_ACCESSED]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="4b1f5b5c-4c61-4159-9357-041fd8e3e775" x="295" y="0" width="138" height="20"/>
                <textElement/>
                <text><![CDATA[IS_ACTIVE]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="1c851f50-9ace-4e30-be1e-7d5454127db8" x="433" y="0" width="103" height="20"/>
                <textElement/>
                <text><![CDATA[POINTS]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="7e358a83-b348-40ca-b6f4-009d6bed4b24" x="0" y="20" width="100" height="23"/>
                <textElement/>
                <text><![CDATA[----------------------]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="523a7abd-0fbd-4b63-b8d5-2ed78ee795cc" x="295" y="20" width="100" height="23"/>
                <textElement/>
                <text><![CDATA[---------]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="7ebe2ec7-9450-4fdf-b99a-48af5fe3ce49" x="152" y="20" width="100" height="23"/>
                <textElement/>
                <text><![CDATA[-------------]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="155d3972-9d99-4d3b-bb41-05f08dcdd5fa" x="433" y="20" width="100" height="23"/>
                <textElement/>
                <text><![CDATA[--------]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement uuid="652256e2-0f1e-4848-b1d0-d064caaa5020" x="0" y="0" width="152" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{USER_NAME}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="380af714-4dbd-4e0f-ae23-bfada0449a2f" x="152" y="0" width="143" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{LAST_ACCESSED}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="752c0f18-2dea-4209-bd0d-d787d8c462a8" x="295" y="0" width="63" height="20"/>
                <textElement textAlignment="Right"/>
                <textFieldExpression><![CDATA[$F{IS_ACTIVE}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="40807ad0-58a4-42b7-9636-04f01c0c3bf5" x="433" y="0" width="54" height="20"/>
                <textElement textAlignment="Right"/>
                <textFieldExpression><![CDATA[$F{POINTS}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

The result - generated txt file via JRTextExporter

I've used iReport for generating output file. The contents of this txt file is:

The report generated as txt file

Using Java code

In case using Java code we should set for JRTextExporter this parameters: JRTextExporterParameter.PAGE_WIDTH and JRTextExporterParameter.PAGE_HEIGHT

The sample code:

public static void generateReport() throws JRException {
    JRCsvDataSource dataSource = new JRCsvDataSource(JRLoader.getLocationInputStream(csvFileName));
    dataSource.setRecordDelimiter("\r\n");
    dataSource.setUseFirstRowAsHeader(true);
    dataSource.setColumnNames(new String[]{ "USER_NAME", "LAST_ACCESSED", "IS_ACTIVE", "POINTS"});

    JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, dataSource);

    JRTextExporter exporter = new JRTextExporter();

    exporter.setParameter(JRTextExporterParameter.PAGE_WIDTH, 80);
    exporter.setParameter(JRTextExporterParameter.PAGE_HEIGHT, 40);
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
    exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outputFileName);
    exporter.exportReport();
}

In this sample I've used the same csv file as datasource.


How we can calculate the x coordinate

In your sample you want to start the second column with 23 symbols. Ok, let see my sample. What we have:

  • The page width is 555 pixels
  • The number of characters in row is 80

The width of one character in this case is 555/80=6,9375.
We have 22 symbols before the first symbol in the 2nd column. It is 22*6,9375=152,625 pixels. In my sample I've set the 152 for the x property of 2nd column header. In my sample the first symbol in the header of the 2nd column has position 23.

For the third column the calculated value is 43*6,9375=298,3125 and the real x in my sample is 295 (the 3rd column stars with 44 symbol).

As you can see the calculation is too accurate.


Notes:

You can find information about exporting to the text file here: Text Export Sample