Migration of BarcodeBuilder to BarcodeGenerator field mapping

Hi,
We are doing migration from older to newer Aspose library and facing issues related to barcode settings as properties has been updated with units.
Please help to update the below code to newer BarcodeGenerator API :

                _barCodeBuilder.CodeLocation = CodeLocation.None;

                // DONOT CHANGE: Very important configuration 0.3F is the base reference
                _barCodeBuilder.Margins.Left = (8.3F * (float)leftMarginInInches) / 0.3F;
                // DONOT CHANGE: Very important configuration 0.3F is the base reference
                _barCodeBuilder.Margins.Right = (8.3F * (float)rightMarginInInches) / 0.3F;
                _barCodeBuilder.Margins.Top = 0F;
                _barCodeBuilder.Margins.Bottom = 0F;
                // DONOT CHANGE: Very important configuration 0.3F is the base reference
                _barCodeBuilder.BarHeight = (8.3F * (float)heightInInches) / 0.3F;
                _barCodeBuilder.xDimension = 0.345F;
                _barCodeBuilder.GraphicsUnit = GraphicsUnit.Inch;
                _barCodeBuilder.RotationAngleF = (float)rotationAngle;

Thank you

@mani.g,
Please give a try to the following sample code and share the feedback.

//BarCodeBuilder _barCodeBuilder = new BarCodeBuilder();
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.DataMatrix, "This is the data to be encoded");

//_barCodeBuilder.CodeLocation = CodeLocation.None;
generator.Parameters.Barcode.CodeTextParameters.Location = Aspose.BarCode.Generation.CodeLocation.None;

float leftMarginInInches = 0;
// DONOT CHANGE: Very important configuration 0.3F is the base reference
//_barCodeBuilder.Margins.Left = (8.3F * (float)leftMarginInInches) / 0.3F;
generator.Parameters.Barcode.Padding.Left.Inches = (8.3F * (float)leftMarginInInches) / 0.3F;


float rightMarginInInches = 0;
// DONOT CHANGE: Very important configuration 0.3F is the base reference
//_barCodeBuilder.Margins.Right = (8.3F * (float)rightMarginInInches) / 0.3F;
generator.Parameters.Barcode.Padding.Right.Inches = (8.3F * (float)rightMarginInInches) / 0.3F;

//_barCodeBuilder.Margins.Top = 0F;
generator.Parameters.Barcode.Padding.Top.Inches = 0F;

//_barCodeBuilder.Margins.Bottom = 0F;
generator.Parameters.Barcode.Padding.Bottom.Inches = 0F;

float heightInInches = 0;
// DONOT CHANGE: Very important configuration 0.3F is the base reference
//_barCodeBuilder.BarHeight = (8.3F * (float)heightInInches) / 0.3F;
generator.Parameters.Barcode.BarHeight.Inches = (8.3F * (float)heightInInches) / 0.3F;

//_barCodeBuilder.xDimension = 0.345F;
generator.Parameters.Barcode.XDimension.Inches = 0.345F;

//_barCodeBuilder.GraphicsUnit = GraphicsUnit.Inch;
//

float rotationAngle = 0;
//_barCodeBuilder.RotationAngleF = (float)rotationAngle;
generator.Parameters.RotationAngle = (float)rotationAngle;

@ahsaniqbalsidiqui
Thank you , while testing i found two issues

  1. i got error on saving barcode with above options :
    “Can’t render barcode: not enough space”
  2. another issue regarding text replace with barcode image. the new replace has new class FindReplaceOptions needs to be at the same i need to use Replace evaluator but new code has text for replacement also. I’m not clear why we need to give again replacement text when we are using Replace evaluator.
    OLD CODE :
    wordDoc.Range.Replace(new Regex(Regex.Escape(searchText), RegexOptions.IgnoreCase),
    new ReplaceEvaluatorFindAndInsert(imageStream, imageWidth, imageHeight, replaceContentType));
    NEW CODE:
    wordDoc.Range.Replace(new Regex(Regex.Escape(searchText), RegexOptions.IgnoreCase)," ", new FindReplaceOptions(
    new ReplaceEvaluatorFindAndInsert(imageStream, imageWidth, imageHeight, replaceContentType)));

