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:
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:
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 Create Custom Barcodes with IBarcodeGenerator.