C#: Add Barcodes to PDF

2024-08-16 02:52:00 Written by  support iceblue
Rate this item
(1 Vote)

Barcodes provide a quick and efficient way to identify and track items, making them indispensable in a variety of industries. By adding barcodes to PDFs, companies can enhance their document management processes, allowing for a more streamlined way to handle and track PDF files. Additionally, this operation also enables the creation of dynamic, interactive PDF documents that not only contain traditional text and images, but also integrate the functionality of barcodes. In this article, you will learn how to add barcodes to PDF in C# using Spire.PDF for .NET and Spire.Barcode for .NET.

Install Spire.PDF for .NET

To begin with, you need to download Spire.PDF for.NET and Spire.Barcode for .NET libraries, then add the DLL files included in both product packages as references in your .NET project. Or you can install them via NuGet.

PM> Install-Package Spire.PDF 
PM> Install-Package Spire.Barcode

Add Barcodes to PDF in C#

Spire.PDF for .NET support several 1D barcode types represented by different classes, such as PdfCodabarBarcode, PdfCode128ABarcode, PdfCode32Barcode, PdfCode39Barcode, PdfCode93Barcode.

Each class provides corresponding properties for setting the barcode text, size, color, etc. The following are the steps to draw the common Codabar, Code128, Code39 and Code93 barcodes at the specified locations on a PDF page.

  • Create a PdfDocument object.
  • Add a PDF page using PdfDocument.Pages.Add() method.
  • Create a PdfTextWidget object and draw text on the page using PdfTextWidget.Draw(  PdfPageBase page, float x, float y) method.
  • Create PdfCodabarBarcode, PdfCode128ABarcode, PdfCode39Barcode, PdfCode93Barcode objects.
  • Set the gap between the barcode and the displayed text through the BarcodeToTextGapHeight property of the corresponding classes.
  • Sets the barcode text display location through the TextDisplayLocation property of the corresponding classes.
  • Set the barcode text color through the TextColor property of the corresponding classes.
  • Draw the barcodes at specified locations on the PDF page using the Draw(PdfPageBase page, PointF location) method of the corresponding classes.
  • Save the result PDF file using PdfDocument.SaveToFile() method.
  • C#
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(PdfPageSize.A4);

            //Initialize y-coordinate
            float y = 20;

            //Create a true type font
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 12f, FontStyle.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:12-3456/7890");
            Codabar.BarcodeToTextGapHeight = 1f;
            Codabar.TextDisplayLocation = TextLocation.Bottom;
            Codabar.TextColor = Color.Blue;
            Codabar.Draw(page, new PointF(0, y));

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

            //Draw Code128-A barcode on the page
            PdfCode128ABarcode Code128 = new PdfCode128ABarcode("HELLO 00-123");
            Code128.BarcodeToTextGapHeight = 1f;
            Code128.TextDisplayLocation = TextLocation.Bottom;
            Code128.TextColor = Color.Blue;
            Code128.Draw(page, new PointF(240, y));

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

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

            //Draw text on the page
            text.Text = "Code93:";
            result = text.Draw(page, 240, Code128.Bounds.Bottom + 8);
            page = result.Page;
            y = result.Bounds.Bottom + 2;

            //Draw Code93 barcode on the page
            PdfCode93Barcode Code93 = new PdfCode93Barcode("16-273849");
            Code93.BarcodeToTextGapHeight = 1f;
            Code93.TextDisplayLocation = TextLocation.Bottom;
            Code93.TextColor = Color.Blue;
            Code93.QuietZone.Bottom = 5;
            Code93.Draw(page, new PointF(240, y));

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

C#: Add Barcodes to PDF

Add QR Codes to PDF in C#

To add 2D barcodes to a PDF file, the Spire.Barcode for .NET library is required to generate QR code first, and then you can add the QR code image to the PDF file with the Spire.PDF for .NET library. The following are the detailed steps.

  • Create a PdfDocument object.
  • Add a PDF page using PdfDocument.Pages.Add() method.
  • Create a BarcodeSettings object.
  • Call the corresponding properties of the BarcodeSettings class to set the barcode type, data, error correction level and width, etc.
  • Create a BarCodeGenerator object based on the settings.
  • Generate QR code image using BarCodeGenerator.GenerateImage() method.
  • Draw the QR code image at a specified location on the PDF page using PdfPageBase.Canvas.DrawImage(PdfImage image, float x, float y) method.
  • Save the result PDF file using PdfDocument.SaveToFile() method.
  • C#
using System.Drawing;
using Spire.Barcode;
using Spire.Pdf;
using Spire.Pdf.Graphics;

namespace PDFQRcode
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PDF document
            PdfDocument pdf = new PdfDocument();
            //Add a page
            PdfPageBase page = pdf.Pages.Add();

            //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 = "E-iceblue";
            settings.Data2D = "E-iceblue";
            //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();

            //Initialize y-coordinate
            float y = 20;

            //Create a true type font
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 12f, FontStyle.Bold), true);

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

            //Draw QR code image on the PDF page
            PdfImage pdfImage = PdfImage.FromImage(QRimage);
            page.Canvas.DrawImage(pdfImage, 0, y);

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

C#: Add Barcodes to PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Friday, 16 August 2024 01:15