How to Create and Stamp Barcodes in PDF Documents Using IronBarcode

How to Stamp Barcodes on PDFs Using C#

Stamp barcodes onto existing PDF documents in C# using IronBarcode's CreateBarcode method with StampToExistingPdfPage for single pages or StampToExistingPdfPages for multiple pages, specifying coordinates and page numbers.

Quickstart: Stamp a GeneratedBarcode onto a PDF Page

This example demonstrates generating a barcode using IronBarCode's CreateBarcode and stamping it onto an existing PDF page. Supply the PDF path, position coordinates, and page number.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronBarcode with NuGet Package Manager

    PM > Install-Package BarCode

  2. Copy and run this code snippet.

    IronBarCode.BarcodeWriter.CreateBarcode("https://my.site", IronBarCode.BarcodeEncoding.QRCode, 150, 150)
        .StampToExistingPdfPage("report.pdf", x: 50, y: 50, pageNumber: 1);
  3. Deploy to test on your live environment

    Start using IronBarcode in your project today with a free trial
    arrow pointer

How Do I Stamp a Barcode on an Existing PDF Page?

Apart from exporting barcodes as PDF, IronBarcode enables stamping the GeneratedBarcode directly onto existing PDF documents. This feature is useful when adding tracking codes, inventory labels, or document identifiers to existing reports, invoices, or forms. The following code snippet demonstrates this task.

:path=/static-assets/barcode/content-code-examples/how-to/StampBarcodeOnExistingPdfPage.cs
using IronBarCode;

GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.Code128, 200, 100);
myBarcode.StampToExistingPdfPage("pdf_file_path.pdf", x: 200, y: 100, 3, "password");
Imports IronBarCode

Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.Code128, 200, 100)
myBarcode.StampToExistingPdfPage("pdf_file_path.pdf", x:= 200, y:= 100, 3, "password")
$vbLabelText   $csharpLabel

What Parameters Does StampToExistingPdfPage Require?

The code snippet calls the StampToExistingPdfPage() method with a GeneratedBarcode object to stamp the object onto the PDF document. This method provides flexibility while maintaining simplicity. Below are the method parameters:

  • pdfFilePath: A System.String representing the PDF document path (relative or absolute).
  • x: A System.Int32 for horizontal position in pixels from the left edge.
  • y: A System.Int32 for vertical position in pixels from the bottom edge.
  • pageNumber: A System.Int32 indicating the page (1-indexed, first page is 1).
  • password: A System.String for password-protected PDFs (optional).

When Should I Use StampToExistingPdfPage?

Running the code snippet stamps the GeneratedBarcode directly into the PDF document without intermediate saving. This method suits scenarios requiring:

  • Unique tracking codes on shipping labels or delivery documents
  • Batch numbers on manufacturing reports
  • Document control numbers on legal or regulatory forms
  • QR codes for digital authentication or quick access links

The direct stamping approach saves processing time and eliminates temporary files. For information on different barcode types, see the supported barcode formats guide.

How Do I Stamp a Barcode on Multiple PDF Pages?

Sometimes the same barcode needs stamping on multiple pages. Common uses include applying document identifiers to every page of multi-page reports, adding version control codes throughout technical documents, or inserting security barcodes on each page of confidential materials. Instead of looping the single-page method, use the StampToExistingPdfPages() method from the GeneratedBarcode class. The following code snippet demonstrates this method.

:path=/static-assets/barcode/content-code-examples/how-to/StampBarcodeOnMultiplePdfPages.cs
using IronBarCode;
using System.Collections.Generic;

GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.Code128, 200, 100);
List<int> pages = new List<int>();
pages.Add(1);
pages.Add(2);
pages.Add(3);
myBarcode.StampToExistingPdfPages("pdf_file_path.pdf", x: 200, y: 100, pages, "password");
Imports IronBarCode
Imports System.Collections.Generic

Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.Code128, 200, 100)
Private pages As New List(Of Integer)()
pages.Add(1)
pages.Add(2)
pages.Add(3)
myBarcode.StampToExistingPdfPages("pdf_file_path.pdf", x:= 200, y:= 100, pages, "password")
$vbLabelText   $csharpLabel

For flexibility, use LINQ to generate page ranges dynamically:

// Stamp on all even pages from 2 to 10
var evenPages = Enumerable.Range(1, 10).Where(x => x % 2 == 0).ToList();
myBarcode.StampToExistingPdfPages("pdf_file_path.pdf", x: 200, y: 100, evenPages, "password");

// Stamp on the first and last 3 pages of a 20-page document
var selectedPages = new List<int> { 1, 2, 3, 18, 19, 20 };
myBarcode.StampToExistingPdfPages("pdf_file_path.pdf", x: 200, y: 100, selectedPages, "password");
// Stamp on all even pages from 2 to 10
var evenPages = Enumerable.Range(1, 10).Where(x => x % 2 == 0).ToList();
myBarcode.StampToExistingPdfPages("pdf_file_path.pdf", x: 200, y: 100, evenPages, "password");

