Font size for code text not working - C# .NET

According to Customize Barcode Text and Captions in C#|Documentation we can set the font size of the code text by doing something like:
gen.Parameters.Barcode.CodeTextParameters.Font.Size.Millimeters = 1000;

I’ve got this simple .NET project that generates a barcode:

BarcodeGenerator gen = new BarcodeGenerator(EncodeTypes.GS1Code128);

//size
gen.Parameters.AutoSizeMode = AutoSizeMode.Interpolation;
gen.Parameters.ImageHeight.Millimeters = 40;
gen.Parameters.ImageWidth.Millimeters = 80;

//setting the font size
gen.Parameters.Barcode.CodeTextParameters.Font.Size.Millimeters = 1000;

gen.Save("barcode.gif");

It doesn’t seem to matter if i set font.size to 1px or 1000 millimeters. The output result seems to keep the same font size for the code text.

What am i missing here?

@Pluriform_Software

It seems that you are experiencing issues with setting the font size for the code text in your barcode generation project. The code snippet you provided is correctly attempting to set the font size, but there are a couple of important points to consider.

  1. Font Mode: The font size setting may not take effect if the FontMode is set to Auto. When FontMode is set to Auto, the font size is calculated automatically based on the dimensions of the barcode, which means that any manual font size settings will be ignored. To manually set the font size, you should set the FontMode to Manual.

  2. Code Example: Here’s how you can modify your code to ensure that the font size is applied correctly:

BarcodeGenerator gen = new BarcodeGenerator(EncodeTypes.GS1Code128);

// Size
gen.Parameters.AutoSizeMode = AutoSizeMode.Interpolation;
gen.Parameters.ImageHeight.Millimeters = 40;
gen.Parameters.ImageWidth.Millimeters = 80;

// Set Font Mode to Manual
gen.Parameters.Barcode.CodeTextParameters.FontMode = FontMode.Manual;

// Setting the font size
gen.Parameters.Barcode.CodeTextParameters.Font.Size.Millimeters = 10; // Adjust as needed

gen.Save("barcode.gif");

In this example, I set the FontMode to Manual, which allows the font size you specify to take effect. Adjust the size in millimeters as needed for your specific requirements.

If you continue to experience issues, ensure that you are using the latest version of the Aspose.BarCode library, as updates may include fixes for related issues.

For further details on customizing barcode text and captions, you can refer to the official documentation here.

If you have any more questions or need further assistance, feel free to ask!

I see, thank you for your answer. I do have another question concerning this font size topic.

Previously we used the BarHeight property to set the sizes of the barcodes.
Now we are using AutoSizeMode.Interpolation. Since the FontMode is set to Automatic, we are now seeing that the font size of the code text differs a lot per barcode. The font size seems to be set inconsistently.

For example, in this image
Schermafbeelding 2024-10-07 164950.png (50.8 KB)
you can see a barcode with a width of 100mm and height of 40mm. The font size is big.

While in this image
Schermafbeelding 2024-10-07 165033.png (16.0 KB)
we have a different barcode with the same dimensions as the one mentioned above, yet the font size is tiny.

Can this perhaps have anything to do with the AutoSizeMode? And if not, any idea what else could be the cause?

@Pluriform_Software,

Thanks for the image files.

Please note, if FontMode is set to Auto, font size will be calculated automatically based on xDimension value, it will ignore font size you set in code. Default value: FontMode.Auto. Moreover, when you set FontMode to Manual, it will set font size you specify in code.

Aspose.BarCode.Generation.BarcodeGenerator gen = new Aspose.BarCode.Generation.BarcodeGenerator(Aspose.BarCode.Generation.EncodeTypes.Code128, "test");

// Size
gen.Parameters.AutoSizeMode = Aspose.BarCode.Generation.AutoSizeMode.Interpolation;
gen.Parameters.ImageHeight.Millimeters = 40;
gen.Parameters.ImageWidth.Millimeters = 100;

// Set Font Mode to Manual
gen.Parameters.Barcode.CodeTextParameters.FontMode = Aspose.BarCode.Generation.FontMode.Manual;

// Setting the font size
gen.Parameters.Barcode.CodeTextParameters.Font.Size.Millimeters = 20; // it will work if FontMode is manual. If FontMode is Auto, it will ignore font size you set in code.

gen.Save("e:\\test2\\barcode1.gif");

barcode1.gif (9.6 KB)

Could you please share your sample (runnable) code to demonstrate the issue. We will check your issue soon.