Generate Barcode and add it to PDF - Height and Width are not being honored set by Aspose.Barcode

I’m also having some issues. I’m creating a barcode with Aspose.Barcode and then adding the barcode to a PDF using Aspose.PDF. I want Aspose.Barcode to control the size of the barcode (because I’m trying to achieve a USPS spec). When I add the barcode to the PDF with ImageStamp, it doesn’t maintain it’s size set in Aspose.Barcode.

Also, the text around the barcode is pixelated.

I tried uploading a .zip of the source and says it’s too large. Here’s the code I’m using.

Imports Aspose.BarCode.Generation
Imports Aspose.Pdf
Imports Aspose.Pdf.Facades
Imports System.IO

Module Module1

    Sub Main()
        Dim pdfDocument = New Document()
        Dim Page = pdfDocument.Pages.Add()
        Dim bc1 As String = "42021224"
        Dim bc2 As String = "9214890219456900520311"
        Dim bchr As String = "9214 8901 1538 5200 5203 11"
        Dim bArr() As Byte

        Dim Generator = New Aspose.BarCode.Generation.BarcodeGenerator(EncodeTypes.Code128, "(01)" + bc1 + "(01)" + bc2)

        Generator.Parameters.Barcode.XDimension.Inches = 0.017F
        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.CourierBold

        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
        Page.AddStamp(ImageStamp)
        'dataDir = dataDir + "AddImageStamp_19.4.pdf"
        '// Save output document
        pdfDocument.Save("C:\Users\nbart\Downloads\test3.pdf")
    End Sub

End Module

.test3.pdf (90.4 KB)

@nathanbartell

We have tested the scenario in our environment using Aspose.Barcode for .NET 20.4 and Aspose.PDF for .NET 20.6. The barcode image generated by Aspose.Barcode was with the dimensions of 657 x186 whereas, the dimensions of PDF Page were 8.26 x 11.69 (A4 Size by default). You may please try to either set dimensions of barcode according to PDF Page or change PDF Page height/width according to the barcode.

generator.Save(dataDir + "barcode.bmp", Aspose.BarCode.BarCodeImageFormat.Bmp);

System.Drawing.Image img = new Bitmap(dataDir + "barcode.bmp");
page.PageInfo.Height = img.Height;
page.PageInfo.Width = img.Width;
page.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);
Image imageStamp = new Image() { File = dataDir + "barcode.bmp" };
page.Paragraphs.Add(imageStamp);
pdfDocument.Save(dataDir + "test3.pdf");

test3.pdf (43.4 KB)

@asad.ali

It looks like your setting the PDF height and width to be the same size as the barcode image. Is that accurate? I need to put additional information on the PDF and need the PDF to be 8.5" by 11". This is for the USPS IMpb and I will be adding a mailing address to the PDF also.

@nathanbartell

Yes.

Would you kindly share an expected PDF document that you want to achieve as output. We will compare it with the one that is currently being generated and try to create a similar one.

@asad.ali

Here is the expected PDF document. Let me know if there is anything else I can provide. I will gladly do so.

Banner_9214890194038316428866.pdf (5.2 KB)

@nathanbartell

There is a different barcode image in your expected PDF document. Would you kindly use the following image in a sample PDF document and place it in your desired size and at the desired position for our reference and share it with us.

barcode.zip (11.1 KB)

We were able to notice the image size issue in our PDF while adding image through ImageStamp. However, we further want to compare the results with desired one and proceed accordingly.

@asad.ali - In the example that I shared with you, the barcode and the PDF are both created by iTextSharp. Would you like me to put the barcode you provided on an Aspose PDF document or on an iTextSharp PDF document?

@nathanbartell

Please use the shared barcode image in iTextSharp so that we can observe the results which you want to achieve using Aspose.PDF. Also, please share the sample values (of positioning and dimensions) which you are using in iTextSharp for barcode placement.

@asad.ali

When I try to add the Aspose Barcode to a iTextSharp PDF, this is what I get.

Banner_9214890167483500053458.pdf (11.7 KB)

To be clear, I’ve purchased Aspose.PDF and plan on using that moving forward (and not using iTextSharp for pdf or barcode generation).

I’d also like to purchase Aspose.Barcode if I can get this working so I can replace iTextSharp in my code.

Thank you,

@nathanbartell

The output generated by iTextSharp is pretty similar to the output generated by Aspose.PDF. Furthermore, we also noticed that barcode image generated by the Aspose.Barcode, had a larger width than the width of PDF Page. Nevertheless, we are further investigating the scenario and will get back to you shortly.

@nathanbartell,
You may give a try to the following sample code and share the feedback. Please note that in your case if you want to use AutoSizeMode.None you should use:

generator.Parameters.Barcode.XDimension.Pixels = 1;
generator.Parameters.Barcode.BarHeight.Pixels = 60;

Please note that parameter generator.Parameters.ImageHeight (and obsolete parameter generator.Parameters.Barcode.BarCodeHeight as well) is ignored in AutoSizeMode.None mode.

Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document();
Page page = pdfDocument.Pages.Add();

string bchr = "9214 8901 9403 8316 4288 66";

var generator = new Aspose.BarCode.Generation.BarcodeGenerator(EncodeTypes.Code128);
generator.Parameters.Barcode.XDimension.Pixels = 1;
generator.Parameters.Barcode.BarHeight.Pixels = 60;
generator.CodeText = bchr;

generator.Save("barcode.bmp", Aspose.BarCode.BarCodeImageFormat.Bmp);
System.Drawing.Image img = new Bitmap("barcode.bmp");
page.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);
ImageStamp imageStamp = new ImageStamp("barcode.bmp");
imageStamp.Background = false;
imageStamp.XIndent = 10;
imageStamp.YIndent = 100;
imageStamp.Quality = 100;
page.AddStamp(imageStamp);
pdfDocument.Save("test3.pdf");