Add barcodes to PDF

Generate PDF, Shapes
8/19/2010

This article demonstrates how to add barcodes to a PDF document.

With PDFKit it is possible to add multiple types of barcodes to your PDF document, such as regular 128 bit barcodes, PDF barcodes and QR codes among others.

pdf-create-barcodes.PNG

C# code sample

1 //create a new PDF and add a single page to it 2 Document doc = new Document(); 3 Page page = new Page(PageSize.Letter); 4 doc.Pages.Add(page); 5 6 //create a new barcode shape and add it to the page 7 Code3of9BarcodeShape shape = new Code3of9BarcodeShape(); 8 page.VisualOverlay.Add(shape); 9 10 //specify some members of the barcode shape 11 shape.Location = TextLocation.BelowEmbedded; 12 shape.Transform = new TranslateTransform(50, 600); 13 shape.Width = 200; 14 shape.Height = 50; 15 shape.Data = "123456789"; 16 17 //create a new qr code shape and add it to the page 18 QRBarcodeShape qr = new QRBarcodeShape(); 19 page.VisualOverlay.Add(qr); 20 21 //specify some members of the qr shape 22 qr.Location = TextLocation.BelowEmbedded; 23 qr.Transform = new TranslateTransform(50, 400); 24 qr.Width = 50; 25 qr.Height = 50; 26 qr.Data = "your data here"; 27 28 using (FileStream stream = new FileStream( 29 @"..\..\out.pdf", FileMode.Create, FileAccess.Write)) 30 { 31 doc.Write(stream); 32 }

VB.NET code sample

1 'create a new PDF and add a single page to it 2 Dim doc As New Document() 3 Dim page As New Page(PageSize.Letter) 4 doc.Pages.Add(page) 5 6 'create a new barcode shape and add it to the page 7 Dim shape As New Code3of9BarcodeShape() 8 page.VisualOverlay.Add(shape) 9 10 'specify some members of the barcode shape 11 shape.Location = TextLocation.BelowEmbedded 12 shape.Transform = New TranslateTransform(50, 600) 13 shape.Width = 200 14 shape.Height = 50 15 shape.Data = "123456789" 16 17 'create a new qr code shape and add it to the page 18 Dim qr As New QRBarcodeShape() 19 page.VisualOverlay.Add(qr) 20 21 'specify some members of the qr shape 22 qr.Location = TextLocation.BelowEmbedded 23 qr.Transform = New TranslateTransform(50, 400) 24 qr.Width = 50 25 qr.Height = 50 26 qr.Data = "your data here" 27 28 Using stream As New FileStream("..\..\out.pdf", FileMode.Create, FileAccess.Write) 29 doc.Write(stream) 30 End Using