How to print several reports with barcode \ or sev

2019-01-15 11:30发布

I've got a barcode report which is using a sequence (Oracle backend) to generate my barcode numbers.

This is in my query:

SELECT to_char(PALLET_ID_NO_SEQ.nextval) FROM dual

I placed this field in designer window which will display the barcode value.

I have an image with expression:

new com.pepkorit.BarbecueRotateRenderer(
    net.sourceforge.barbecue.BarcodeFactory.createCode128C(
    $F{TO_CHAR(PALLET_ID_NO_SEQ.NEXTVAL)}), false, true, 1, 50, 190, 50)

The above is the barcode using the sequence value.

I want to be able to say to print/generate 100 or more reports. At this moment I'm able to generate only one report at a time.

So my first guess is to get a parameter that prompts the user a value and that value will indicate how many barcodes to be printed and each with an individual number.

I'm not sure that my ideas about solving this problem are right and how to do it.

Can someone please help?

2条回答
Bombasti
2楼-- · 2019-01-15 12:04

One possible way :

1) Create a bean :

public class Entity {
    private Image image;

    public Entity(Image image) {
        this.image = image;
    }

    public Entity() {}

    public Image getImage() {
        return image;
    }

    public void setImage(Image image) {
        this.image = image;
    }
}

2) Populate 100 of such beans with data ( image of your barcode )

3) Create a jasper report like this :

<?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="ExtendedPortReport" language="groovy" pageWidth="1190" pageHeight="842" orientation="Landscape" columnWidth="1150" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <property name="ireport.zoom" value="0.75"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <field name="image" class="java.awt.Image"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <detail>
        <band height="750" splitType="Stretch">
            <image scaleImage="RetainShape" hAlign="Center" vAlign="Top" isUsingCache="true" onErrorType="Blank">
                <reportElement isPrintRepeatedValues="false" x="0" y="0" width="1150" height="750"/>
                <box>
                    <pen lineWidth="0.0"/>
                    <topPen lineWidth="0.0"/>
                    <leftPen lineWidth="0.0"/>
                    <bottomPen lineWidth="0.0"/>
                    <rightPen lineWidth="0.0"/>
                </box>
                <imageExpression><![CDATA[$F{image}]]></imageExpression>
            </image>
        </band>
    </detail>
</jasperReport>

4) Set type of image field ( in the report ) to java.awt.Image

5) Create the report as a desktop application ( in your case, use other ways, if needed )

    final JasperReport report = (JasperReport)JRLoader.loadObjectFromFile(MAIN_BINARY_PATH);
    final JRDataSource dataSource = new JRBeanCollectionDataSource(/*there is your list of entities*/);

    final JasperPrint jasperPrint = JasperFillManager.fillReport(report, null, dataSource);
    JRViewer viewer = new JRViewer(jasperPrint);
    //add viewer to a frame/panel
查看更多
淡お忘
3楼-- · 2019-01-15 12:13

It can be easily done with small modifying of your query without programming in several ways.

Solution 1. Using single report with Barcode component in Detail band

You can use single report's template for generating several barcodes in one report.

In this case the queryString expression (works for Oracle DB) will be like this:

SELECT seq_barcode.nextval AS barcode, rownum FROM dual CONNECT BY LEVEL <= $P{quantity}

- it is generates a value from the sequence as many times as you need. The $P{quantity} parameter determines the number of rows (barcodes) to be generated.

The working rjxml file:

<jasperReport ...>
    <parameter name="quantity" class="java.lang.Integer">
        <defaultValueExpression><![CDATA[20]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[SELECT seq_barcode.nextval AS barcode, rownum FROM dual CONNECT BY LEVEL <= $P{quantity}]]>
    </queryString>
    <field name="BARCODE" class="java.lang.Integer"/>
    <field name="ROWNUM" class="java.lang.Integer"/>
    <title>
        <band height="82" splitType="Stretch">
            <textField>
                <reportElement x="145" y="18" width="240" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA["The number of barcodes is: " + $P{quantity}]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <detail>
        <band height="47" splitType="Stretch">
            <componentElement>
                <reportElement x="145" y="10" width="200" height="28"/>
                <jr:barbecue xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" type="2of7" drawText="false" checksumRequired="false">
                    <jr:codeExpression><![CDATA[$F{BARCODE}]]></jr:codeExpression>
                </jr:barbecue>
            </componentElement>
        </band>
    </detail>
</jasperReport>

The result will be ($P{quantity} == 5):

The result via preview in iReport


In your case the queryString expression will be like this:

SELECT to_char(PALLET_ID_NO_SEQ.nextval) AS barcode, rownum FROM dual CONNECT BY LEVEL <= $P{quantity}

and the expression of Barcode component will be:

new com.pepkorit.BarbecueRotateRenderer(
    net.sourceforge.barbecue.BarcodeFactory.createCode128C($F{barcode}),
    false, true, 1, 50, 190, 50)

Solution 2. Using Group Header band

You can use the same queryString expression as in the first solution. The group on rownum field will help us to generate single report with many barcodes belonging to its own group (one group - one barcode). The Barcode component should be placed to the Group Header band.

Using the isStartNewPage property we can manage to generate group on new page or not.

The rjxml file:

<jasperReport ...>
    <parameter name="quantity" class="java.lang.Integer">
        <defaultValueExpression><![CDATA[20]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[SELECT seq_barcode.nextval AS barcode, rownum FROM dual CONNECT BY LEVEL <= $P{quantity}]]>
    </queryString>
    <field name="BARCODE" class="java.lang.Integer"/>
    <field name="ROWNUM" class="java.lang.Integer"/>
    <group name="rownumGroup" isStartNewPage="true">
        <groupExpression><![CDATA[$F{ROWNUM}]]></groupExpression>
        <groupHeader>
            <band height="50">
                <componentElement>
                    <reportElement x="145" y="11" width="200" height="28"/>
                    <jr:barbecue xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" type="2of7" drawText="false" checksumRequired="false">
                        <jr:codeExpression><![CDATA[$F{BARCODE}]]></jr:codeExpression>
                    </jr:barbecue>
                </componentElement>
            </band>
        </groupHeader>
    </group>
    <title>
        <band height="82" splitType="Stretch">
            <textField>
                <reportElement x="145" y="18" width="240" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA["The number of barcodes is: " + $P{quantity}]]></textFieldExpression>
            </textField>
        </band>
    </title>
</jasperReport>

In case isStartNewPage="false" for group rownumGroup the result will be ($P{quantity}== 7):

The result via preview in iReport, isStartNewPage="false"

In case isStartNewPage="true" for group rownumGroup the result will be ($P{quantity} == 5):

The result via preview in iReport, isStartNewPage="true"

Solution 3. Using subreport

We can add Subreport component to the Detail band (see first solution) or Group Header (see second solution) band. In this case you can add to the subreport not only the Barcode component, but everything you want.

查看更多
登录 后发表回答