FAQ TBarCode .NET
How Can I Add Additional Text to the Control?
How Can I Add Additional Text to the Control?
This is possible by changing the active text index programmatically. The following sample code assumes a Barcode Control, which is drawn on a Graphics (gfx) in Pixel units and shows how to add additional text on the right side.
Barcode tbc = barcodeControl1.Barcode;
Graphics gfx = this.CreateGraphics();
SizeF barcodeSizeMM = tbc.CalculateBarcodeSize(gfx);
double pixelSizeMM = 25.4F / gfx.DpiX;
int barcodeWidthPx = Convert.ToInt32(0.5 + barcodeSizeMM.Width / pixelSizeMM);
int barcodeHeightPx = Convert.ToInt32(0.5 + barcodeSizeMM.Height / pixelSizeMM);
// first add a quiet zone for our text area
tbc.QuietZones.Right.Size = 100;
tbc.QuietZones.Unit = QuietZoneUnit.Pixel;
// set barcode data (+ normal HRT) in text area #0
tbc.ActiveTextIndex = 0;
tbc.BarcodeType = BarcodeType.Code128;
tbc.Data = "1234567890";
tbc.IsTextVisible = false;
// change text index to (auxiliary) text area #1
tbc.ActiveTextIndex = 1;
// now adjust all settings for text area #1
tbc.HumanReadableText ="My Label Text";
tbc.Font = new Font("Arial", 10, FontStyle.Regular, GraphicsUnit.Point);
tbc.TextClipping = false;
tbc.IsTextVisible = true;
tbc.TextPositionTop = (barcodeHeightPx - (int) tbc.Font.GetHeight(gfx)) / 2 ;
tbc.TextPositionLeft = barcodeWidthPx - (int)tbc.QuietZones.Right.Size + 10;
// reset text index back to barcode text
tbc.ActiveTextIndex = 0;
The text area #1 can be set to any text, but also to your bar code data. You have to take care on the dimensions and need to center / align the text with your own coordinates.