How to generate Code 128 Barcodes in JasperReport?
Here is a simple example to create Code 128 barcode in iReport. More details here: tutorial to generate and print barcodes in iReport reprts.
One more guide for you to stream Code 128 barcodes in iReport using Java Barcode Generator buildin Servlet web application.
One more guide for you to stream Code 128 barcodes in iReport using Java Barcode Generator buildin Servlet web application.
- Create a new report, and add barcode.jar to report classpath from the downloaded trial package
- Drag an image item to report from Palette, and click "Cancel" in pop-up window
- Right click on the image and edit its "Properties", and set property "Expression Class" to "net.sf.jasperreports.engine.JRRenderable";
- To input Code 128 barcode data manually, set property "Image Expression" to "new com.onbarcode.barcode.jasper.JasperRenderer(com.onbarcode.barcode.jasper.BarcodeGenerator.draw(12, "12345", "", "X=2&Y=60"))"
- To print Code 128 with data from database, please set property "Image Expression" to "new com.onbarcode.barcode.jasper.JasperRenderer(
com.onbarcode.barcode.jasper.BarcodeGenerator.draw(12, $F{data field} + "", "", "X=2&Y=60"))";
BarcodeGenerator.draw method accepts 4 parameters:
the second is the barcode data to encode, in String,
the last is the barcode settings String. More details about barcode settings string, please check the Code 128 property table below
Code 128 Barcode Data Character Encoding in JasperReport Reports
This section describes supported characters, code sets, function characters, and mandatory check digit behavior for Code 128 in JasperReport reports. All settings are managed in Report Item Property.
Code 128 Supported Character Set
Code 128 supports a full range of characters for enterprise reporting:
- All 128 full ASCII characters defined in ISO/IEC 646
- Extended ASCII characters (128–255 from ISO/IEC 8859‑1 Latin Alphabet 1) using FNC4
- Four non‑data function characters
- Four code set selection characters
- Three start characters
- One stop character
Code 128 Code Sets in JasperReport Reports
Code 128 uses three dedicated code sets to optimize data density:
- Code Set A
- Code Set B
- Code Set C
- Use barcode type CODE128 to enable automatic code set selection.
- The JasperReports Library automatically chooses the optimal starting code set.
- To enforce a specific code set, select one of these types in Report Item Property:
- CODE128A
- CODE128B
- CODE128C
Code 128 Function Characters
Function characters (FNC) control scanner behavior and are automatically encoded by the JasperReports engine:
- FNC1: Used for GS1 system data (GS1‑128 / EAN‑128)
- FNC2: Used for message append operations
- FNC3: Used for scanner initialization or reprogramming instructions
- FNC4: Used to extend encoding to ISO/IEC 8859‑1 extended ASCII
Code 128 Check Digit Character
Code 128 includes a mandatory check digit calculated using Modulo 103.
- The check digit is not visible in HRI text.
- It is not returned as part of decoded data by scanners.
- The JasperReports Library always computes and appends the check digit automatically.
- No manual checksum property is required or available for Code 128.
Code 128 Barcode Human Readable Interpretation (HRI)
The Human Readable Interpretation (HRI) is the text displayed below or above the Code 128 symbol.Position and appearance are configured in the Report Item Property panel.
Display HRI Below the Barcode
Display HRI Above the Barcode
Display HRI Below the Barcode
- Select the Code 128 Barcode Component.
- In Report Item Property, enable text display.
- Set text alignment and default position (below the symbol).
- Use Padding and Margin settings to control spacing.
Display HRI Above the Barcode
- Enable HRI text in the property panel.
- Adjust vertical position using margin or offset properties.
- Use negative text margin values to shift text above the barcode.
- Configure TopMargin and BottomMargin to avoid overlapping the quiet zone.
Code 128 Barcode Dimension and Size Settings
You can fully control Code 128 size and layout in JasperReport reports through Report Item Property.
Available dimension properties:
Note: A quiet zone smaller than 10X may result in unreliable scanning, especially in printed reports.
- UOM (Unit of Measure): PIXEL, CM, or INCH
- X: Width of the narrowest bar module
- Y: Height of the barcode module
- LeftMargin, RightMargin: Quiet zone (minimum recommended width = 10X)
- TopMargin, BottomMargin: Vertical spacing around the symbol
- Set UOM to your preferred unit.
- Define X (module width) based on printing or scanning requirements.
- Set Y for appropriate barcode height.
- Apply minimum quiet zone margins to ensure scan reliability.
- Preview and adjust using Preview Mode before final export.
Note: A quiet zone smaller than 10X may result in unreliable scanning, especially in printed reports.
JasperReport Barcode Code 128 Generator - Property Settings
| Category | Properties | Value | Comments | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Basic | Property: Data | Type: expression
Default: ""
|
Barcode value to encode.
Code 128 Valid Data Char Set:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Property: Add CheckSum Char |
Type: boolean
Default: false |
Add CheckSum Char property is not applied here. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Code 128 Special |
Property: Code 128 Code Set |
Type: int
Default: AUTO |
4 valid code sets: AUTO, A, B, C | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Property: processTilde |
Type: boolean
Default: false |
Set the processTilde property to true, if you want use the tilde character "~"
to specify special characters in the input data.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Barcode Size Related |
Property: Unit of Measure |
Type: int
Default: 0 (pixel) |
Unit of meature for all size related settings in the library.
Valid values: 0: pixel; 1: inch; 2: cm. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Property: X |
Type: float
Default: 1 |
Width of barcode bar module (narrow bar), default is 1 pixel | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Property: Y | Type: float
Default: 30 |
Height of barcode bar module, default is 30 pixel | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Property: Left Margin | Type: float
Default: 0 |
Barcode image left margin size. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Property: Right Margin | Type: float
Default: 0 |
Barcode image right margin size. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Property: Top Margin | Type: float
Default: 0 |
Barcode image top margin size. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Property: Bottom | Type: float
Default: 0 |
Barcode image bottom margin size. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Property: Resolution (in dpi) | Type: int
Default: 72 |
Barcode image resolution in DPI (Dots per inch). | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Property: Rotate |
Type: int
Default: 0 (rotate angle in 0 degree) |
Valid values:
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Font Style |
Property: Show Text |
Type: boolean
Default: true |
If true, display barcode data text under the barcode, otherwise do not display. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Property: Text Font Size |
Type: int
Default: 11 |
Barcode text font size. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Property: Text Margin |
Type: float
Default: 6 |
Space between barcode and barcode data text, default is 6 pixels | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Summary
This guide has walked you through the complete process of designing, configuring, and generating Code 128 barcodes in JasperReport reports using Jaspersoft Studio and iReport Designer.
Main contents covered:
- Code 128 is implemented as a Barcode Component within JRXML report designs.
- Automatic code set selection and check digit calculation ensure correct barcode structure.
- HRI text position, size, margins, and dimensions are fully customizable.
- GS1 data is supported natively via GS1‑128 / EAN‑128 mode.
- Preview and export workflows use standard JasperReport tools including Preview Mode, JASPER compilation, and PDF Exporter.
