How to Stamp Barcodes on PDF Documents in C#
- Download C# library to stamp barcode on PDF
- Create a barcode with a specified barcode type and value
- Specify the barcode size
- Utilize the
StampToExistingPdfPage
method to stamp the barcode on a single PDF page - Use the
StampToExistingPdfPages
method to stamp the barcode on multiple PDF pages
Start using IronBarcode in your project today with a free trial.
Stamp Barcode on Existing PDF Page
Apart from exporting barcodes as PDF, one of the most popular functionalities in IronBarcode is the ability to stamp the GeneratedBarcode
directly onto a user's existing PDF document. The following code snippet demonstrates how to do this.
:path=/static-assets/barcode/content-code-examples/how-to/StampBarcodeOnExistingPdfPage.cs
// Include the IronBarCode namespace to work with barcode generation and manipulation
using IronBarCode;
public class BarcodeHandler
{
public static void Main()
{
// Generate a barcode with specific encoding and dimensions
// - The first argument is the data to encode, e.g., a URL.
// - The second argument specifies the type of barcode encoding (Code128 in this case).
// - The third and fourth arguments specify the width and height of the barcode.
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode(
"https://ironsoftware.com/csharp/barcode/", // Data to encode
BarcodeEncoding.Code128, // Type of barcode encoding
200, // Width of the barcode
100 // Height of the barcode
);
// Stamp the generated barcode into an existing PDF file at a specified location
// - The first argument is the file path to an existing PDF where the barcode will be stamped.
// - The 'x' and 'y' parameters define the position on the PDF page where the barcode will be placed.
// - The 'pageNumber' parameter specifies the page within the PDF file (PDF pages can be 1-indexed or 0-indexed based on the library).
// - The last argument is for a password if the PDF is encrypted; otherwise, use null or an empty string if not needed.
// Note: Ensure the pageNumber is correct as per the library conventions.
myBarcode.StampToExistingPdfPage(
"pdf_file_path.pdf", // File path to existing PDF
x: 200, // X position on the page
y: 100, // Y position on the page
pageNumber: 2, // Page number in the PDF (1-indexed for IronBarCode)
password: null // PDF password if encrypted, or null for no password
);
}
}
From the code snippet above, we simply call the StampToExistingPdfPage()
method with a GeneratedBarcode
object to stamp the object onto the PDF document. Below are the parameters used in this method:
pdfFilePath
: A System.String representing the path of the PDF document in memory.x
: A System.Int32 representing the horizontal position on the PDF page in pixels.y
: A System.Int32 representing the vertical position on the PDF page in pixels.pageNumber
: A System.Int32 indicating the PDF page to stamp. Note this value is 1-indexed, so the first page is represented as 1.password
: A System.String for a password required to open the PDF. This is optional and only used for password-protected PDFs. Users can leave this argument blank if the PDF is not password-protected.
Running the code snippet above will stamp the GeneratedBarcode
immediately into the PDF document without an intermediate document saving step.
Stamp Barcode on Multiple PDF Pages
Sometimes, the same barcode is needed to be stamped on multiple pages rather than on one page. Instead of looping the above method to stamp the same barcode on multiple pages, users can use the StampToExistingPdfPages()
method from the GeneratedBarcode
class to do this directly. The following code snippet demonstrates how this method can be used.
:path=/static-assets/barcode/content-code-examples/how-to/StampBarcodeOnMultiplePdfPages.cs
using IronBarCode;
using System;
using System.Collections.Generic;
// Generate a barcode of type Code128 with specified text, width, and height.
try
{
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode(
"https://ironsoftware.com/csharp/barcode/", // The text or URL to encode in the barcode.
BarcodeEncoding.Code128, // The type of the barcode encoding format.
200, // The width of the barcode in pixels.
100 // The height of the barcode in pixels.
);
// List of page numbers where the barcode will be stamped.
List<int> pages = new List<int>
{
1, // First page to stamp the barcode.
2, // Second page to stamp the barcode.
3 // Third page to stamp the barcode.
};
// Attempt to stamp the generated barcode onto the specified pages of an existing PDF file.
myBarcode.StampToExistingPdfPages(
"pdf_file_path.pdf", // The path to the PDF file where the barcode will be stamped.
x: 200, // X-axis position for stamping the barcode on the page.
y: 100, // Y-axis position for stamping the barcode on the page.
pages, // Pages of the PDF where the barcode needs to be stamped.
"password" // Password for opening the PDF if it's password-protected.
);
}
catch (Exception ex)
{
Console.WriteLine("An error occurred: " + ex.Message);
}
Below are the parameters used in this method:
pdfFilePath
: A System.String representing the path of the PDF document in memory.x
: A System.Int32 representing the horizontal position on the PDF page in pixels.y
: A System.Int32 representing the vertical position on the PDF page in pixels.pageNumbers
: An IEnumerablecomprising the PDF pages to stamp. Note these values are 1-indexed, so the first page of a PDF is represented as 1. password
: A System.String for a password required to open the PDF. This is optional and only used for password-protected PDFs. Users can leave this argument blank if the PDF is not password-protected.
Frequently Asked Questions
How can I stamp a barcode on a PDF document using IronBarcode in .NET?
You can use the IronBarcode library to stamp a barcode on a PDF by utilizing the StampToExistingPdfPage method. First, create a PdfDocument from your existing file, then call the method with the GeneratedBarcode object and specify the position and page number.
What is the StampToExistingPdfPage method used for?
The StampToExistingPdfPage method is used to stamp a GeneratedBarcode onto a specific page of an existing PDF document at a defined position.
Can I stamp a barcode on multiple pages of a PDF at once?
Yes, you can stamp a barcode on multiple pages using the StampToExistingPdfPages method. This method allows you to specify multiple page numbers and apply the barcode to each of them.
What parameters are required for the StampToExistingPdfPage method?
The required parameters for the StampToExistingPdfPage method include pdfFilePath, x and y coordinates for position, pageNumber, and an optional password for password-protected PDFs.
Is it possible to use IronBarcode with password-protected PDFs?
Yes, IronBarcode can be used with password-protected PDFs. You need to provide the password as an optional parameter when creating the PdfDocument.
What is the difference between StampToExistingPdfPage and StampToExistingPdfPages methods?
The StampToExistingPdfPage method is used for stamping a barcode on a single page, while StampToExistingPdfPages allows you to stamp the barcode on multiple specified pages.
How do I specify the position for stamping a barcode on a PDF?
You specify the position for stamping a barcode by providing x and y coordinates in pixels, which determine the horizontal and vertical position on the PDF page.
What barcode types can be created using IronBarcode?
IronBarcode supports creating various barcode types, including QR codes, Code 128, UPC, and many others. You can specify the type when creating a barcode.
Do I need to save the PDF after stamping barcodes?
Yes, after stamping the barcodes, you need to save the modified PDF document using the SaveAs method to retain the changes.
Where can I download the IronPdf library?
You can download the IronPdf library from the NuGet package manager at nuget.org/packages/IronPdf.