|
| 1 | +--- |
| 2 | +title: "Customize Barcode Captions" |
| 3 | +description: "Learn how to add and customize independent captions above and below barcode images in Aspose.BarCode for Java." |
| 4 | +type: docs |
| 5 | +weight: 50 |
| 6 | +url: /java/developer-guide/barcode-generation/visual-parameters/customize-captions/ |
| 7 | +--- |
| 8 | + |
| 9 | +# Customize Barcode Captions |
| 10 | + |
| 11 | +Aspose.BarCode for Java supports two independent caption areas: |
| 12 | + |
| 13 | +- `CaptionAbove` renders text above the barcode; |
| 14 | +- `CaptionBelow` renders text below the barcode. |
| 15 | + |
| 16 | +Captions are independent from the encoded payload. They can contain titles, product names, instructions, warehouse locations, batch information, or any other descriptive text. |
| 17 | + |
| 18 | +The complete source code for the examples in this article is available on GitHub: |
| 19 | + |
| 20 | +<a href="https://github.com/aspose-barcode/Aspose.BarCode-for-Java/blob/master/src/test/java/com/aspose/barcode/guide/generation/visual_parameters/CustomizingCaptionsExample.java" target="_blank">View CustomizingCaptionsExample.java</a> |
| 21 | + |
| 22 | +You can also browse the <a href="https://github.com/aspose-barcode/Aspose.BarCode-for-Java/tree/master/src/test/java/com/aspose/barcode/guide/generation" target="_blank">Generation examples directory</a>. |
| 23 | + |
| 24 | +## Captions and human-readable code text |
| 25 | + |
| 26 | +Captions and human-readable code text serve different purposes. |
| 27 | + |
| 28 | +Human-readable code text represents the encoded barcode value and is configured through `CodetextParameters`. |
| 29 | + |
| 30 | +Captions are independent labels configured through `CaptionParameters`. |
| 31 | + |
| 32 | +To use a caption as the only visible text, hide the built-in human-readable code text: |
| 33 | + |
| 34 | +```java |
| 35 | +generator.getParameters() |
| 36 | + .getBarcode() |
| 37 | + .getCodeTextParameters() |
| 38 | + .setLocation(CodeLocation.NONE); |
| 39 | +``` |
| 40 | + |
| 41 | +Hiding the human-readable text does not change the encoded barcode payload. |
| 42 | + |
| 43 | +## Add a caption above a barcode |
| 44 | + |
| 45 | +The following example adds a centered title above a QR Code. |
| 46 | + |
| 47 | +```java |
| 48 | +BarcodeGenerator generator = new BarcodeGenerator( |
| 49 | + EncodeTypes.QR, |
| 50 | + "QR-TITLE" |
| 51 | +); |
| 52 | + |
| 53 | +generator.getParameters() |
| 54 | + .getBarcode() |
| 55 | + .getCodeTextParameters() |
| 56 | + .setLocation(CodeLocation.NONE); |
| 57 | + |
| 58 | +CaptionParameters captionAbove = |
| 59 | + generator.getParameters().getCaptionAbove(); |
| 60 | + |
| 61 | +captionAbove.setVisible(true); |
| 62 | +captionAbove.setText("SCAN ME"); |
| 63 | +captionAbove.getFont().getSize().setPoint(14); |
| 64 | +captionAbove.setAlignment(TextAlignment.CENTER); |
| 65 | +captionAbove.getPadding().getBottom().setPoint(8); |
| 66 | + |
| 67 | +generator.save( |
| 68 | + "qr_title_only.png", |
| 69 | + BarCodeImageFormat.PNG |
| 70 | +); |
| 71 | +``` |
| 72 | + |
| 73 | +`CaptionAbove.getPadding().getBottom()` controls the space between the upper caption and the barcode. |
| 74 | + |
| 75 | +## Add captions above and below |
| 76 | + |
| 77 | +Upper and lower captions can be displayed at the same time. |
| 78 | + |
| 79 | +```java |
| 80 | +BarcodeGenerator generator = new BarcodeGenerator( |
| 81 | + EncodeTypes.CODE_128, |
| 82 | + "ABOVE-BELOW" |
| 83 | +); |
| 84 | + |
| 85 | +generator.getParameters() |
| 86 | + .getBarcode() |
| 87 | + .getCodeTextParameters() |
| 88 | + .setLocation(CodeLocation.NONE); |
| 89 | + |
| 90 | +CaptionParameters captionAbove = |
| 91 | + generator.getParameters().getCaptionAbove(); |
| 92 | + |
| 93 | +captionAbove.setVisible(true); |
| 94 | +captionAbove.setText("INVENTORY LABEL"); |
| 95 | +captionAbove.getFont().getSize().setPoint(12); |
| 96 | +captionAbove.setAlignment(TextAlignment.CENTER); |
| 97 | +captionAbove.getPadding().getBottom().setPoint(6); |
| 98 | + |
| 99 | +CaptionParameters captionBelow = |
| 100 | + generator.getParameters().getCaptionBelow(); |
| 101 | + |
| 102 | +captionBelow.setVisible(true); |
| 103 | +captionBelow.setText("SKU-001-RED"); |
| 104 | +captionBelow.getFont().getSize().setPoint(10); |
| 105 | +captionBelow.setAlignment(TextAlignment.CENTER); |
| 106 | +captionBelow.getPadding().getTop().setPoint(8); |
| 107 | + |
| 108 | +generator.save( |
| 109 | + "code128_above_and_below.png", |
| 110 | + BarCodeImageFormat.PNG |
| 111 | +); |
| 112 | +``` |
| 113 | + |
| 114 | +The upper and lower captions have separate text, font, color, alignment, and padding settings. |
| 115 | + |
| 116 | +## Configure caption alignment |
| 117 | + |
| 118 | +Use `TextAlignment` to align caption text horizontally. |
| 119 | + |
| 120 | +```java |
| 121 | +CaptionParameters caption = |
| 122 | + generator.getParameters().getCaptionBelow(); |
| 123 | + |
| 124 | +caption.setVisible(true); |
| 125 | +caption.setText("WAREHOUSE A"); |
| 126 | +caption.setAlignment(TextAlignment.CENTER); |
| 127 | +``` |
| 128 | + |
| 129 | +Available alignment values include: |
| 130 | + |
| 131 | +```java |
| 132 | +TextAlignment.LEFT |
| 133 | +TextAlignment.CENTER |
| 134 | +TextAlignment.RIGHT |
| 135 | +``` |
| 136 | + |
| 137 | +The visual effect is easiest to see when the output image is wider than the caption text. |
| 138 | + |
| 139 | +## Configure caption font size |
| 140 | + |
| 141 | +Caption font size is configured through the caption font unit. |
| 142 | + |
| 143 | +```java |
| 144 | +CaptionParameters caption = |
| 145 | + generator.getParameters().getCaptionBelow(); |
| 146 | + |
| 147 | +caption.setVisible(true); |
| 148 | +caption.setText("CAPTION 14pt"); |
| 149 | +caption.getFont().getSize().setPoint(14); |
| 150 | +``` |
| 151 | + |
| 152 | +Point units are convenient when the output is intended for print because they represent typographic size rather than a fixed number of pixels. |
| 153 | + |
| 154 | +The requested font must be available in the runtime environment. Otherwise, Java can substitute another installed font. |
| 155 | + |
| 156 | +## Configure caption color |
| 157 | + |
| 158 | +Use `setTextColor` to set the caption text color. |
| 159 | + |
| 160 | +```java |
| 161 | +CaptionParameters caption = |
| 162 | + generator.getParameters().getCaptionBelow(); |
| 163 | + |
| 164 | +caption.setVisible(true); |
| 165 | +caption.setText("BRAND CAPTION"); |
| 166 | +caption.setTextColor(new Color(0, 128, 128)); |
| 167 | +``` |
| 168 | + |
| 169 | +The caption color can be matched to the barcode bar color for a consistent visual style. |
| 170 | + |
| 171 | +```java |
| 172 | +Color brandColor = new Color(30, 30, 30); |
| 173 | + |
| 174 | +generator.getParameters() |
| 175 | + .getBarcode() |
| 176 | + .setBarColor(brandColor); |
| 177 | + |
| 178 | +generator.getParameters() |
| 179 | + .getCaptionBelow() |
| 180 | + .setTextColor(brandColor); |
| 181 | +``` |
| 182 | + |
| 183 | +Maintain sufficient contrast between caption text and the image background. |
| 184 | + |
| 185 | +## Configure caption padding |
| 186 | + |
| 187 | +Each caption has independent padding on all sides. |
| 188 | + |
| 189 | +```java |
| 190 | +CaptionParameters caption = |
| 191 | + generator.getParameters().getCaptionBelow(); |
| 192 | + |
| 193 | +caption.getPadding().getTop().setPoint(6); |
| 194 | +caption.getPadding().getLeft().setMillimeters(2.0f); |
| 195 | +caption.getPadding().getRight().setPixels(10); |
| 196 | +``` |
| 197 | + |
| 198 | +Padding can be specified in different units: |
| 199 | + |
| 200 | +- pixels for exact raster spacing; |
| 201 | +- points for typographic layouts; |
| 202 | +- millimeters or inches for print-oriented layouts. |
| 203 | + |
| 204 | +For a caption above the barcode, bottom padding controls the gap toward the symbol. |
| 205 | + |
| 206 | +For a caption below the barcode, top padding controls the gap toward the symbol. |
| 207 | + |
| 208 | +## Use DPI-aware caption spacing |
| 209 | + |
| 210 | +A `Unit` value can be associated with a specific resolution before converting physical or typographic units to pixels. |
| 211 | + |
| 212 | +```java |
| 213 | +CaptionParameters captionAbove = |
| 214 | + generator.getParameters().getCaptionAbove(); |
| 215 | + |
| 216 | +Unit bottomPadding = |
| 217 | + captionAbove.getPadding().getBottom(); |
| 218 | + |
| 219 | +bottomPadding.updateResolution(203.0f); |
| 220 | +bottomPadding.setPoint(10.0f); |
| 221 | +``` |
| 222 | + |
| 223 | +This is useful for thermal-printer layouts where the same physical spacing must be converted for a known printer resolution. |
| 224 | + |
| 225 | +## Create multiline captions |
| 226 | + |
| 227 | +Captions can contain explicit line breaks. |
| 228 | + |
| 229 | +```java |
| 230 | +CaptionParameters caption = |
| 231 | + generator.getParameters().getCaptionBelow(); |
| 232 | + |
| 233 | +caption.setVisible(true); |
| 234 | +caption.setText("PICK FACE\nAISLE 12"); |
| 235 | +caption.setNoWrap(false); |
| 236 | +caption.getFont().getSize().setPoint(11); |
| 237 | +caption.setAlignment(TextAlignment.CENTER); |
| 238 | +``` |
| 239 | + |
| 240 | +`setNoWrap(false)` allows the caption text to wrap when the available image width is limited. |
| 241 | + |
| 242 | +Provide enough image height for all caption lines and the barcode symbol. |
| 243 | + |
| 244 | +## Combine a title with human-readable code text |
| 245 | + |
| 246 | +A caption can be used as a title while the barcode value remains visible below the symbol. |
| 247 | + |
| 248 | +```java |
| 249 | +BarcodeGenerator generator = new BarcodeGenerator( |
| 250 | + EncodeTypes.EAN_13, |
| 251 | + "5901234123457" |
| 252 | +); |
| 253 | + |
| 254 | +generator.getParameters() |
| 255 | + .getBarcode() |
| 256 | + .getCodeTextParameters() |
| 257 | + .setLocation(CodeLocation.BELOW); |
| 258 | + |
| 259 | +CaptionParameters captionAbove = |
| 260 | + generator.getParameters().getCaptionAbove(); |
| 261 | + |
| 262 | +captionAbove.setVisible(true); |
| 263 | +captionAbove.setText("PRODUCT LABEL"); |
| 264 | +captionAbove.getFont().getSize().setPoint(12); |
| 265 | +captionAbove.setAlignment(TextAlignment.CENTER); |
| 266 | +captionAbove.getPadding().getBottom().setPoint(6); |
| 267 | +``` |
| 268 | + |
| 269 | +The caption text does not replace or modify the EAN-13 payload. |
| 270 | + |
| 271 | +## Configure compact caption layouts |
| 272 | + |
| 273 | +For compact labels, use a small font and exact pixel spacing. |
| 274 | + |
| 275 | +```java |
| 276 | +CaptionParameters caption = |
| 277 | + generator.getParameters().getCaptionBelow(); |
| 278 | + |
| 279 | +caption.setVisible(true); |
| 280 | +caption.setText("LOT 24"); |
| 281 | +caption.getFont().getSize().setPoint(8); |
| 282 | +caption.getPadding().getTop().setPixels(4); |
| 283 | +caption.setAlignment(TextAlignment.CENTER); |
| 284 | +``` |
| 285 | + |
| 286 | +Test compact layouts at the final print resolution. A caption that looks readable on screen may become too small after printing. |
| 287 | + |
| 288 | +## Recommendations |
| 289 | + |
| 290 | +- Use captions for descriptive text that is not part of the encoded payload. |
| 291 | +- Hide human-readable code text only when the layout requires it. |
| 292 | +- Keep caption text visually separate from barcode bars and modules. |
| 293 | +- Provide enough image width and height for the selected font and padding. |
| 294 | +- Use point, millimeter, or inch units for print-oriented layouts. |
| 295 | +- Verify that the required fonts are installed in the runtime environment. |
| 296 | +- Test multiline and compact captions with the final renderer and printer. |
0 commit comments