// Stamp on the first and last 3 pages of a 20-page document
var selectedPages = new List<int> { 1, 2, 3, 18, 19, 20 };
myBarcode.StampToExistingPdfPages("pdf_file_path.pdf", x: 200, y: 100, selectedPages, "password");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

What Parameters Does StampToExistingPdfPages Accept?

Below are the method parameters:

  • pdfFilePath: A System.String representing the PDF document path.
  • x: A System.Int32 for horizontal position in pixels.
  • y: A System.Int32 for vertical position in pixels.
  • pageNumbers: An IEnumerableof pages to stamp (1-indexed).
  • password: A System.String for password-protected PDFs (optional).

Why Use StampToExistingPdfPages Instead of Looping?

This method provides efficient barcode stamping on multiple pages without manual iteration, improving code readability and performance. The internal implementation optimizes PDF processing, resulting in:

  • Faster execution: PDF opened and processed once, not multiple times
  • Lower memory usage: Efficient resource management for large PDFs
  • Cleaner code: No manual loop and error handling management
  • Atomic operations: All pages stamped in single operation

Advanced Stamping Techniques

Customizing Barcode Appearance Before Stamping

Before stamping your barcode onto a PDF, customize its appearance. IronBarcode offers extensive customization options:

GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("INVOICE-2024-001", BarcodeEncoding.Code128, 250, 80);

// Customize the appearance
myBarcode.AddAnnotationTextAboveBarcode("Invoice Number");
myBarcode.AddAnnotationTextBelowBarcode("Scan for digital copy");
myBarcode.SetMargins(10);
myBarcode.ChangeBarcodeForegroundColor(System.Drawing.Color.DarkBlue);

// Now stamp the customized barcode
myBarcode.StampToExistingPdfPage("invoice.pdf", x: 450, y: 700, pageNumber: 1);
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("INVOICE-2024-001", BarcodeEncoding.Code128, 250, 80);

// Customize the appearance
myBarcode.AddAnnotationTextAboveBarcode("Invoice Number");
myBarcode.AddAnnotationTextBelowBarcode("Scan for digital copy");
myBarcode.SetMargins(10);
myBarcode.ChangeBarcodeForegroundColor(System.Drawing.Color.DarkBlue);

// Now stamp the customized barcode
myBarcode.StampToExistingPdfPage("invoice.pdf", x: 450, y: 700, pageNumber: 1);
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Working with Different Barcode Types

Different scenarios require different barcode types. QR codes suit URLs and large data sets, while Code128 works for alphanumeric identifiers. Learn more about creating QR codes or explore other formats:

// QR Code for contact information
var qrCode = BarcodeWriter.CreateBarcode("BEGIN:VCARD\nFN:John Doe\nTEL:555-1234\nEND:VCARD", 
    BarcodeEncoding.QRCode, 150, 150);
qrCode.StampToExistingPdfPage("businesscard.pdf", x: 400, y: 50, pageNumber: 1);

// Data Matrix for product tracking
var dataMatrix = BarcodeWriter.CreateBarcode("PROD-2024-BATCH-789", 
    BarcodeEncoding.DataMatrix, 100, 100);
dataMatrix.StampToExistingPdfPage("product_sheet.pdf", x: 50, y: 750, pageNumber: 1);
// QR Code for contact information
var qrCode = BarcodeWriter.CreateBarcode("BEGIN:VCARD\nFN:John Doe\nTEL:555-1234\nEND:VCARD", 
    BarcodeEncoding.QRCode, 150, 150);
qrCode.StampToExistingPdfPage("businesscard.pdf", x: 400, y: 50, pageNumber: 1);

// Data Matrix for product tracking
var dataMatrix = BarcodeWriter.CreateBarcode("PROD-2024-BATCH-789", 
    BarcodeEncoding.DataMatrix, 100, 100);
dataMatrix.StampToExistingPdfPage("product_sheet.pdf", x: 50, y: 750, pageNumber: 1);
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Error Handling and Best Practices

Implement proper error handling when working with PDF stamping operations:

