Generate a Custom BarCode Image
A barcode is a visual representation of data in the form of parallel lines or patterns. Barcodes are widely used in various industries such as retail, logistics, healthcare, banking, and many others.
Microsoft Word allows users to embed barcodes directly into documents using fields. Users can insert a specific type of barcode, such as a QR code or a linear barcode, using the BARCODE field.
In this article, we will look at how the BARCODE field is implemented in Aspose.Words and how Aspose.Words allows users to work with Word documents to which a barcode has already been added.
Barcode Types Supported by Aspose.Words
Aspose.Words supports various types of barcodes. The barcode type is passed as a string value in the BarcodeType property.
When saving to Word formats that support barcodes, you can use any type of barcode that is supported by Microsoft Word. If an incorrect type of barcode was passed, Word will display an error.
When saving to other formats, such as PDF, Aspose.Words delegates barcode rendering to the user code, so the user is limited to the barcode types of their implementation or library used.
Insert a Barcode into a Document or Load a Document with an Added Barcode
Aspose.Words provides the ability to:
- Programmatically insert a barcode into a document using the DisplayBarcode and MergeBarcode field codes
- Or load a Word document with barcodes already inserted into it for further work
Aspose.Words has an interface for generating custom barcodes that makes it easy to use Aspose.Words and Aspose.BarCode together to render barcode images in output documents. For example, you can create a DOC, OOXML, or RTF document and add DISPLAYBARCODE field to it using Aspose.Words. Or you can load a DOC, OOXML or RTF document with DISPLAYBARCODE field already existing in it and provide your implementation of custom barcode generator.
A typical DISPLAYBARCODE field has the following syntax:
{ DISPLAYBARCODE "SomeData" QR \h 720 }
Below is an example code generator using the Aspose.Words and Aspose.BarCode APIs. This example shows how to insert barcode images at DISPLAYBARCODE field position in a Word document:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
Document doc = new Document(getMyDir() + "Field sample - BARCODE.docx"); | |
doc.getFieldOptions().setBarcodeGenerator(new CustomBarcodeGenerator()); | |
doc.save(getArtifactsDir() + "WorkingWithBarcodeGenerator.GenerateACustomBarCodeImage.pdf"); |
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
public class CustomBarcodeGenerator extends DocsExamplesBase implements IBarcodeGenerator | |
{ | |
/// <summary> | |
/// Converts barcode image height from Word units to Aspose.BarCode units. | |
/// </summary> | |
/// <param name="heightInTwipsString"></param> | |
/// <returns></returns> | |
private static float convertSymbolHeight(String heightInTwipsString) throws Exception { | |
// Input value is in 1/1440 inches (twips). | |
int heightInTwips = tryParseInt(heightInTwipsString); | |
if (heightInTwips == Integer.MIN_VALUE) | |
throw new Exception("Error! Incorrect height - " + heightInTwipsString + "."); | |
// Convert to mm. | |
return (float) (heightInTwips * 25.4 / 1440.0); | |
} | |
/// <summary> | |
/// Converts barcode image color from Word to Aspose.BarCode. | |
/// </summary> | |
/// <param name="inputColor"></param> | |
/// <returns></returns> | |
private static Color convertColor(String inputColor) throws Exception { | |
// Input should be from "0x000000" to "0xFFFFFF". | |
int color = tryParseHex(inputColor.replace("0x", "")); | |
if (color == Integer.MIN_VALUE) | |
throw new Exception("Error! Incorrect color - " + inputColor + "."); | |
return new Color((color >> 16), ((color & 0xFF00) >> 8), (color & 0xFF)); | |
// Backward conversion - | |
// return string.Format("0x{0,6:X6}", mControl.ForeColor.ToArgb() & 0xFFFFFF); | |
} | |
/// <summary> | |
/// Converts bar code scaling factor from percent to float. | |
/// </summary> | |
/// <param name="scalingFactor"></param> | |
/// <returns></returns> | |
private static float convertScalingFactor(String scalingFactor) throws Exception { | |
boolean isParsed = false; | |
int percent = tryParseInt(scalingFactor); | |
if (percent != Integer.MIN_VALUE && percent >= 10 && percent <= 10000) | |
isParsed = true; | |
if (!isParsed) | |
throw new Exception("Error! Incorrect scaling factor - " + scalingFactor + "."); | |
return percent / 100.0f; | |
} | |
/// <summary> | |
/// Implementation of the GetBarCodeImage() method for IBarCodeGenerator interface. | |
/// </summary> | |
/// <param name="parameters"></param> | |
/// <returns></returns> | |
public BufferedImage getBarcodeImage(BarcodeParameters parameters) throws Exception { | |
if (parameters.getBarcodeType() == null || parameters.getBarcodeValue() == null) | |
return null; | |
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.QR); | |
String type = parameters.getBarcodeType().toUpperCase(); | |
switch (type) | |
{ | |
case "QR": | |
generator = new BarcodeGenerator(EncodeTypes.QR); | |
break; | |
case "CODE128": | |
generator = new BarcodeGenerator(EncodeTypes.CODE_128); | |
break; | |
case "CODE39": | |
generator = new BarcodeGenerator(EncodeTypes.CODE_39_STANDARD); | |
break; | |
case "EAN8": | |
generator = new BarcodeGenerator(EncodeTypes.EAN_8); | |
break; | |
case "EAN13": | |
generator = new BarcodeGenerator(EncodeTypes.EAN_13); | |
break; | |
case "UPCA": | |
generator = new BarcodeGenerator(EncodeTypes.UPCA); | |
break; | |
case "UPCE": | |
generator = new BarcodeGenerator(EncodeTypes.UPCE); | |
break; | |
case "ITF14": | |
generator = new BarcodeGenerator(EncodeTypes.ITF_14); | |
break; | |
case "CASE": | |
generator = new BarcodeGenerator(EncodeTypes.NONE); | |
break; | |
} | |
if (generator.getBarcodeType().equals(EncodeTypes.NONE)) | |
return null; | |
generator.setCodeText(parameters.getBarcodeValue()); | |
if (generator.getBarcodeType().equals(EncodeTypes.QR)) | |
generator.getParameters().getBarcode().getCodeTextParameters().setTwoDDisplayText(parameters.getBarcodeValue()); | |
if (parameters.getForegroundColor() != null) | |
generator.getParameters().getBarcode().setBarColor(convertColor(parameters.getForegroundColor())); | |
if (parameters.getBackgroundColor() != null) | |
generator.getParameters().setBackColor(convertColor(parameters.getBackgroundColor())); | |
if (parameters.getSymbolHeight() != null) | |
{ | |
generator.getParameters().getImageHeight().setPixels(convertSymbolHeight(parameters.getSymbolHeight())); | |
generator.getParameters().setAutoSizeMode(AutoSizeMode.NONE); | |
} | |
generator.getParameters().getBarcode().getCodeTextParameters().setLocation(CodeLocation.NONE); | |
if (parameters.getDisplayText()) | |
generator.getParameters().getBarcode().getCodeTextParameters().setLocation(CodeLocation.BELOW); | |
generator.getParameters().getCaptionAbove().setText(""); | |
// Empiric scaling factor for converting Word barcode to Aspose.BarCode. | |
final float SCALE = 2.4f; | |
float xdim = 1.0f; | |
if (generator.getBarcodeType().equals(EncodeTypes.QR)) | |
{ | |
generator.getParameters().setAutoSizeMode(AutoSizeMode.NEAREST); | |
generator.getParameters().getImageWidth().setInches(generator.getParameters().getImageWidth().getInches() * SCALE); | |
generator.getParameters().getImageHeight().setInches(generator.getParameters().getImageWidth().getInches()); | |
xdim = generator.getParameters().getImageHeight().getInches() / 25; | |
generator.getParameters().getBarcode().getXDimension().setInches(xdim); | |
generator.getParameters().getBarcode().getBarHeight().setInches(xdim); | |
} | |
if (parameters.getScalingFactor() != null) | |
{ | |
float scalingFactor = convertScalingFactor(parameters.getScalingFactor()); | |
generator.getParameters().getImageHeight().setInches(generator.getParameters().getImageHeight().getInches() * scalingFactor); | |
if (generator.getBarcodeType().equals(EncodeTypes.QR)) | |
{ | |
generator.getParameters().getImageWidth().setInches(generator.getParameters().getImageHeight().getInches()); | |
generator.getParameters().getBarcode().getXDimension().setInches(xdim * scalingFactor); | |
generator.getParameters().getBarcode().getBarHeight().setInches(xdim * scalingFactor); | |
} | |
generator.getParameters().setAutoSizeMode(AutoSizeMode.NONE); | |
} | |
return generator.generateBarCodeImage(); | |
} | |
/// <summary> | |
/// Implementation of the GetOldBarcodeImage() method for IBarCodeGenerator interface. | |
/// </summary> | |
/// <param name="parameters"></param> | |
/// <returns></returns> | |
public BufferedImage getOldBarcodeImage(BarcodeParameters parameters) | |
{ | |
if (parameters.getPostalAddress() == null) | |
return null; | |
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.POSTNET); | |
{ | |
generator.setCodeText(parameters.getPostalAddress()); | |
} | |
return generator.generateBarCodeImage(); | |
} | |
/// <summary> | |
/// Parses an integer using the invariant culture. Returns Int.MinValue if cannot parse. | |
/// | |
/// Allows leading sign. | |
/// Allows leading and trailing spaces. | |
/// </summary> | |
public static int tryParseInt(String s) { | |
try { | |
double value = Double.parseDouble(s); | |
return castDoubleToInt(value); | |
} catch (NumberFormatException e) { | |
return Integer.MIN_VALUE; | |
} | |
} | |
/// <summary> | |
/// Casts a double to int32 in a way that uint32 are "correctly" casted too (they become negative numbers). | |
/// </summary> | |
public static int castDoubleToInt(double value) | |
{ | |
long temp = (long) value; | |
return (int) temp; | |
} | |
/// <summary> | |
/// Try parses a hex String into an integer value. | |
/// on error return int.MinValue | |
/// </summary> | |
public static int tryParseHex(String s) | |
{ | |
try { | |
return Integer.parseInt(s); | |
} catch (NumberFormatException e) { | |
return Integer.MIN_VALUE; | |
} | |
} | |
} |
You can also save the document with the loaded or newly inserted barcode in fixed page formats such as PDF, XPS, etc. The following code example shows how to save a Word document to PDF format:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
Document doc = new Document(getMyDir() + "Document.docx"); | |
doc.save(getArtifactsDir() + "BaseConversions.DocxToPdf.pdf"); |
Specify Barcode Options
When working with barcodes, you can set some additional properties. Aspose.Words provides you with the BarcodeParameters class – class for barcode parameters to pass-through to BarcodeGenerator.
Aspose.Words supports embedded 96 ppi resolution for images generated with IBarcodeGenerator, which limits the minimum size of a barcode image. To address this, developers can manually insert barcode images with the target resolution into a Word document and save them in the required format. For more details and examples on working with barcodes, see the article Read Barcodes from Word Documents.