@mani.g,

  1. I tested using the following sample code it works find and I got the output image:
    e.g
    Sample code:

    Aspose.BarCode.Generation.BarcodeGenerator generator = new Aspose.BarCode.Generation.BarcodeGenerator(Aspose.BarCode.Generation.EncodeTypes.DataMatrix, “This is the data to be encoded”);

                    generator.Parameters.Barcode.CodeTextParameters.Location = Aspose.BarCode.Generation.CodeLocation.None;

                    float leftMarginInInches = 0;
                    generator.Parameters.Barcode.Padding.Left.Inches = (8.3F * (float)leftMarginInInches) / 0.3F;


                    float rightMarginInInches = 0;
                    generator.Parameters.Barcode.Padding.Right.Inches = (8.3F * (float)rightMarginInInches) / 0.3F;

                    generator.Parameters.Barcode.Padding.Top.Inches = 0F;

                    generator.Parameters.Barcode.Padding.Bottom.Inches = 0F;

                    float heightInInches = 1;
                    generator.Parameters.Barcode.BarHeight.Inches = (8.3F * (float)heightInInches) / 0.3F;

                    generator.Parameters.Barcode.XDimension.Inches = 0.345F;

                    float rotationAngle = 0;
                    generator.Parameters.RotationAngle = (float)rotationAngle;


                    // Get barcode image Bitmap and Save QR code
                    Bitmap lBmp = generator.GenerateBarCodeImage();  

                    MemoryStream xStream = new MemoryStream();
                    lBmp.Save(xStream, System.Drawing.Imaging.ImageFormat.Png);

                    byte[] data = xStream.ToArray();
                    lBmp.Save("e:\\test2\\out1.png", System.Drawing.Imaging.ImageFormat.Png);

The output image is attached for your reference.
out1.png (714 Bytes)

I am using Aspose.BarCode for .NET v20.3. If you still find the issue, please provide your sample code (runnable) to reproduce the issue on our end, we will check it soon.

  1. Do you use Aspose.Words APIs to replace the text in MS Word document with barcode image (generated by Aspose.BarCode)? If true, please post your issue in Aspose.Words forum. If false, and you are using only Aspose.BarCode API and find the issue in it, please post a sample console application (runnable), zip the project and post us, we will check it soon.

Hi Sorry,
I updated variable values i’m using for calculating the dimensions of the image. Please check again by modifying the variable values.

                generator.Parameters.Barcode.CodeTextParameters.Location = Aspose.BarCode.Generation.CodeLocation.None;
                float leftMarginInInches = 0.25;
                generator.Parameters.Barcode.Padding.Left.Inches = (8.3F * (float)leftMarginInInches) / 0.3F;
                float rightMarginInInches = 0.25;
                generator.Parameters.Barcode.Padding.Right.Inches = (8.3F * (float)rightMarginInInches) / 0.3F;
                generator.Parameters.Barcode.Padding.Top.Inches = 0F;
                generator.Parameters.Barcode.Padding.Bottom.Inches = 0F;

                float heightInInches = 0.5;
                generator.Parameters.Barcode.BarHeight.Inches = (8.3F * (float)heightInInches) / 0.3F;
               generator.Parameters.Barcode.XDimension.Inches = 0.345F;

                float rotationAngle = 0;
                generator.Parameters.RotationAngle = (float)rotationAngle;

                // Get barcode image Bitmap and Save QR code
                Bitmap lBmp = generator.GenerateBarCodeImage();  

                MemoryStream xStream = new MemoryStream();
                lBmp.Save(xStream, System.Drawing.Imaging.ImageFormat.Png);

@mani.g,

I still could not find the issue with Aspose.BarCode for .NET v20.3. The output barcode image is ok. Here is my sample code (I added/updated your pasted code to it accordingly):
e.g
Sample code:

