Adding QR Codes

Brief

The article describes how to add and configure QR codes.

Details

You can add QR codes (that are a type of matrix barcode) to your documents. Pass data for encoding, specify the necessary settings, and the library will automatically encode the QR code and generate the QR code image.

QR codes can be added to paragraphs, sections, table cells, and repeating areas.

To add a QR code, use one of the methods of the QRCodesUserAPI class:

When adding a QR code, you can specify the following settings:

If you want to add a QR code with a URL, you do not have to add a URL and a QR code one by one. You can do it at once using one of the following methods:

When adding a QR code with a URL, specify the URL text in the textUrl parameter. You can also configure the QR code scaling (the scaleMultiplier parameter), color (the foregroundColor parameter), and background color (the backgroundColor parameter).

Also, you can configure the QR code size, borders, margins, and paddings using the methods of the BarcodeInlineBuilder and BarcodeBuilder classes, for example SetWidth, SetMargins, SetPaddings, SetBorder.

If you need to configure the size of your barcode, use either SetWidth or SetHeight method to specify either the width or the height. The other parameter will be set to the same value automatically as the QR code aspect ratio is 1:1. If you use both SetWidth and SetHeight methods to configure the size of a QR code, make sure that they have the same values.

Examples

Example 1. Add a QR code to a paragraph [hide]

            DocumentBuilder.New()
                .AddSection()
                    .AddParagraph()
                        .AddQRCode("Some information for sharing",
                            QRCodeEncodingMethod.Binary,
                            QRCodeErrorCorrection.M,
                            QRCodeVersion.Version4,
                            4)
            .ToDocument()
                .Build("Result.pdf");

 The above code will generate the following:
 
 See the document

Example 2. Add a QR code with a URL to a paragraph [hide]

            var colorDarkBlue = Color.FromHtml("#00008B");
            var colorLightYellow = Color.FromHtml("#FFFFE0");
            DocumentBuilder.New()
                .AddSection()
                    .AddParagraph().AddUrl("https://www.pdfflow.io/").ToSection()
                    .AddParagraph().AddQRCodeUrlToParagraph("https://www.pdfflow.io/", 4,
                                                            colorDarkBlue, colorLightYellow)
            .ToDocument()
                .Build("Result.pdf");

 The above code will generate the following:
 
 See the document

Example 3. Add a QR code with a URL to a table cell [hide]

            var textUrl = "https://www.nuget.org/packages/Gehtsoft.PDFFlowLib/";
            DocumentBuilder.New()
                .AddSection()
                    .AddTable()
                        .SetWidth(230)
                        .AddColumnToTable()
                        .AddColumnToTable()
                        .AddRow()
                            .AddCellToRow("QR-code in table cell")
                            .AddCell()
                                .AddQRCodeUrl(textUrl)
                                    .SetWidth(150)
            .ToDocument()
                .Build("Result.pdf");

 The above code will generate the following:
 
 See the document

Example 4. Add a QR code with an URL and a border to a header [hide]

            DocumentBuilder.New()
                .AddSection()
                    .AddHeaderToBothPages(120)
                        .AddQRCodeUrl("https://www.pdfflow.io/#documentation", 4)
                            .SetBorder(Stroke.Solid, Color.Blue, 2)
                            .SetHeight(100)
                    .ToArea()
                        .AddParagraph("Comprehensive\nDocumentation")
            .ToDocument()
                .Build("Result.pdf");

 The above code will generate the following:
 
 See the document

Example 5. Add a QR code with automatic version selection [hide]

            DocumentBuilder.New()
                .AddSection()
                    .AddParagraphToSection("Automatic selected version of QR-Code")
                    .AddQRCode("You can add QR codes " +
                               "(that are a type of matrix barcode) to your documents.",
                               QRCodeEncodingMethod.Binary, QRCodeErrorCorrection.M)
                        .SetWidth(150)
            .ToDocument()
                .Build("Result.pdf");

 The above code will generate the following:
 
 See the document

Example 6. Add a QR code to a repeating area [hide]

            DocumentBuilder.New()
                .AddSection()
                    .AddRptAreaLeftToBothPages(70)
                        .AddQRCodeUrl("https://github.com/gehtsoft-usa/PDF.Flow")
                            .SetWidth(100)
                            .SetMarginRight(10)
                .ToSection()
                    .AddParagraph("QR-Code in repeating area")
                        .SetMarginTop(5)
            .ToDocument()
                .Build("Result.pdf");

 The above code will generate the following:
 
 See the document

Example 7. QR code scaling [show]

back