Add Barcodes and QR codes to PDF in C#

Andrew Wilson
4 min readOct 17, 2024

--

Adding barcodes to a PDF document can add an extra layer of information to the document, which can be useful for purposes such as automated processing, inventory management, or simple data retrieval. In this article, you will learn how to add barcodes and QR codes to a PDF document in C#.

  • Add Barcodes to PDF in C#
  • Add QR codes to PDF in C#

Free .NET PDF Library

Free Spire.PDF for .NET is a free .NET library supports various PDF processing features (with certain page limits).

You can install it directly via NuGet, or download the product package via the link below to unzip it and reference the dlls manually.

How to Add Barcodes to PDF in C#

The Spire.PDF for .NET provides different classes to represent different 1D barcode types (Spire.Pdf.Barcode Namespace). This example will demonstrate how to use the library to draw common Codabar, Code128, and Code39 barcodes in PDF.

Main steps:

  1. Create a PDF document and add pages;
  2. Drawing text on PDF pages;
  3. Create PdfCodabarBarcode object, and then use its Draw() method to draw the Codabar barcode to the specified location on the page;
  4. Create PdfCode128ABarcode object, and then use its Draw() method to draw the Code128 barcode to the specified location on the page;
  5. Create a PdfCode39Barcode object, and then use its Draw() method to draw the Code39 barcode to the specified location on the page;
  6. Save the PDF file.

C# code:

using Spire.Pdf;
using Spire.Pdf.Barcode;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace PDFBarcode
{
class Program
{
static void Main(string[] args)
{
//Create a PDF document
PdfDocument pdf = new PdfDocument();

//Add a page
PdfPageBase page = pdf.Pages.Add();

//Initialize y-coordinate
float y = 20;

//Create a true type font
PdfTrueTypeFont font = new PdfTrueTypeFont("Arial", 11f, PdfFontStyle.Bold, true);

//Draw text on the page
PdfTextWidget text = new PdfTextWidget();
text.Font = font;
text.Text = "Codabar:";
PdfLayoutResult result = text.Draw(page, 0, y);
page = result.Page;
y = result.Bounds.Bottom + 2;

//Draw Codabar barcode on the page
PdfCodabarBarcode Codabar = new PdfCodabarBarcode("00:55-12345/590");
Codabar.BarcodeToTextGapHeight = 1f;
Codabar.TextDisplayLocation = TextLocation.Bottom;
Codabar.TextColor = Color.Green;
Codabar.Draw(page, new PointF(0, y));
y = Codabar.Bounds.Bottom + 6;

//Draw text on the page
text.Text = "Code128-A:";
result = text.Draw(page, 0, y);
page = result.Page;
y = result.Bounds.Bottom + 2;

//Draw Code128-A barcode on the page
PdfCode128ABarcode Code128 = new PdfCode128ABarcode("HELLO 55-150");
Code128.BarcodeToTextGapHeight = 1f;
Code128.TextDisplayLocation = TextLocation.Bottom;
Code128.TextColor = Color.Green;
Code128.Draw(page, new PointF(0, y));
y = Code128.Bounds.Bottom + 6;

//Draw text on the page
text.Text = "Code39:";
result = text.Draw(page, 0, y);
page = result.Page;
y = result.Bounds.Bottom + 2;

//Draw Code39 barcode on the page
PdfCode39Barcode Code39 = new PdfCode39Barcode("15-364559");
Code39.BarcodeToTextGapHeight = 1f;
Code39.TextDisplayLocation = TextLocation.Bottom;
Code39.TextColor = Color.Green;
Code39.Draw(page, new PointF(0, y));

//Save the document
pdf.SaveToFile("AddBarcodes.pdf");
pdf.Close();
}
}
}

Result:

C# Barcode in PDF

How to Add QR codes to PDF in C#

As Free Spire.PDF for Python only supports generating 1D barcodes, to add 2D barcodes to PDF, we need to use it in conjunction with the Free Spire.Barcode for .NET library.

Main Steps:

1. Generate the QR code image using the Free Spire.Barcode library.

  • Create a BarcodeSettings object and then use its Type property to set the barcode type to QRCode.
  • Set the QR code data, width, error correction level, and whether to display text, etc.
  • Create BarCodeGenerator object based on the above settings, then use its GenerateImage() method to generate QR code image.

2. Save the generated QR code image as a PNG image.

3. Draw the QR code image to PDF using the Free Spire.PDF library.

  • Create a PDF document and add a page.
  • Load the QR code image, and then use the DrawImage() method to draw the QR code to the specified location on the PDF page.

4. Save the PDF document.

C# code:

using System.Drawing;
using Spire.Barcode;
using Spire.Pdf;
using Spire.Pdf.Graphics;

namespace PDFQRcode
{
class Program
{
static void Main(string[] args)
{

//Create a BarcodeSettings object
BarcodeSettings settings = new BarcodeSettings();

//Set the barcode type to QR Code
settings.Type = BarCodeType.QRCode;
//Set the data of the QR code
settings.Data = "https://www.google.com/";
settings.Data2D = "Google";
//Set the width of the QR code
settings.X = 2.5f;
//Set the error correction level of the QR code
settings.QRCodeECL = QRCodeECL.Q;
//Set to show QR code text at the bottom
settings.ShowTextOnBottom = true;

//Generate QR code image based on the settings
BarCodeGenerator generator = new BarCodeGenerator(settings);
Image QRimage = generator.GenerateImage();

//Save the generated QR code image as a PNG image
QRimage.Save("QR Code.png");

//Create a PDF document
PdfDocument pdf = new PdfDocument();

//Add a page
PdfPageBase page = pdf.Pages.Add();

//Draw QR code image on the PDF page
PdfImage pdfImage = PdfImage.FromFile("QR Code.png");
page.Canvas.DrawImage(pdfImage, 0, 20);

//Save the document
pdf.SaveToFile("PdfQRCode.pdf");
}
}
}

Result:

C# QR Code in PDF

--

--

Andrew Wilson
Andrew Wilson

Written by Andrew Wilson

Explore C#, Java and Python solutions for processing Word/Excel/PowerPoint/PDF files.

No responses yet