JasperReports Ultimate Guide - Sample Reference - Schema Reference - Configuration Reference - API (Javadoc)

JasperReports - Barcode4J Sample (version 4.6.0)


Shows how barcodes could be included in reports using the Barcode4J component.

Download All Sample Source Files
Browse Sample Source Files on SVN


Main Features in This Sample

Barcodes

Barcodes (Barcode4J Custom Component)


top

BarcodesDocumented by Sanda Zaharia


Description / Goal
Shows how to render barcodes in a report, using the barcode components that are shipped with the JasperReports library.

Since
3.5.2

Other Samples
/demo/samples/barbecue


Barcode Overview

A barcode denotes an encoded graphical representation of data which can be read/decoded using dedicated scanning devices such as barcode scanners. Due to its exceptional ability to completely characterize a given product, the barcode technology became one of the most common product registering technologies. It consists in computing a graphical "fingerprint" for a given object based on a unique digit/character sequence mapped to the object. Once generated, the barcode uniquely identifies the product. When scanned, the barcode allows retrieving the original data it was built on, in order to be visualized or printed out.

Barcode Symbologies

The whole "fingerprint" graphics is computed from elementary fingerprints of single digits/characters in the sequence. There are several ways to map individual characters or digits to a graphical representation. These mappings are specified in the so-called barcode symbologies. Some very common symbology categories are:
  • linear vs. 2D
    • linear - represent data by varying the widths and spacings of parallel lines. Are optimized for laser scanners.
    • 2D - use rectangles, dots, hexagons and other 2D geometric patterns to represent data. Are optimized for image-based scanners.
  • continuous vs. discrete
    • continuous - one character ends with a space and the next begins with the next bar, etc.
    • discrete - any character begins and ends with bars; the space between consecutive characters is ignored
  • two-width vs. many-width
    • two-width - bars and spaces are either wide or narrow. The exact width of a bar/space has no meaning by itself.
    • many-width - bars and spaces may have only predefined width values, multiple of a basic width called "module". Usually a bar/space width can count up to 4 modules.
Some of linear symbologies are highly standardized, some others are niche-oriented, most of them presenting mixed features. Below are few examples:
  • Universal Product Code (UPC) - International Standard ISO/IEC 15420. There are 5 versions of the UPC symbology designed for future industry requirements: UPC A, B, C, D, E.
  • Codabar - Outdated format used in libraries, blood banks and on air waybills. Not standardized.
  • Code 25 – Interleaved 2 of 5 - Used in wholesales and libraries. International standard ISO/IEC 16390
  • Code 39 - International standard ISO/IEC 16388
  • Code 128 – International Standard ISO/IEC 15417
  • EAN-8 - International Standard ISO/IEC 15420
  • EAN-13 - International Standard ISO/IEC 15420
  • JAN - Available for Japan, compatible with EAN-13 (ISO/IEC 15420)
  • etc.
Some of the 2D symbologies are enumerated below:
  • Aztec Code - International Standard ISO/IEC 24778
  • Codablock - Not standardized.
  • Code 16K - Based on linear Code 128.
  • Data Matrix - Used throughout the United States. Standard ISO/IEC 16022:2000(E)
  • EZcode - Designed for decoding by cameraphones.
  • PDF417 - Standard ISO/IEC 15438:2001(E)
  • etc.

top

Barcodes (Barcode4J Custom Component)Documented by Sanda Zaharia


Description / Goal
Shows how to render barcodes in a report, using the barcode component based on the Barcode4J library.

Since
3.5.2


The Barcode4J Library

Today barcodes are present almost everywhere: in wholesales as well as in retails, on the bills, in airports, libraries, banks, hospitals, hotels, theaters, amusement parks, on the internet, and the list could continue... Due to this ubiquity, there is an increasing need of barcode components in the reporting area. Powerful reporting engines should now provide support for as many as possible barcode symbologies. To cover a large category of customer needs, JR comes with 2 built-in barcode components, and in addition, it also makes room for other complementary implementations. This sample illustrates how to use the built-in component based on the open source Barcode4J library.
The Barcode4J library supports the following symbologies:
  • linear:
    • Code 25 – Interleaved 2 of 5
    • Codabar
    • ITF-14
    • Code 39 - International standard ISO/IEC 16388
    • Code 128 – International Standard ISO/IEC 15417
    • EAN-128, GS1-128 (based on Code 128)
    • EAN-13 and EAN-8 (with supplementals)
    • UPC-A and UPC-E (with supplementals)
    • POSTNET
    • Royal Mail Customer Barcode (Four State)
    • USPS Intelligent Mail (4-State Customer Barcode)
  • 2D:
    • DataMatrix
    • PDF 417