try
{
    GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("DOCUMENT-ID-12345", 
        BarcodeEncoding.Code128, 200, 60);

    // Verify the PDF exists before attempting to stamp
    if (File.Exists("target.pdf"))
    {
        myBarcode.StampToExistingPdfPage("target.pdf", x: 100, y: 100, pageNumber: 1);
        Console.WriteLine("Barcode stamped successfully!");
    }
    else
    {
        Console.WriteLine("PDF file not found!");
    }
}
catch (Exception ex)
{
    Console.WriteLine($"Error stamping barcode: {ex.Message}");
    // Log the error or handle it appropriately
}
try
{
    GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("DOCUMENT-ID-12345", 
        BarcodeEncoding.Code128, 200, 60);

    // Verify the PDF exists before attempting to stamp
    if (File.Exists("target.pdf"))
    {
        myBarcode.StampToExistingPdfPage("target.pdf", x: 100, y: 100, pageNumber: 1);
        Console.WriteLine("Barcode stamped successfully!");
    }
    else
    {
        Console.WriteLine("PDF file not found!");
    }
}
catch (Exception ex)
{
    Console.WriteLine($"Error stamping barcode: {ex.Message}");
    // Log the error or handle it appropriately
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Performance Considerations

When working with large PDFs or multiple stamping operations, consider these tips:

  1. Batch Operations: Use StampToExistingPdfPages() instead of looping StampToExistingPdfPage()
  2. Barcode Caching: Create once and reuse the GeneratedBarcode object
  3. Coordinate Calculation: Pre-calculate consistent position coordinates
  4. Memory Management: Process very large PDFs in batches

For advanced scenarios involving reading barcodes from PDFs after stamping, see our guide on reading barcodes from PDF documents.

Integration with Other IronBarcode Features

PDF stamping functionality works seamlessly with other IronBarcode features. Combine it with asynchronous processing for better web application performance:

// Asynchronous PDF stamping
public async Task StampBarcodeAsync(string pdfPath, string barcodeData)
{
    await Task.Run(() =>
    {
        var barcode = BarcodeWriter.CreateBarcode(barcodeData, BarcodeEncoding.QRCode, 200, 200);
        barcode.StampToExistingPdfPage(pdfPath, x: 100, y: 100, pageNumber: 1);
    });
}
// Asynchronous PDF stamping
public async Task StampBarcodeAsync(string pdfPath, string barcodeData)
{
    await Task.Run(() =>
    {
        var barcode = BarcodeWriter.CreateBarcode(barcodeData, BarcodeEncoding.QRCode, 200, 200);
        barcode.StampToExistingPdfPage(pdfPath, x: 100, y: 100, pageNumber: 1);
    });
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Additionally, leverage IronBarcode's image correction features when working with scanned PDFs that might need enhancement before or after barcode stamping.

Troubleshooting Common Issues

If encountering issues while stamping barcodes on PDFs, here are solutions:

  1. Coordinate Issues: PDF coordinates start from bottom-left corner, not top-left
  2. Password-Protected PDFs: Ensure correct password parameter for encrypted PDFs
  3. Large File Sizes: For optimization and handling tips, see our troubleshooting guide
  4. Font or Encoding Issues: For special characters or Unicode, check our writing Unicode barcodes guide

Following these guidelines and leveraging IronBarcode's PDF stamping capabilities enables efficient barcode addition to existing PDF documents while maintaining high performance and code quality.

Frequently Asked Questions

How do I add a barcode to an existing PDF document in C#?

Use IronBarcode's CreateBarcode method to generate a barcode, then apply the StampToExistingPdfPage method to place it on your PDF. Simply specify the PDF file path, position coordinates (x, y), and the page number where you want the barcode to appear.

What parameters are required for the StampToExistingPdfPage method?

The StampToExistingPdfPage method in IronBarcode requires: pdfFilePath (string for the PDF location), x and y coordinates (integers for positioning in pixels), pageNumber (integer, 1-indexed), and an optional password parameter for protected PDFs.

Can I stamp the same barcode on multiple pages of a PDF?

Yes, IronBarcode provides the StampToExistingPdfPages method (note the plural 'Pages') which allows you to stamp a single generated barcode across multiple pages in your PDF document.

What coordinate system is used for positioning barcodes on PDFs?

IronBarcode uses a pixel-based coordinate system where the x-coordinate measures from the left edge of the page, and the y-coordinate measures from the bottom edge of the page when using the StampToExistingPdfPage method.

What are common use cases for stamping barcodes on existing PDFs?

IronBarcode's PDF stamping feature is commonly used for adding unique tracking codes to shipping labels, batch numbers on manufacturing reports, document control numbers on legal forms, and QR codes for digital authentication or quick access links.

Does stamping a barcode on a PDF require saving intermediate files?

No, IronBarcode's StampToExistingPdfPage method stamps the barcode directly onto the PDF document without creating temporary files, which saves processing time and storage space.

Can I stamp barcodes on password-protected PDF documents?

Yes, IronBarcode supports stamping barcodes on password-protected PDFs. Simply provide the PDF password as an optional parameter in the StampToExistingPdfPage method.

Hairil Hasyimi Bin Omar
Software Engineer
Like all great engineers, Hairil is an avid learner. He’s refining his knowledge of C#, Python, and Java, using that knowledge to add value to team members across Iron Software. Hairil joined the Iron Software team from Universiti Teknologi MARA in Malaysia, where he graduated with a Bachelor's degree ...
Read More
Ready to Get Started?
Nuget Downloads 1,990,480 | Version: 2025.12 just released