Insert image inside PDF using C# - Different size and place of the inserted image in different PDFs

Hello Aspose,
The task is to add the generated QR code to a specific location on the PDF page.
Here is the code:
using (var memoryStream = new MemoryStream())
Aspose.Pdf.Page page = pdfDocument.Pages[1];

  page.Contents.Add(new Aspose.Pdf.Operators.GSave());

  var qr = GetCoordinate(barcodeSetting, page.Rect.Width, page.Rect.Height);

  // qr.Size = 70pts
  // qr.LeftX = 467pts
  // qr.LeftY = 742pts
  Matrix matrix = new Matrix(new double[] { qr.Size, 0, 0, qr.Size, qr.LeftX, qr.LeftY });
  page.Contents.Add(new Aspose.Pdf.Operators.ConcatenateMatrix(matrix));
  XImage ximage = page.Resources.Images[page.Resources.Images.Count];
  page.Contents.Add(new Aspose.Pdf.Operators.Do(ximage.Name));
  page.Contents.Add(new Aspose.Pdf.Operators.GRestore());

Here are examples of files with the inserted image:
Ok.pdf (50.9 KB)
Wrong.pdf (177.6 KB)

As you can see from the examples - documents are obtained in different ways from the same source file. The file size is the same in both cases. I checked the margins in debugging, they are the same. Why is the QR located in different places on the page? How can this be fixed? Please help me to solve this problem.


The issue seems related to the specific structure of the PDF file i.e. Wrong.pdf. We were able to notice the issue in our environment. However, could you please try using page.GetPageRect(true) method instead of page.Rect and pass the values to your GetCoordinate() method. If issue still persists, please share the definition of this method along with sample barcode image. We will log an issue in our issue tracking system and share the ID with you.

Thanks for the answer!
I tried as you said, but the issue unfortunately persisted.
var qr = Functions.BarcodeSetting.GetCoordinate(barcodeSetting, page.GetPageRect(true).Width, page.GetPageRect(true).Height, isReverseYCoordinate);

Here is a QR generator class:

using QRCoder;
using System.Drawing;

namespace BarcodeW
  public class BarcodeGenerator
    public byte[] GenerateQRCode(string qrCodeText)
      QRCodeGenerator qrGenerator = new QRCodeGenerator();
      QRCodeData qRCodeData = qrGenerator.CreateQrCode(qrCodeText, QRCodeGenerator.ECCLevel.Q);
      QRCode qrCode = new QRCode(qRCodeData);
      Bitmap qrImage = qrCode.GetGraphic(20);
      ImageConverter converter = new ImageConverter();
      byte[] qrByte = (byte[])converter.ConvertTo(qrImage, typeof(byte[]));
      return qrByte;

Generation in insert to PDF method:

var barcodeGenerator = new BarcodeW.BarcodeGenerator();
var qrCode = barcodeGenerator.GenerateQRCode(qrValue);
MemoryStream barcode = new MemoryStream(qrCode);

Is there any alternative way to insert the generated QR to the PDF?


Thanks for sharing the requested information.

As requested earlier, would you please share the sample barcode image as well. This would help us investigating the issue accordingly.

In the zip attachment, QR image in different extensions.
5308.png (689 Bytes) (28.2 KB)


We have logged an issue as PDFNET-49281 in our issue tracking system for this case. We will further investigate the reasons behind this behavior of the API and let you know as soon as the logged ticket is resolved. Please be patient and spare us some time.

You can please try adding an image stamp inside PDF document as an alternative and let us know if you still face any issue.

We are sorry for the inconvenience.

Thanks a lot!
Method AddStamp works perfect.

1 Like


It is good to know that your issue has been resolved by suggested approach. Please continue using our API and we will surely inform you as soon as the logged issue is resolved.