How to Generate Code 39 Barcode in JasperReport?
Here are simple steps to create a Code 39 barcode in iReport. View details here: tutorial to generate and print barcodes in iReport reprts.
One more guide for you to stream Code 39 barcodes in iReport using Java Barcode Generator buildin Servlet web application.
One more guide for you to stream Code 39 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 in Palette to report
- Click "Cancel" button, in "Select an image file" window
- Right click on the image and edit its "Properties", and set property "Expression Class" to "net.sf.jasperreports.engine.JRRenderable";
- To input Code 39 barcode data manually, set property "Image Expression" to "new com.onbarcode.barcode.jasper.JasperRenderer(
com.onbarcode.barcode.jasper.BarcodeGenerator.draw(2, "12345", "", "X=2&Y=60"))" - To print Code 39 with data from database, please set property "Image Expression" to "new com.onbarcode.barcode.jasper.JasperRenderer(
com.onbarcode.barcode.jasper.BarcodeGenerator.draw(2, $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 39 property table below
Code 39 Barcode Data Characters Encoding in JasperReport Reports
Code 39 barcodes support two main encoding modes: Standard Mode and Full ASCII Mode. Both modes are configurable in the Report Item Property panel of Jaspersoft Studio or iReport Designer.
Code 39 Standard Mode
Code 39 Standard Mode supports 43 characters, making it ideal for basic alphanumeric encoding. Here's how to configure it in your JasperReport report:
Supported characters in Standard Mode:
Example configuration:
Note: Do not include the start/stop character (*) in the "Data" field. The JasperReports Barcode Library automatically adds it to ensure scannability.
Supported characters in Standard Mode:
- Numeric digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
- Uppercase letters (A - Z)
- Special characters: - (Dash), $ (Dollar), % (Percentage), (Space), . (Point), / (Slash), + (Plus)
- Start/Stop character (asterisk *—automatically added by the JasperReports Library)
- Select the Code 39 Barcode Component in your JRXML template.
- Open the Report Item Property panel and set the "Barcode Type" to "CODE39" (this enables Standard Mode).
- Enter the data to encode in the "Data" field (static text or dynamic Expression referencing a Field/Parameter).
- Save the JRXML template to apply the settings.
Example configuration:
- Barcode Type: CODE39
- Data: "ABC-12345" (static) or $F{StandardCode39Data} (dynamic)
Note: Do not include the start/stop character (*) in the "Data" field. The JasperReports Barcode Library automatically adds it to ensure scannability.
Code 39 Full ASCII Mode
Code 39 Full ASCII Mode supports the full 128-character ASCII set (per ISO 646 IRV), including lowercase letters, additional special characters, and control characters. This mode is ideal for advanced encoding needs. Here's how to configure it:
Configuration steps:
Example configuration:
Note: Ensure your barcode scanner supports Full ASCII Mode to correctly decode the data. Some older scanners may only support Standard Mode.
Configuration steps:
- Select the Code 39 Barcode Component in your JRXML template.
- Open the Report Item Property panel and set the "Barcode Type" to "CODE39EX" (this enables Full ASCII Mode).
- Enter the data to encode in the "Data" field—this can include lowercase letters (e.g., "abc-12345") and other ASCII characters.
- Save the JRXML template to apply the settings.
Example configuration:
- Barcode Type: CODE39EX
- Data: "abc-12345" (static) or $F{FullAsciiCode39Data} (dynamic)
Note: Ensure your barcode scanner supports Full ASCII Mode to correctly decode the data. Some older scanners may only support Standard Mode.
Code 39 Barcode Check Digit
Code 39 does not include a check digit by default. However, for enhanced data security, you can enable a modulo 43 (MOD43) check digit. This setting is configured in the Report Item Property panel:
To enable the check digit:
Note: The check digit is calculated based on the input data and is not part of the original "Data" field. It is added automatically by the JasperReport Barcode Library.
To enable the check digit:
- Select the Code 39 Barcode Component and open the Report Item Property panel.
- Locate the "AddCheckSum" property and set it to "true".
- Save the JRXML template. The JasperReport Barcode Library will automatically calculate and append the MOD43 check digit.
- Barcode Type: CODE39
- Data: "ABC-123"
- AddCheckSum: true
Note: The check digit is calculated based on the input data and is not part of the original "Data" field. It is added automatically by the JasperReport Barcode Library.
Hide Code 39 Check Digit in HRI
You can enable the check digit while hiding it from the Human Readable Interpretation (HRI). The text displayed below/above the barcode. This is configured in the Report Item Property panel:
Steps to hide the check digit in HRI:
Note: Hiding the check digit in HRI is useful for maintaining a clean report design while still ensuring data integrity via the check digit.
Steps to hide the check digit in HRI:
- Ensure "AddCheckSum" is set to "true" (as shown in Section 2.3).
- Locate the "ShowCheckSumChar" property in the Report Item Property panel and set it to "false".
- Save the JRXML template. The check digit will be encoded in the barcode but not displayed in the HRI text.
Note: Hiding the check digit in HRI is useful for maintaining a clean report design while still ensuring data integrity via the check digit.
Code 39 Barcode Start/Stop Characters
Code 39 uses the asterisk (*) as its start and stop characters. These characters are required for scanning but should not be included in the "Data" field. You can control their visibility in the HRI text via the Report Item Property panel:
Note: Hiding the start/stop characters in HRI is recommended for most report designs, as the asterisks are not part of the actual data and can clutter the layout.
- The start/stop characters (*) are automatically added by the JasperReport Barcode Library. Do not include them in the "Data" field.
- To show or hide the start/stop characters in the HRI text, use the "ShowStartStopInText" property.
Note: Hiding the start/stop characters in HRI is recommended for most report designs, as the asterisks are not part of the actual data and can clutter the layout.
Code 39 Barcode Dimension Size in JasperReport Reports
You can fully customize the dimension size of Code 39 barcodes in JasperReport reports, including unit of measure, bar width, height, wide/narrow ratio, intercharacter gap, and margins.
The following settings are available in the Report Item Property panel to customize Code 39 dimensions.
The following settings are available in the Report Item Property panel to customize Code 39 dimensions.
- UOM (Unit of Measure): Select the unit for dimension settings: PIXEL, CM, or INCH. Default is PIXEL.
- X: Width of the narrow bar module (the smallest unit of the barcode). The minimum width is defined by your application’s specifications.
- Y: Height of the barcode module (the vertical length of the bars).
- N: Wide/narrow bar ratio. Valid values range from 2.0 to 3.0 (inclusive). Default is 2.0.
- I: Width of the intercharacter gap (space between two barcode characters).
Minimum gap is equal to X (narrow bar width).
Maximum gap: For X < 0.287mm, it is 5.3X; for X ≥ 0.287mm, it is 1.52mm or 3X (whichever is greater). - LeftMargin, RightMargin: Quiet zone (empty space) around the Code 39. The minimum width of each margin is 10X (10 times the narrow bar width X).
- TopMargin, BottomMargin: Vertical spacing around the barcode (optional, but recommended to avoid overlapping with other report elements).
Customize Code 39 Barcode Features in JasperReport Reports
You can further customize Code 39 barcodes in JasperReport reports by adjusting the wide/narrow bar ratio (N) and intercharacter gap (I). These settings are critical for optimizing barcode readability and fitting the barcode into your report layout.
Code 39 Wide Bar vs Narrow Bar Ratio (N)
The ratio (N) defines the relationship between the width of wide bars and narrow bars. This setting directly impacts barcode scannability and appearance:
Note: Choose a ratio based on your scanning environment. A higher ratio (e.g., 3.0) is more readable for low-resolution printers or scanners.
- Valid values: 2.0 to 3.0 (inclusive). Default is 2.0.
- A ratio of 2.0 means wide bars are twice as wide as narrow bars.
- A ratio of 3.0 means wide bars are three times as wide as narrow bars.
Note: Choose a ratio based on your scanning environment. A higher ratio (e.g., 3.0) is more readable for low-resolution printers or scanners.
Code 39 Intercharacter Space (I)
The intercharacter space (I) is the gap between two consecutive barcode characters. It is a multiple of the narrow bar width (X) and impacts barcode readability:
Note: Increasing the intercharacter gap (I) can improve readability but will increase the overall width of the barcode. Ensure the barcode fits within your report's layout.
- Default value: 1.0f (gap equals the narrow bar width X).
- The gap must be at least X (minimum) and follow the maximum gap rules outlined in Section 3.1.
Note: Increasing the intercharacter gap (I) can improve readability but will increase the overall width of the barcode. Ensure the barcode fits within your report's layout.
JasperReport Barcode Code39 Generator - Property Settings
| Category | Properties | Value | Comments | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Basic | Property: Data | Type: expression
Default: ""
|
Barcode value to encode.
Code 39 Valid Data Char Set:
Code 39 extension Valid Data Char Set:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Property: Add CheckSum Char |
Type: boolean
Default: false |
Add CheckSum Char property is optional. Modulo 43 will be applied, if addCheckSum property is true. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Code 39 Special |
Property: Code 39 extension |
Type: boolean
Default: false |
If you want to encode any 128 ASCII characters, extension must be true. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Property: I |
Type: float
Default: 1.0f |
The space between 2 characters in code 39. This a multiple of X. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Property: N |
Type: float
Default: 2.0f |
Wide/narrow ratio, 2.0 - 3.0 inclusive, default is 2. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Property: Code39 Show Start Stop Chars |
Type: boolean
Default: true |
If true, display a * in the beginning and end of barcode text | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Property: Process Tilde |
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
Here we have explained step-by-step approach to generating and customizing Code 39 barcodes in JasperReport reports using Jaspersoft Studio or iReport Designer. We covered all key aspects, including basic generation, data encoding (Standard and Full ASCII modes), check digit configuration, start/stop character management, dimension customization, and advanced settings for wide/narrow ratio and intercharacter gap.
