How to Create and Stamp Barcodes in PDF Documents Using IronBarcode

In this tutorial, we explore the process of stamping barcodes on PDF files using Iron Barcode. To begin, ensure Iron Barcode is installed on your machine. The process starts by importing the Iron Barcode namespace and utilizing the CreateBarcode method, which requires the content, encoding type, and dimensions for the barcode. In this example, a URL is encoded into a 100x100 pixel QR code.

Next, specify the PDF pages to be modified by creating a list of integers. Use the StampToExistingPdfPages method to apply the barcode. This involves providing the PDF path, X and Y coordinates for placement, and pages for modification, with an optional password for protected files. Running the program applies the barcode to the defined pages, as demonstrated in the output PDF. The QR code, when scanned, redirects to the encoded URL. The tutorial concludes by inviting viewers to try a 30-day trial of the software for firsthand experience.

Here is an example code snippet to demonstrate the process:

// Import the necessary namespace
using IronBarCode;

class BarcodeExample
{
    static void Main()
    {
        // Define the URL to be encoded in the QR code
        string url = "https://www.example.com";

        // Create a QR code with specified content, format, and size
        var qrCode = QRCodeWriter.CreateQrCode(url, 100);

        // Specify the path of the PDF file to be modified
        string pdfFilePath = "input.pdf";

        // Specify the pages to modify. For example, pages 1 and 2
        var pagesToModify = new List<int> { 1, 2 };

        // Specify the X and Y coordinates for where the barcode will be placed
        int xCoordinate = 300;
        int yCoordinate = 450;

        // Optionally, specify a password for the PDF if it's protected
        string pdfPassword = "password"; // Leave empty if no password

        // Stamp the barcode onto specified PDF pages
        qrCode.StampToExistingPdfPages(pdfFilePath, xCoordinate, yCoordinate, pagesToModify, pdfPassword);

        // Successfully applied the QR code onto the specified pages of the PDF
        Console.WriteLine("Barcode stamped on PDF successfully.");
    }
}
// Import the necessary namespace
using IronBarCode;

class BarcodeExample
{
    static void Main()
    {
        // Define the URL to be encoded in the QR code
        string url = "https://www.example.com";

        // Create a QR code with specified content, format, and size
        var qrCode = QRCodeWriter.CreateQrCode(url, 100);

        // Specify the path of the PDF file to be modified
        string pdfFilePath = "input.pdf";

        // Specify the pages to modify. For example, pages 1 and 2
        var pagesToModify = new List<int> { 1, 2 };

        // Specify the X and Y coordinates for where the barcode will be placed
        int xCoordinate = 300;
        int yCoordinate = 450;

        // Optionally, specify a password for the PDF if it's protected
        string pdfPassword = "password"; // Leave empty if no password

        // Stamp the barcode onto specified PDF pages
        qrCode.StampToExistingPdfPages(pdfFilePath, xCoordinate, yCoordinate, pagesToModify, pdfPassword);

        // Successfully applied the QR code onto the specified pages of the PDF
        Console.WriteLine("Barcode stamped on PDF successfully.");
    }
}
' Import the necessary namespace
Imports IronBarCode

Friend Class BarcodeExample
	Shared Sub Main()
		' Define the URL to be encoded in the QR code
		Dim url As String = "https://www.example.com"

		' Create a QR code with specified content, format, and size
		Dim qrCode = QRCodeWriter.CreateQrCode(url, 100)

		' Specify the path of the PDF file to be modified
		Dim pdfFilePath As String = "input.pdf"

		' Specify the pages to modify. For example, pages 1 and 2
		Dim pagesToModify = New List(Of Integer) From {1, 2}

		' Specify the X and Y coordinates for where the barcode will be placed
		Dim xCoordinate As Integer = 300
		Dim yCoordinate As Integer = 450

		' Optionally, specify a password for the PDF if it's protected
		Dim pdfPassword As String = "password" ' Leave empty if no password

		' Stamp the barcode onto specified PDF pages
		qrCode.StampToExistingPdfPages(pdfFilePath, xCoordinate, yCoordinate, pagesToModify, pdfPassword)

		' Successfully applied the QR code onto the specified pages of the PDF
		Console.WriteLine("Barcode stamped on PDF successfully.")
	End Sub
End Class
$vbLabelText   $csharpLabel

Explanation of the Code:

  • Import IronBarCode namespace at the beginning of your program.
  • Create a QR code using the QRCodeWriter.CreateQrCode method by providing the URL and desired dimensions.
  • Specify the PDF file path where the QR code will be stamped.
  • Define which pages of the PDF should be modified by creating a list of integers representing page numbers.
  • Set the X and Y coordinates to determine the placement of the QR code on the PDF page.
  • If the PDF is password-protected, input the password. If not, leave it as an empty string.
  • Use the StampToExistingPdfPages method to apply the QR code to the specified PDF pages.

Further Reading: How to Stamp Barcodes on PDFs

Jordi Bardia
Software Engineer
Jordi is most proficient in Python, C# and C++, when he isn’t leveraging his skills at Iron Software; he’s game programming. Sharing responsibilities for product testing, product development and research, Jordi adds immense value to continual product improvement. The varied experience keeps him challenged and engaged, and he says it’s one of his favorite aspects of working with Iron Software. Jordi grew up in Miami, Florida and studied Computer Science and Statistics at University of Florida.
< PREVIOUS
How to Create Barcodes as HTML in C# 1
NEXT >
How to Generate QR codes and Barcodes in C# for .NET 5 using Iron Barcode