Distorted Barcode Generation in PDF with Aspose.Pdf.Drawing and Aspose.BarCode v25.2.0

Hello Aspose Team,

We are encountering an issue in our ASP.NET Core application using Aspose.Pdf.Drawing and Aspose.BarCode (latest version 25.2.0) to generate PDFs containing both linear and DataMatrix barcodes. With version 25.2.0, the barcodes in the resulting PDF appear distorted. However, reverting to version 25.1.0 resolves the problem.

Interestingly, using the free (unlicensed) version of 25.2.0 generates correct-looking barcodes (except for the expected watermark). But once we apply our license, the barcodes in the PDF become visually corrupted—even though the individual barcode images themselves look fine before being inserted.

It appears that the issue occurs when integrating the barcodes into the PDF. Below is an outline of our PDF creation process:

  1. We have an HTML template that defines the layout, containing placeholders for text and images.
  2. We load the template as a string and replace specific labels with our data.
  3. We generate a barcode using Aspose.BarCode, convert it to a Base64-encoded string, and embed it in the HTML.
  4. Finally, we convert the HTML to PDF using Aspose with HtmlLoadOptions.

Questions:

  1. Are you aware of any changes in version 25.2.0 that could cause this behavior?
  2. Do you have any recommendations for resolving this issue while using the latest version?
  3. Are there any specific settings or approaches we should consider when adding barcodes to PDFs using your libraries?

We would greatly appreciate any assistance in resolving this problem. Please let us know if you need any additional information or if you’d like us to provide sample code or output for further investigation.

I came here to report the same issue. v25.2.0 and V25.3.0 Enterprise Licensed Aspose.PDF results in garbled barcode images when I use ImageStamp. This has worked fine for almost 10 years on every version up until now. 25.1.0 is the last version it worked properly in.

1 Like

@sidharth.gupta @jiveabillion

If possible, could you please share a sample barcode image along with the sample code snippet that you are using to insert it into PDF document? We will test the scenario in our environment and address it accordingly.

Here is a dummy code of what we are doing.
Note… Our app is a .NET 9 API, containerize using Docker, and deployed onto a Kubernetes cluster running in a Linux environment.

using System.Text;
using Aspose.Pdf;
using Aspose.Pdf.Optimization;

namespace HelloWorld
{
public class Program
{
public static void Main(string[] args)
{
byte[] pngBytes = File.ReadAllBytes(“path_to_image”);
Document pdfDoc = PdfGenerator.CreatePdfFromPngByteArray(pngBytes);
pdfDoc.Save(“Output.pdf”);
}
}
public static class PdfGenerator
{

    public static Document CreatePdfFromPngByteArray(byte[] imageBytes)
    {
        string base64Image = Convert.ToBase64String(imageBytes);
        string htmlContent = $@"
              <html>
                  <body>
                      <div>
                          <img src='data:image/png;base64,{base64Image}' alt='Embedded PNG'/>
                      </div>
                  </body>
              </html>";
        return ConvertHtmlToPdf(htmlContent);
    }


    private static Document ConvertHtmlToPdf(string htmlContent)
    {
        HtmlLoadOptions htmlLoadOptions = new()
        {
            PageInfo =
        {
            Margin = new MarginInfo(0, 50, -140, 80)
        }
        };

        using MemoryStream stream = new(Encoding.UTF8.GetBytes(htmlContent));
        stream.Seek(0, SeekOrigin.Begin);
        Document pdfDocument = new(stream, htmlLoadOptions);
        OptimizationOptions optimizeOptions = new()
        {
            RemoveUnusedObjects = true,
            RemoveUnusedStreams = true,
            CompressObjects = true,
            LinkDuplicateStreams = true,
            ImageCompressionOptions = { CompressImages = true }
        };
        pdfDocument.OptimizeResources(optimizeOptions);

        return pdfDocument;
    }
}

}

TestBarcode.png (111 Bytes)

2025-03-24-04-43-43-Label.pdf (163.0 KB)