Wrong Format for GS1 Code128

@ahsaniqbalsidiqui
I’m trying to create a USPS IMpb. I am receiving an error when trying to produce a GS1 Code128.

I don’t understand how my barcode value is wrong. Please help.

Here is a screenshot of the error.

2020-07-16_8-27-41.png (225.9 KB)

Here is the code:

Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click

    Dim bchr As String = "9214 8901 1538 5200 5203 11"
    Dim bArr() As Byte

    'Dim Generator = New Aspose.BarCode.Generation.BarcodeGenerator(EncodeTypes.GS1Code128, bc1 + bc2)
    Dim Generator = New Aspose.BarCode.Generation.BarcodeGenerator(EncodeTypes.GS1Code128, "(420)90221(92)0569013951581555550")

    Generator.Parameters.Barcode.XDimension.Inches = 0.007F
    Generator.Parameters.Barcode.BarCodeHeight.Inches = 0.35F
    Generator.Parameters.Barcode.Padding.Left.Inches = 0.17F
    Generator.Parameters.Barcode.Padding.Right.Inches = 0.17F
    Generator.Parameters.Barcode.Padding.Top.Inches = 0.05F
    Generator.Parameters.Barcode.Padding.Bottom.Inches = 0.15F
    Generator.CodeText = bchr
    Generator.Parameters.Barcode.CodeTextParameters.Alignment = Aspose.BarCode.Generation.TextAlignment.Center
    Generator.Parameters.CaptionAbove.Text = "My Barcode"
    Generator.Parameters.CaptionAbove.Padding.Top.Inches = 0.05F
    Generator.Parameters.CaptionAbove.Visible = True
    Generator.Parameters.CaptionAbove.Font.Size.Inches = 0.125F
    Generator.Parameters.CaptionAbove.Alignment = Aspose.BarCode.Generation.TextAlignment.Center
    Generator.Parameters.CaptionAbove.Font.Style = FontStyle.Bold

    Dim mStream = New System.IO.MemoryStream()

    Generator.Save(mStream, Aspose.BarCode.BarCodeImageFormat.Bmp)
    bArr = mStream.ToArray
    Dim ms = New MemoryStream(bArr)
    Dim ImageStamp = New ImageStamp(ms)
    ImageStamp.Background = True
    'ImageStamp.XIndent = 10
    'ImageStamp.YIndent = 100
    ''ImageStamp.Height = 55
    ''ImageStamp.Width = 350
    ImageStamp.Quality = 100
    '// Add stamp to particular page
    Dim pdfDocument = New Document()
    Dim Page = pdfDocument.Pages.Add()
    Page.PageInfo.Height = ImageStamp.Height
    Page.PageInfo.Width = ImageStamp.Width
    Page.PageInfo.Margin = New MarginInfo(0, 0, 0, 0)

    Page.AddStamp(ImageStamp)
    'dataDir = dataDir + "AddImageStamp_19.4.pdf"
    '// Save output document
    pdfDocument.Save("C:\Users\nbart\Downloads\test3.pdf")
End Sub

@nathanbartell,
It seems that you are not setting the text in the correct format. Please comment the following line of code and share the feedback.

Generator.CodeText = bchr

@ahsaniqbalsidiqui - Commenting out the line you suggested worked. But now I’m wondering why it is working now but not when I provide my own Human Readable value.

@nathanbartell,
We have tested your string with some other tools and observed that it works fine there but raises exception when used with Aspose.BarCode. This issue is logged in our database for an investigation and a fix. You will be notified here once any update is ready to share.

This issue is logged as:
BARCODENET-37568-Exception "Wrong format of GS1 Code128 input string" while creating barcode

Thank you, @ahsaniqbalsidiqui. Is there a way to not show the human-readable value?

For example, if the entire GS1 Code128 barcode string value is “(420)55350(92)14890194038316398145”, the string value I need to display under the barcode is: “9214 8901 9403 8316 3981 45”

Thank you.

@nathanbartell,
We have noted your query and will share our feedback soon.

At first GS1 barcodes and GS1 Code 128 version must be encoded in special format with Application Identifiers. To encode barcode in arbitrary format you should use Code128 barcode.

BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.Code128, “9214 8901 1538 5200 5203 11”);
generator.Save(@“d:\save\rec\gen.png”, BarCodeImageFormat.Png);

But USPS IMpb barcode format requires GS1 Code 128 format with human readable codetext. This does not mean that you must encode human readable codetext, you must encode data in GS1 Code 128 with caption replacing.

We do not have special USPS IMpb barcode version but it is easy and possible to implement with our engine with GS1 Code 128. However we can add this in future versions to ComplexBarcode encoder. To create USPS IMpb you need to:

  1. encode data in GS1 Code 128 format
  2. hide GS1 Code 128 generated codetext
  3. replace barcode caption.

//set GS1 Code 128 USPS IMpb value
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.GS1Code128, “(420)90221(92)0569013951581555550”);

//hide Codetext Value
generator.Parameters.Barcode.CodeTextParameters.Location = CodeLocation.None;