Aspose.BarCode.Generation.BarcodeGenerator generator = new Aspose.BarCode.Generation.BarcodeGenerator(Aspose.BarCode.Generation.EncodeTypes.DataMatrix, "This is the data to be encoded");

            generator.Parameters.Barcode.CodeTextParameters.Location = Aspose.BarCode.Generation.CodeLocation.None;
                float leftMarginInInches = 0.25f;
                generator.Parameters.Barcode.Padding.Left.Inches = (8.3F * (float)leftMarginInInches) / 0.3F;
                float rightMarginInInches = 0.25f;
                generator.Parameters.Barcode.Padding.Right.Inches = (8.3F * (float)rightMarginInInches) / 0.3F;
                generator.Parameters.Barcode.Padding.Top.Inches = 0F;
                generator.Parameters.Barcode.Padding.Bottom.Inches = 0F;

                float heightInInches = 0.5f;
                generator.Parameters.Barcode.BarHeight.Inches = (8.3F * (float)heightInInches) / 0.3F;
               generator.Parameters.Barcode.XDimension.Inches = 0.345F;

                float rotationAngle = 0;
                generator.Parameters.RotationAngle = (float)rotationAngle;

                // Get barcode image Bitmap and Save QR code
                Bitmap lBmp = generator.GenerateBarCodeImage();  

                MemoryStream xStream = new MemoryStream();
                lBmp.Save(xStream, System.Drawing.Imaging.ImageFormat.Png);

          
            byte[] data = xStream.ToArray();
            lBmp.Save("e:\\test2\\out1.png", System.Drawing.Imaging.ImageFormat.Png);

out1.png (1.3 KB)

In my code i’m using Snippet

_barCodeBuilder.Parameters.AutoSizeMode = AutoSizeMode.Nearest;

This is making the BarcodeGeneration Fail.
After commenting this it started workin.

@mani.g,
We were able to observe the issue but we need to look into it more. We have logged the issue in our database for investigation and for a fix. Once, we will have some news for you, we will update you in this topic.

This issue has been logged as

BARCODENET-37440 – Exception raised while setting the AutoSizeMode = Nearest

Also this code is working in local, but failing in hosting server(PCF(Pivotal cloud foundry)).
With error message :
image.png (11.3 KB)

i tried installing system.drawing.common nuget package again explicitly , still getting this error in the hosted server.
Please help us if you know solution for this. Thanks.

@mani.g,
Please install libgdiplus in your hosting server and let us know your feedback.

@mani.g,

Indeed it throws an exception if AutoSizeMode.Nearest is set. It happens because in AutoSize modes you need to set desired size of the output image:
generator.Parameters.AutoSizeMode = AutoSizeMode.Nearest;
generator.Parameters.ImageWidth.Inches = 21.5f;
generator.Parameters.ImageHeight.Inches = 7.6f;

And also these parameters are ignored in AutoSize modes:
generator.Parameters.Barcode.BarHeight.Inches = (8.3F * (float)heightInInches) / 0.3F;
generator.Parameters.Barcode.XDimension.Inches = 0.345F;

AutoSize modes should be used when you need to create barcode image of exact size. If that is the case you need to set ImageHeight and ImageWidth parameters. If those parameters are not set then default ones will be used. In you case default width and height are too small to generate your barcode with all paddings specified, so you get an exception “not enough space”.

If you want to specify XDimension and BarHeight then do use AutoSizeMode.None. Otherwise set ImageWidth and ImageHeight parameters.

Hi Team,
We are using PCF container to deploy .Net Core service.
We are unable to run commands as root in the container. so we are unable to install through normal means.
we added CoreCompact.libgdiplus nuget package and updated respective env paths.
Still we are getting same error : Unable to load DLL : libgdiplus.

Can you please help me resolve this issue while using aspose.barcode.
Thanks

@mani.g,
We have noted this information and will share our feedback soon.

Have you installed System.Drawing.Common?

Also this problem could be related with yours

In 20.4 version we updated .Net Standard versions to System.Drawing.Common 4.7.0 version.

Hi ,
we tried the above mentioned options but still not working.
We need help how to fix this in problem in PCF container.
Is it possible to have one on one call.
Thank you

Hi.

To split the task in the two different tasks, please create new topic with “PCF+libgdiplus on .Net Core” issue description, because this is different problem from upper.

Also, it will be good if you describe host system in new topic (there might be some version of Linux and we have problems with some versions of Linux).

PS
Have you tried to add some of the strings to your csproj, this could help.

< ItemGroup>
< PackageReference Include=“System.Drawing.Common” Version=“4.7.0” />
< PackageReference Include=“System.Text.Encoding.CodePages” Version=“4.7.0” />
< /ItemGroup>

projref.zip (230 Bytes)

Hi ,
Thank you very much.
i didn’t added the text encoding package , i’ll try that also.

Sure i’ll create new topic as you mentioned above for this issue.

Thanks again.