Manage Caption of the Barcode

Skip to end of metadata
Go to start of metadata
Sometimes, developers might be interested in displaying some extra information on the barcode image that is called Caption. Using Aspose.BarCode, developers can display captions on above, below or both positions of the barcode image.


Figure: All possible positions where captions can be placed

It is also possible to customize the text, alignment, font, color and space of the caption. Aspose.BarCode provides a Caption class to serve this purpose. All barcode generating classes have two properties, CaptionAbove and CaptionBelow that take an instance of Caption class. So, developers can create an object of Caption class and then assign that object to CaptionAbove, CaptionBelow or both properties to set the captions of the barcode.

[C#]
//Instantiate barcode object
BarCodeBuilder bb = new BarCodeBuilder();

//Set the Code text for the barcode
bb.CodeText = "1234567";

//Set the symbology type to Code128
bb.SymbologyType = Symbology.Code128;

//Create caption object. Set its text and text alignment & also make it visible
Caption caption = new Caption();
caption.Text = "Aspose.BarCode";
caption.TextAlign = System.Drawing.StringAlignment.Center;
caption.Visible = true;

//Assign caption object to be displayed above and below the barcode
bb.CaptionAbove = caption;
bb.CaptionBelow = caption;

//Save the image to your system and set its image format to Jpeg
bb.Save("barcode.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
 
[VB.NET]
'Instantiate barcode object
Dim bb As BarCodeBuilder = New BarCodeBuilder()

'Set the Code text for the barcode
bb.CodeText = "1234567"

'Set the symbology type to Code128
bb.SymbologyType = Symbology.Code128

'Create caption object. Set its text and text alignment & also make it visible
Dim caption As Caption = New Caption()
caption.Text = "Aspose.BarCode"
caption.TextAlign = System.Drawing.StringAlignment.Center
caption.Visible = True

'Assign caption object to be displayed above and below the barcode
bb.CaptionAbove = caption
bb.CaptionBelow = caption

'Save the image to your system and set its image format to Jpeg
bb.Save("barcode.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
 

The output generated barcode by the above code is shown below in the figure:

Figure: Resulting barcode image generated after example code execution


The output of Java code snippet:

Figure: Resulting barcode rendering to Graphics object


Continuing the sample above, the following sample shows Font and Color setting:

[C#]
bb.CaptionAbove.TextAlign = StringAlignment.Near;
bb.CaptionAbove.Text = "Aspose.Demo";
bb.CaptionAbove.Visible = true;
bb.CaptionAbove.Font = new System.Drawing.Font("Pristina", 14f);
bb.CaptionAbove.ForeColor = Color.OrangeRed;

bb.CaptionBelow.TextAlign = StringAlignment.Far;
bb.CaptionBelow.Text = "Aspose.Demo";
bb.CaptionBelow.Visible = true;
bb.CaptionBelow.Font = new System.Drawing.Font("Pristina", 14f);
bb.CaptionBelow.ForeColor = Color.OrangeRed;
 
[VB.NET]
bb.CaptionAbove.TextAlign = StringAlignment.Near
bb.CaptionAbove.Text = "Aspose.Demo"
bb.CaptionAbove.Visible = True
bb.CaptionAbove.Font = New System.Drawing.Font("Pristina", 14.0F)
bb.CaptionAbove.ForeColor = Color.OrangeRed

bb.CaptionBelow.TextAlign = StringAlignment.Far
bb.CaptionBelow.Text = "Aspose.Demo"
bb.CaptionBelow.Visible = True
bb.CaptionBelow.Font = New System.Drawing.Font("Pristina", 14.0F)
bb.CaptionBelow.ForeColor = Color.OrangeRed
 

Figure: customizing caption

For the functionality differences in Aspose.BarCode .Net and Aspose.BarCode Java, please check out EditionType
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.