It also provides support for multiple output formats:
  • SVG
  • EPS
  • Bitmap images (such as PNG or JPEG)
  • Java2D
  • Text - for testing and debugging only
The Barcode4J Component

Each of supported symbologies enumerated above is characterized by a specific structure of elements and attributes described in the components.xsd schema. All of them are based on the same Barcode4j parent element, and differences are given by their specific extracontent: The basic Barcode4j structure is characterized by the following series of elements:
  • codeExpression
  • patternExpression
and contains the following attributes:
  • evaluationTime - the evaluationTime attribute of any JR report element
  • evaluationGroup - the evaluationGroup attribute of any JR report element
  • orientation - specifies the rotation (in degrees) of the generated barcode image. Allowed values are: 0, 90, 180, 270. The default value is 0.
  • moduleWidth - specifies the narrow module width (in points). This will be converted in mm at runtime.
  • textPosition - specifies the text presence and its position relative to the bar. Allowed values are: none, bottom, top.
  • quietZone - specifies the width of the quiet zone left and right of the barcode in points. This will be converted in mm at runtime.
  • verticalQuietZone - specifies the height of the vertical quiet zone above and below the barcode in points. This will be converted in mm at runtime.
Another attribute available for almost all Barcode4j elements is the checksumMode. Allowed values are auto, ignore, add and check.

Barcode4J Samples

Some examples of howto use the available Barcode4j components are given in the JRXML sample:
<componentElement>
  <reportElement style="Barcode" x="130" y="100" width="200" height="30"/>
  <c:Code128 xmlns:c="http://jasperreports.sourceforge.net/jasperreports/components" 
    xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components 
    http://jasperreports.sourceforge.net/xsd/components.xsd" 
    moduleWidth="1.0">
    <c:codeExpression><![CDATA["0123456789"]] ></c:codeExpression>
  </c:Code128>
</componentElement>
...
<componentElement>
  <reportElement style="Barcode" x="130" y="135" width="200" height="30"/>
  <c:Codabar xmlns:c="http://jasperreports.sourceforge.net/jasperreports/components" 
    xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components 
    http://jasperreports.sourceforge.net/xsd/components.xsd" moduleWidth="1.2" 
    textPosition="none" wideFactor="4.0">
    <c:codeExpression><![CDATA["01234567890"]] ></c:codeExpression>
  </c:Codabar>
</componentElement>
...
<componentElement>
  <reportElement style="Barcode" x="130" y="170" width="70" height="40"/>
  <c:DataMatrix xmlns:c="http://jasperreports.sourceforge.net/jasperreports/components" 
    xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components 
    http://jasperreports.sourceforge.net/xsd/components.xsd" 
    moduleWidth="4.0">
    <c:codeExpression><![CDATA["JasperReports"]] ></c:codeExpression>
  </c:DataMatrix>
</componentElement>
...
<componentElement>
  <reportElement style="Barcode" x="130" y="215" width="250" height="30"/>
  <c:EAN128 xmlns:c="http://jasperreports.sourceforge.net/jasperreports/components" 
    xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components 
    http://jasperreports.sourceforge.net/xsd/components.xsd" 
    moduleWidth="1.4" checksumMode="check">
    <c:codeExpression><![CDATA["0101234567890128"]] ></c:codeExpression>
  </c:EAN128>
</componentElement>
...
<componentElement>
  <reportElement style="Barcode" x="130" y="250" width="400" height="30"/>
  <c:Code39 xmlns:c="http://jasperreports.sourceforge.net/jasperreports/components" 
    xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components 
    http://jasperreports.sourceforge.net/xsd/components.xsd">
    <c:codeExpression><![CDATA["0123456789"]] ></c:codeExpression>
  </c:Code39>
</componentElement>
...
<componentElement>
  <reportElement style="Barcode" x="130" y="285" width="400" height="30"/>
  <c:USPSIntelligentMail xmlns:c="http://jasperreports.sourceforge.net/jasperreports/components" 
    xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components 
    http://jasperreports.sourceforge.net/xsd/components.xsd" 
    ascenderHeight="8.0" trackHeight="10.0">
    <c:codeExpression><![CDATA["00040123456200800001987654321"]] ></c:codeExpression>
  </c:USPSIntelligentMail>
