Create a Square QR Code rather than a rectangle using Aspose.Barcode for .NET


I am trying to generate a QR code image for our Documents, But it always generate as a rectangle rather than a Square. I have tried setting different options, but with no luck. Can you give me a sample setting code for generating Square QR Code image rather than a rectangle.


Hi Rajesh,

Thanks for your inquiry. We will appreicate it if you please share your sample code here, we will look into it and will guide you accordingly.

Furthermore, please check following sample code snippet to generate and add QR code in PDF document.

Aspose.Pdf.Facades.PdfFileInfo pdfFileInfo = new Aspose.Pdf.Facades.PdfFileInfo("E:/Data/HelloWorld.pdf");

Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document("E:/Data/HelloWorld.pdf");

//Get the page where image needs to be added

Aspose.Pdf.Page page = pdfDocument.Pages[1];

Aspose.BarCode.BarCodeBuilder builder = new Aspose.BarCode.BarCodeBuilder();

builder.EncodeType = Aspose.BarCode.Generation.EncodeTypes.QR;

builder.QREncodeMode = Aspose.BarCode.QREncodeMode.Auto;

// Set Auto for Micro QR

builder.QREncodeType = Aspose.BarCode.QREncodeType.Auto;

// Set code text

builder.CodeText = "";

// Set Barcode CodeLocation

builder.CodeLocation = Aspose.BarCode.CodeLocation.None;

// Set error correction level

builder.QRErrorLevel = Aspose.BarCode.QRErrorLevel.LevelL;

// Get barcode image Bitmap

Bitmap QrBarcode = builder.GenerateBarCodeImage();

MemoryStream Stream = new MemoryStream();

// Save QR code

QrBarcode.Save(Stream, System.Drawing.Imaging.ImageFormat.Bmp);

// Add image to Images collection of
Page Resources


// Using GSave operator: this operator
saves current graphics state

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

//Set coordinates

int lowerLeftX = Convert.ToInt32(pdfFileInfo.GetPageWidth(1)

(50 * 2.83) -QrBarcode.Width);
int lowerLeftY = Convert.ToInt32(502.83);

int upperRightX = Convert.ToInt32(pdfFileInfo.GetPageWidth(1)

(50 * 2.83));
int upperRightY = Convert.ToInt32(502.83) + QrBarcode.Height;
// Create Rectangle and Matrix objects

Aspose.Pdf.Rectangle rectangle = new Aspose.Pdf.Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY);

Matrix matrix = new Matrix(new double[] {rectangle.URX - rectangle.LLX,0,0,rectangle.URY - rectangle.LLY,rectangle.LLX,rectangle.LLY });

// Using ConcatenateMatrix (concatenate matrix) operator: defines how image must be placed

page.Contents.Add(new Aspose.Pdf.Operator.ConcatenateMatrix(matrix));

XImage ximage = page.Resources.Images[page.Resources.Images.Count];

// Using Do operator: this operator
draws image

page.Contents.Add(new Aspose.Pdf.Operator.Do(ximage.Name));

// Using GRestore operator: this
operator restores graphics state

page.Contents.Add(new Aspose.Pdf.Operator.GRestore());

// Save updated document

Best Regards,