Generate a QR barcode in a PDF

Generate PDF, Shapes
11/10/2011

Downloads

This article shows how to generate a QR-code for a new or existing PDF document using the QRBarcodeShape.

About QR-Code

A QR code (Quick Response code) is a type of matrix barcode (or two-dimensional code) that consists of black modules arranged in a square pattern on a white background. The information encoded can be made up of any kind of data (e.g., binary, alphanumeric, or Kanji symbols).

C# code sample

1 //create a new document with one page 2 Document document = new Document(); 3 Page page = new Page(PageSize.Letter); 4 document.Pages.Add(page); 5 6 //create a QR barcode which points to tallcomponents.com. 7 QRBarcodeShape qrBarcode = new QRBarcodeShape(); 8 qrBarcode.Data = @"http://www.tallcomponents.com"; 9 10 //make the QR barcode square 11 qrBarcode.Width = qrBarcode.Height = 50; 12 qrBarcode.Transform = new TranslateTransform(72, page.Height - 72 - qrBarcode.Height); 13 page.VisualOverlay.Add(qrBarcode); 14 15 using (FileStream file = new FileStream("out.pdf", FileMode.Create, FileAccess.Write)) 16 { 17 document.Write(file); //save the pdf 18 }

VB.NET code sample

1 'create a new document with one page 2 Dim document As New Document() 3 Dim page As New Page(PageSize.Letter) 4 document.Pages.Add(page) 5 6 'create a QR barcode which points to tallcomponents.com. 7 Dim qrBarcode As New QRBarcodeShape() 8 qrBarcode.Data = "http://www.tallcomponents.com" 9 10 'make the QR barcode square 11 qrBarcode.Width = qrBarcode.Height = 50 12 qrBarcode.Transform = New TranslateTransform(72, page.Height - 72 - qrBarcode.Height) 13 page.VisualOverlay.Add(qrBarcode) 14 15 Using file As New FileStream("out.pdf", FileMode.Create, FileAccess.Write) 16 'save the pdf 17 document.Write(file) 18 End Using 19 }

This will create the following result:

generate-qr-code-pdf.png