//add caption
generator.Parameters.CaptionBelow.Visible = true;
generator.Parameters.CaptionBelow.Alignment = TextAlignment.Center;
generator.Parameters.CaptionBelow.Font.Size.Pixels = 18;
generator.Parameters.CaptionBelow.Text = “9214 8901 1538 5200 5203 11”;
generator.Save(@“d:\save\rec\gen.png”, BarCodeImageFormat.Png);

About Application Identifiers(AI) you can read by Wikipedia link, placed above.

@alexander.gavriluk - This was helpful - thanks!

Another question about the captions above and below the barcode. They are not very clean. Is there a way to make the captions as clean and crisp as the barcode?

Here is the Aspose barcode on an Aspose PDF:
test3.pdf (15.7 KB)

Here is my code:
Private Sub ButtonSupport_Click(sender As Object, e As EventArgs) Handles Button7.Click

    Dim bArr() As Byte
    Dim License As Aspose.BarCode.License = New Aspose.BarCode.License()
    License.SetLicense("C:\Users\nbart\Documents\SourceCode\psg-docutransfer\ConnectSuite\Aspose\Aspose.Library\bin\Aspose.Barcode.lic")
    Dim Generator = New Aspose.BarCode.Generation.BarcodeGenerator(EncodeTypes.GS1Code128, "(420)55350(92)14890194038316398145")

    Aspose.Library.PdfHelper.LoadLic()

    Generator.Parameters.Barcode.BarHeight.Point = 39
    Generator.Parameters.Barcode.AutoSizeMode = Generation.AutoSizeMode.None
    Generator.Parameters.Barcode.XDimension.Point = 1
    Generator.Parameters.Barcode.CodeTextParameters.Location = CodeLocation.None
    Generator.Parameters.Barcode.CodeTextParameters.Alignment = Aspose.BarCode.Generation.TextAlignment.Center
    Generator.Parameters.CaptionBelow.Visible = True
    Generator.Parameters.Barcode.Padding.Bottom.Point = 6
    Generator.Parameters.Barcode.Padding.Top.Point = 6

    Generator.Parameters.CaptionBelow.Text = "9214 8901 1538 5200 5203 11"
    Generator.Parameters.CaptionBelow.Font.FamilyName = Font.Bold

    Generator.Parameters.CaptionAbove.Text = "USPS CERTIFIED MAIL"
    Generator.Parameters.CaptionAbove.Visible = True


    Dim lGeneratedBitmap = Generator.GenerateBarCodeImage()
    Dim imageWidth = lGeneratedBitmap.Width
    Dim imageLength = lGeneratedBitmap.Height
    Dim mStream = New System.IO.MemoryStream()

    Generator.Save(mStream, Aspose.BarCode.BarCodeImageFormat.Bmp)

    Dim ImageStamp = New ImageStamp(mStream)
    ImageStamp.Background = True
    ImageStamp.XIndent = 18
    ImageStamp.YIndent = 0
    ImageStamp.Quality = 100

    Dim pdfDocument = New Document()

    Dim Page = pdfDocument.Pages.Add()
    pdfDocument.Pages(1).SetPageSize(imageWidth + 36, 108)
    Page.PageInfo.Margin = New MarginInfo(0, 0, 0, 0)
    Page.AddStamp(ImageStamp)

    Dim graph = New Aspose.Pdf.Drawing.Graph(Page.PageInfo.Width, Page.PageInfo.Height)

    Dim line = New Aspose.Pdf.Drawing.Line(New Single() {1, 106, (imageWidth - 1) + 36, 106})
    line.GraphInfo.LineWidth = 2
    graph.Shapes.Add(line)

    Dim line2 = New Aspose.Pdf.Drawing.Line(New Single() {1, 2, (imageWidth - 1) + 36, 2})
    line2.GraphInfo.LineWidth = 2
    graph.Shapes.Add(line2)
    Page.Paragraphs.Add(graph)

    pdfDocument.Save("C:\Users\nbart\Downloads\test3.pdf")

End Sub

@nathanbartell,
We have reproduced this issue here and logged in our database for an investigation and fix. We will notify you once any update is ready to share.

This issue is logged as:
BARCODENET-37582 - Sharpness of captions below and above the barcode

You can setup font sizes for captions manually.

//set GS1 Code 128 USPS IMpb value
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.GS1Code128, “(420)90221(92)0569013951581555550”);

//hide Codetext Value
generator.Parameters.Barcode.CodeTextParameters.Location = CodeLocation.None;

//add below caption
generator.Parameters.CaptionBelow.Visible = true;
generator.Parameters.CaptionBelow.Text = “9214 8901 1538 5200 5203 11”;
//set font size
generator.Parameters.CaptionBelow.Font.Size.Pixels = generator.Parameters.Barcode.CodeTextParameters.Font.Size.Pixels;

//add above caption
generator.Parameters.CaptionAbove.Visible = true;
generator.Parameters.CaptionAbove.Text = “USPS CERTIFIED MAIL”;
//set font size
generator.Parameters.CaptionAbove.Font.Size.Pixels = generator.Parameters.Barcode.CodeTextParameters.Font.Size.Pixels;

generator.Save(@“d:\save\rec\gen.png”, BarCodeImageFormat.Png);

Or you want addition some of the auto mode like

CodeTextParameters.FontMode = FontMode.Auto;