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:
AddQRCode()
methods create a QR code with the specified parameters and return an instance of the
BarcodeInlineBuilder
class when called for a paragraph or an instance of the
BarcodeBuilder
class when called for a section, table cell, or repeating area.
AddQRCodeToParagraph()
methods add a QR code with the specified parameters to a paragraph and return the instance of ParagraphBuilder
.
AddQRCodeToSection()
methods add a QR code with the specified parameters to a section and return the instance of SectionBuilder
.
AddQRCodeToCell()
methods add a QR code with the specified parameters to a table cell and return the instance of TableCellBuilder
.
AddQRCodeToRepeatingArea()
methods add a QR code with the specified parameters to a repeating area and return the instance of RepeatingAreaBuilder
.
When adding a QR code, you can specify the following settings:
QR code encoding method (the encoding
parameter). Currently only the Binary
option is available.
QR code error correction level (the levelCorrection
parameter). Defines the error correction capability to restore data if the code is dirty or damaged.
The four available correction levels are listed in the QRCodeErrorCorrection
enumeration.
QR code version (the version
parameter). Defines the size (the number of modules) of the QR code - the greater version you set, the larger QR code you get.
You can use versions from 1 to 40: version 1 has 21 × 21 modules, version 40 has 177 × 177 modules. Each higher version number comprises four additional modules per side.
The library will define the necessary QR code version for you, if you specify the version value Automatic
.
The available QR code versions are listed in the QRCodeVersion
enumeration.
QR code pixel scaling (the scaleMultiplier
parameter). Defines the pixel scaling of the resulting image. The pixel scaling allows getting clearer QR code images.
The value is a positive integer that defines how much times the size of the generated QR code is increased. If the QR code image is blurred, increase the value of this parameter.
For an illustration, see Example 7 below.
QR code color (the foregroundColor
parameter). QR codes support both predefined colors and RGB colors created using the Color.FromRgba
method.
Background color (the backgroundColor
parameter). QR codes support both predefined colors and RGB colors created using the Color.FromRgba
method.
Whether the QR code image has a quiet zone (the hasQuietZone
parameter). By default, the quiet zone is drawn around the QR code, but you can remove it if needed
by setting the parameter value to False
.
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:
AddQRCodeUrl()
methods create a QR code with a URL and the specified parameters and return an instance of the
BarcodeInlineBuilder
class when called for a paragraph or an instance of the
BarcodeBuilder
class when called for a section, table cell, or repeating area.
AddQRCodeUrlToParagraph()
methods add a QR code with a URL and the specified parameters to a paragraph and return the instance of ParagraphBuilder
.
AddQRCodeUrlToSection()
methods add a QR code with a URL and the specified parameters to a section and return the instance of SectionBuilder
.
AddQRCodeUrlToCell()
methods add a QR code with a URL and the specified parameters to a table cell and return the instance of TableCellBuilder
.
AddQRCodeUrlToRepeatingArea()
methods add a QR code with a URL and the specified parameters to a repeating area and return the instance of RepeatingAreaBuilder
.
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]