How to Stamp Barcodes on PDFs

by Hairil Hasyimi Bin Omar



C# NuGet Library for

Install with NuGet

Install-Package BarCode
or
C#  DLL

Download DLL

Download DLL

Manually install into your project

Stamp Barcode on Existing PDF Page

Apart from Exporting Barcode as PDF, one of the most sorted out functionality in IronBarcode is the ability to stamp the GeneratedBarcode directly into the users existing PDF document. This can be easily done by calling StampToExistingPdfPage() method on the GeneratedBarcode object. Let's see the implementation of this method in the code snippet below

: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")
VB   C#

From the code snippet above, we simply call the StampToExistingPdfPage() method at the GeneratedBarcode object to stamp the object into the PDF document. Below is the list of arguments accepted in this method:

  • FilePath : This argument is of System.String type where the value of string is the path that points to the PDF document inside the disk.
  • Coordinates: This argument specifies the coordinate of the location in the PDF document where the GeneratedBarcode need to be stamped. This is basically two System.Int32 type arguments, which are X and Y coordinates with pixels(px) as the measurement unit.
  • PageNumber: This argument allows users to specify the page in the PDF document to be stamped with the GeneratedBarcode. If this argument is not specified, a default page number value of 1 will be used.
  • Password: This argument is optional and only be used for PDF document that is protected by a password. Users can leave this argument if the PDF document to be stamped is not protected with password.

Running the code snippet above will stamp the GeneratedBarcode immediately into the PDF document without having to save the document.

Stamp Barcode on Multiple PDF Pages

Sometimes, same barcode is needed to be stamped on multiple pages rather than 1 page. Instead of looping the above method to stamp the same barcode on multiple pages, users can use StampToExistingPdfPages() method from the GeneratedBarcode class which is a direct method to do just that. Let's look at the code snippet below on how to use the 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")
VB   C#

From the code snippet above, the arguments used are pretty much similar to StampToExistingPdfPage() method, such as FilePath, coordinates, and password. The only difference is:

  • Page: This argument accepts a List of integers that represent a collection of page numbers in the PDF document to be stamped with the GeneratedBarcode. This method is 1 based, which means the first page is 1 instead of 0. The code snippet above instantiates an integer list and populate it with the number 1, 2, and 3. This will stamp the GeneratedBarcode im the first three pages of the PDF document.

Note: Please double check on the spelling of the method whenever you are using this two methods as stamping barcode on multiple pages will have additional 's' in the spelling that shows plurality