</componentElement>
...
<componentElement>
  <reportElement style="Barcode" x="130" y="320" width="400" height="30"/>
  <c:RoyalMailCustomer xmlns:c="http://jasperreports.sourceforge.net/jasperreports/components" 
    xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components 
    http://jasperreports.sourceforge.net/xsd/components.xsd" 
    ascenderHeight="8.0" intercharGapWidth="2.5" trackHeight="10.0">
    <c:codeExpression><![CDATA["0123456789"]] ></c:codeExpression>
  </c:RoyalMailCustomer>
</componentElement>
...
<componentElement>
  <reportElement style="Barcode" x="130" y="355" width="400" height="30"/>
  <c:Interleaved2Of5 xmlns:c="http://jasperreports.sourceforge.net/jasperreports/components" 
    xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components 
    http://jasperreports.sourceforge.net/xsd/components.xsd">
    <c:codeExpression><![CDATA["0123456789"]] ></c:codeExpression>
  </c:Interleaved2Of5>
</componentElement>
...
<componentElement>
  <reportElement style="Barcode" x="130" y="390" width="400" height="30"/>
  <c:UPCA xmlns:c="http://jasperreports.sourceforge.net/jasperreports/components" 
    xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components 
    http://jasperreports.sourceforge.net/xsd/components.xsd">
    <c:codeExpression><![CDATA["01234567890"]] ></c:codeExpression>
  </c:UPCA>
</componentElement>
...
<componentElement>
  <reportElement style="Barcode" x="130" y="425" width="400" height="30"/>
  <c:UPCE xmlns:c="http://jasperreports.sourceforge.net/jasperreports/components" 
    xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components 
    http://jasperreports.sourceforge.net/xsd/components.xsd">
    <c:codeExpression><![CDATA["01234133"]] ></c:codeExpression>
  </c:UPCE>
</componentElement>
....
<componentElement>
  <reportElement style="Barcode" x="130" y="460" width="400" height="30"/>
  <c:EAN13 xmlns:c="http://jasperreports.sourceforge.net/jasperreports/components" 
    xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components 
    http://jasperreports.sourceforge.net/xsd/components.xsd">
    <c:codeExpression><![CDATA["012345678901"]] ></c:codeExpression>
  </c:EAN13>
</componentElement>
...
<componentElement>
  <reportElement style="Barcode" x="130" y="495" width="400" height="30"/>
  <c:EAN8 xmlns:c="http://jasperreports.sourceforge.net/jasperreports/components" 
    xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components 
    http://jasperreports.sourceforge.net/xsd/components.xsd">
    <c:codeExpression><![CDATA["01234565"]] ></c:codeExpression>
  </c:EAN8>
</componentElement>
...
<componentElement>
  <reportElement style="Barcode" x="130" y="530" width="400" height="20"/>
  <c:POSTNET xmlns:c="http://jasperreports.sourceforge.net/jasperreports/components" 
    xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components 
    http://jasperreports.sourceforge.net/xsd/components.xsd" 
    shortBarHeight="10.0" checksumMode="add">
    <c:codeExpression><![CDATA["01234"]] ></c:codeExpression>
  </c:POSTNET>
</componentElement>
...
<componentElement>
  <reportElement style="Barcode" x="130" y="555" width="400" height="30"/>
  <c:PDF417 xmlns:c="http://jasperreports.sourceforge.net/jasperreports/components" 
    xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components 
    http://jasperreports.sourceforge.net/xsd/components.xsd" 
    moduleWidth="4.0">
    <c:codeExpression><![CDATA["JasperReports"]] ></c:codeExpression>
  </c:PDF417>
</componentElement>
Running the Sample

Running the sample requires the Apache Ant library. Make sure that ant is already installed on your system (version 1.5 or later).
In a command prompt/terminal window set the current folder to demo/samples/barcode4j within the JasperReports source project and run the > ant test view command.
It will generate all supported document types containing the sample report in the demo/samples/barcode4j/build/reports directory.
Then the report will open in the JasperReports internal viewer.



© 2001- Jaspersoft Corporation www.jaspersoft.com