We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

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

Team,


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.

Thanks!
Rajesh

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”);<o:p></o:p>

Aspose.Pdf.Document
pdfDocument = new Aspose.Pdf.Document(“E:/Data/HelloWorld.pdf”);<o:p></o:p>

//Get the page where image needs to be
added
<o:p></o:p>

Aspose.Pdf.Page
page = pdfDocument.Pages[1];<o:p></o:p>

Aspose.BarCode.BarCodeBuilder
builder = new Aspose.BarCode.BarCodeBuilder();<o:p></o:p>

builder.EncodeType = Aspose.BarCode.Generation.EncodeTypes.QR;<o:p></o:p>

builder.QREncodeMode = Aspose.BarCode.QREncodeMode.Auto;<o:p></o:p>

// Set Auto for Micro QR<o:p></o:p>

builder.QREncodeType = Aspose.BarCode.QREncodeType.Auto;<o:p></o:p>

// Set code text<o:p></o:p>

builder.CodeText = https://www.aspose.com/;<o:p></o:p>

// Set Barcode CodeLocation<o:p></o:p>

builder.CodeLocation = Aspose.BarCode.CodeLocation.None;<o:p></o:p>

// Set error correction level<o:p></o:p>

builder.QRErrorLevel = Aspose.BarCode.QRErrorLevel.LevelL;<o:p></o:p>

// Get barcode image Bitmap<o:p></o:p>

Bitmap QrBarcode = builder.GenerateBarCodeImage();<o:p></o:p>

MemoryStream Stream = new
MemoryStream();<o:p></o:p>

// Save QR code<o:p></o:p>

QrBarcode.Save(Stream, System.Drawing.Imaging.ImageFormat.Bmp);<o:p></o:p>

<o:p></o:p>

// Add image to Images collection of
Page Resources
<o:p></o:p>

page.Resources.Images.Add(Stream);<o:p></o:p>

// Using GSave operator: this operator
saves current graphics state
<o:p></o:p>

page.Contents.Add(new
Aspose.Pdf.Operator.GSave());<o:p></o:p>

//Set coordinates<o:p></o:p>

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

  • (50 * 2.83) -QrBarcode.Width);<o:p></o:p>

    int lowerLeftY = Convert.ToInt32(50

  • 2.83);<o:p></o:p>

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

  • (50 * 2.83));<o:p></o:p>

    int upperRightY = Convert.ToInt32(50

  • 2.83) + QrBarcode.Height;<o:p></o:p>

    // Create Rectangle and Matrix objects<o:p></o:p>

    Aspose.Pdf.Rectangle
    rectangle = new Aspose.Pdf.Rectangle(lowerLeftX, lowerLeftY, upperRightX,
    upperRightY);<o:p></o:p>

    Matrix matrix = new Matrix(new double[] {<o:p></o:p>

    rectangle.URX - rectangle.LLX,0,0,rectangle.URY - rectangle.LLY,rectangle.LLX,rectangle.LLY

    });<o:p></o:p>

    // Using ConcatenateMatrix (concatenate
    matrix) operator: defines how image must be placed
    <o:p></o:p>

    page.Contents.Add(new
    Aspose.Pdf.Operator.ConcatenateMatrix(matrix));<o:p></o:p>

    XImage ximage =
    page.Resources.Images[page.Resources.Images.Count];<o:p></o:p>

    // Using Do operator: this operator
    draws image
    <o:p></o:p>

    page.Contents.Add(new
    Aspose.Pdf.Operator.Do(ximage.Name));<o:p></o:p>

    // Using GRestore operator: this
    operator restores graphics state
    <o:p></o:p>

    page.Contents.Add(new
    Aspose.Pdf.Operator.GRestore());<o:p></o:p>

    // Save updated document<o:p></o:p>

    pdfDocument.Save(“E:/Data/HelloWorld_Barcode.pdf”);<o:p></o:p>


Best Regards,