Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
315 views
in Technique[技术] by (71.8m points)

java - How to print several reports with barcode or several barcodes in one report

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...