How to insert or stamp high quality barcodes into existing PDF files using iTextSharp

Neodynamic

Well-known member
Joined
Dec 5, 2005
Messages
137
Programming Experience
10+
How to insert or stamp high quality barcodes into existing PDF files using iTextSharp and C# or VB.NET

Prerequisites

Suppose you have been required to take a PDF document and insert or stamp a barcode into it. In the .NET world, there’re plenty of PDF-related products or solutions for managing such document format. One of the most popular solutions out there is <a href="http://sourceforge.net/projects/itextsharp/">iTextSharp</a> (ak.a. iText#), which is a port of the iText open source java library. iTextSharp is written in C# and allows you PDF generation as well as PDF manipulation like splitting, merging, etc.; all inside the .NET platform.

iTextSharp does offer some barcode generation capabilities out-of-the-box which can be used without troubles in your project. However, there’re situations or clients/apps requirements that the barcode features built-in iTextSharp might not be sufficient to fill them. In these cases, a specialized software component like Neodynamic Barcode Professional SDK for .NET is the right tool you need.

In this article, we’ll show you how to use Barcode Professional SDK for .NET along iTextSharp for inserting or stamping an EAN/UPC Composite barcode, a 2D Micro PDF417 barcode, and a linear GS1 DataBar (formerly RSS) barcode which are not part of iTextSharp library into an existing PDF document (NOTE: you could replace the built-in iTextSharp barcode encoders with the one offered by Barcode Professional SDK if needed. For example, Barcode Professional provides features like human readable text justifying, Bar Width/Module Adjustment, etc. which are not found on the iTextSharp library)

The barcodes generated by Barcode Professional SDK will be at high quality resolution (300 dpi although you can change the code below to match any other dpi value) to get a more convenience output printing quality.

This is the PDF document before and after inserting or stamping the barcodes:




To reproduce the code, please follow up these steps:
  • Download and install Barcode Professional SDK 3.0 for .NET (or greater)
  • Download iTextSharp dll from iTextSharp | Download iTextSharp software for free at SourceForge.net
  • Open Visual Studio and create a new project, for instance, a Console App project
  • In the created project, add a reference to itextsharp.dll and Neodynamic.SDK.Barcode.dll assemblies
  • Download the sample PDF document called SampleDoc.pdf to C:\temp folder
  • Copy/paste the following code in your project:
    NOTE: The barcodes in the code below are being generated at 300dpi so please change it per your needs.

    Visual Basic .NET
    Dim pdfFile As String = "C:\Temp\SampleDoc.pdf"
    Dim pdfFileOut As String = "C:\Temp\SampleDoc_barcode.pdf"
    Dim dpi As Integer = 300

    '1. Open the PDF file using a PdfReader
    Dim pdfReader As New iTextSharp.text.pdf.PdfReader(pdfFile)
    '2. Create a PdfStamper object for inserting or stamping the barcodes...
    Dim pdfStamper As New iTextSharp.text.pdf.PdfStamper(pdfReader, new System.IO.FileStream(pdfFileOut, System.IO.FileMode.Create, System.IO.FileAccess.Write))
    '3. Get the page content from the PDF file by creating a PdfContentByte object
    Dim pdfPage As iTextSharp.text.pdf.PdfContentByte = pdfStamper.GetOverContent(1)
    Dim pageWidth As Single = pdfReader.GetPageSize(1).Width
    Dim pageHeight As Single = pdfReader.GetPageSize(1).Height
    '4. Generate the barcode images using Barcode Professional SDK

    '4.1. Generate the UPC-A CCB composite barcode.
    Using bc1 As New Neodynamic.SDK.Barcode.BarcodeProfessional()
    'set the desired barcode symbology e.g. UPC-A CCB
    bc1.Symbology = Neodynamic.SDK.Barcode.Symbology.UpcACCB
    'set the value to encode e.g. Primary Data = 01234567890 and Secondary Data = 991234-abcd
    bc1.Code = "01234567890|991234-abcd"
    'set the barcode unit and sizes...
    bc1.BarcodeUnit = Neodynamic.SDK.Barcode.BarcodeUnit.Inch
    bc1.BarWidth = 0.013 'the narrow bar width a.k.a. X value
    bc1.BarHeight = 1 'the height of the barcode bars
    bc1.GuardBarHeight = bc1.BarHeight + 5 * bc1.BarWidth 'the height of the guard bars only available for EAN/UPC barcodes
    bc1.QuietZoneWidth = 9 * bc1.BarWidth 'the quiet zone for UPC-A is 9 times the BarWidth value per GS1 spec

    'Generate the barcode image content for inserting it into the PDF page
    'For high quality, you can generated the barcode at, for instance, 300 dpi
    Dim barcodeImage1 As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(bc1.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png, dpi))
    'set the position of the barcode image. PDF uses Points as unit (1 point = 1/72 inch)
    'the starting point (x=0, y=0) in the PDF coord is the bottom-left corner!!!
    barcodeImage1.SetAbsolutePosition(pageWidth - 4.25F * 72F + ((3.25F - (barcodeImage1.Width / dpi))/2) * 72F , pageHeight - ((barcodeImage1.Height / dpi) * 72F) - (2.5F * 72F))
    'scale the image based on the image dpi
    barcodeImage1.ScalePercent(72F / CSng(dpi) * 100F)
    'add the image object to the pdf page
    pdfPage.AddImage(barcodeImage1)
    End Using

    '4.2. Generate the 2D Micro PDF417 barcode.
    Using bc2 As New Neodynamic.SDK.Barcode.BarcodeProfessional()
    'set the desired barcode symbology e.g. Micro PDF417
    bc2.Symbology = Neodynamic.SDK.Barcode.Symbology.MicroPdf417
    'set the value to encode e.g. ABCDE-1234567890-PDF
    bc2.Code = "ABCDE-1234567890-PDF"
    'set the barcode unit and sizes...
    bc2.BarcodeUnit = Neodynamic.SDK.Barcode.BarcodeUnit.Inch
    bc2.BarWidth = 0.01 'the narrow bar width a.k.a. X value
    bc2.BarRatio = 3 'in MicroPdf417 each bars' heigh in a row is calculated as BarWidth * BarRatio

    'Generate the barcode image content for inserting it into the PDF page
    'For high quality, you can generated the barcode at, for instance, 300 dpi
    Dim barcodeImage2 As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(bc2.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png, dpi))
    'set the position of the barcode image. PDF uses Points as unit (1 point = 1/72 inch)
    'the starting point (x=0, y=0) in the PDF coord is the bottom-left corner!!!
    barcodeImage2.SetAbsolutePosition(72F + ((3.25F - (barcodeImage2.Width / dpi)) / 2) * 72F, pageHeight - ((barcodeImage2.Height / dpi) * 72F) - (6F * 72F));
    'scale the image based on the image dpi
    barcodeImage2.ScalePercent(72F / CSng(dpi) * 100F)
    'add the image object to the pdf page
    pdfPage.AddImage(barcodeImage2)
    End Using

    '4.3. Generate the GS1 DataBar-14 Stacked barcode.
    Using bc3 As New Neodynamic.SDK.Barcode.BarcodeProfessional()
    'set the desired barcode symbology e.g. DataBar-14 stacked
    bc3.Symbology = Neodynamic.SDK.Barcode.Symbology.GS1DataBar14Stacked
    'set the value to encode e.g. 0061414199999
    bc3.Code = "0061414199999"
    'set the barcode unit and sizes...
    bc3.BarcodeUnit = Neodynamic.SDK.Barcode.BarcodeUnit.Inch
    bc3.BarWidth = 0.0208 'the narrow bar width a.k.a. X value

    'Generate the barcode image content for inserting it into the PDF page
    'For high quality, you can generated the barcode at, for instance, 300 dpi
    Dim barcodeImage3 As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(bc3.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png, dpi))
    'set the position of the barcode image. PDF uses Points as unit (1 point = 1/72 inch)
    'the starting point (x=0, y=0) in the PDF coord is the bottom-left corner!!!
    barcodeImage3.SetAbsolutePosition(pageWidth - 4.25F * 72F + ((3.25F - (barcodeImage3.Width / dpi)) / 2) * 72F, pageHeight - ((barcodeImage3.Height / dpi) * 72F) - (6f * 72F));
    'scale the image based on the image dpi
    barcodeImage3.ScalePercent(72F / CSng(dpi) * 100F)
    'add the image object to the pdf page
    pdfPage.AddImage(barcodeImage3)
    End Using

    'close PdfStamper
    pdfStamper.Close()
    'close PdfReader
    pdfReader.Close()


    C#
    string pdfFile = @"C:\Temp\SampleDoc.pdf";
    string pdfFileOut = @"C:\Temp\SampleDoc_barcode.pdf";
    int dpi = 300;

    //1. Open the PDF file using a PdfReader
    iTextSharp.text.pdf.PdfReader pdfReader = new iTextSharp.text.pdf.PdfReader(pdfFile);
    //2. Create a PdfStamper object for inserting or stamping the barcodes...
    iTextSharp.text.pdf.PdfStamper pdfStamper = new iTextSharp.text.pdf.PdfStamper(pdfReader, new System.IO.FileStream(pdfFileOut, System.IO.FileMode.Create, System.IO.FileAccess.Write));
    //3. Get the page content from the PDF file by creating a PdfContentByte object
    iTextSharp.text.pdf.PdfContentByte pdfPage = pdfStamper.GetOverContent(1);
    float pageWidth = pdfReader.GetPageSize(1).Width;
    float pageHeight = pdfReader.GetPageSize(1).Height;
    //4. Generate the barcode images using Barcode Professional SDK

    //4.1. Generate the UPC-A CCB composite barcode.
    using (Neodynamic.SDK.Barcode.BarcodeProfessional bc1 = new Neodynamic.SDK.Barcode.BarcodeProfessional())
    {
    //set the desired barcode symbology e.g. UPC-A CCB
    bc1.Symbology = Neodynamic.SDK.Barcode.Symbology.UpcACCB;
    //set the value to encode e.g. Primary Data = 01234567890 and Secondary Data = 991234-abcd
    bc1.Code = "01234567890|991234-abcd";
    //set the barcode unit and sizes...
    bc1.BarcodeUnit = Neodynamic.SDK.Barcode.BarcodeUnit.Inch;
    bc1.BarWidth = 0.013; //the narrow bar width a.k.a. X value
    bc1.BarHeight = 1; //the height of the barcode bars
    bc1.GuardBarHeight = bc1.BarHeight + 5 * bc1.BarWidth; //the height of the guard bars only available for EAN/UPC barcodes
    bc1.QuietZoneWidth = 9 * bc1.BarWidth; //the quiet zone for UPC-A is 9 times the BarWidth value per GS1 spec

    //Generate the barcode image content for inserting it into the PDF page
    //For high quality, you can generated the barcode at, for instance, 300 dpi
    iTextSharp.text.Image barcodeImage1 = iTextSharp.text.Image.GetInstance(bc1.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png, dpi));
    //set the position of the barcode image. PDF uses Points as unit (1 point = 1/72 inch)
    //the starting point (x=0, y=0) in the PDF coord is the bottom-left corner!!!
    barcodeImage1.SetAbsolutePosition(pageWidth - 4.25f * 72f + ((3.25f - (barcodeImage1.Width / dpi))/2) * 72f , pageHeight - ((barcodeImage1.Height / dpi) * 72f) - (2.5f * 72f));
    //scale the image based on the image dpi
    barcodeImage1.ScalePercent(72f / (float)dpi * 100f);
    //add the image object to the pdf page
    pdfPage.AddImage(barcodeImage1);
    }

    //4.2. Generate the 2D Micro PDF417 barcode.
    using (Neodynamic.SDK.Barcode.BarcodeProfessional bc2 = new Neodynamic.SDK.Barcode.BarcodeProfessional())
    {
    //set the desired barcode symbology e.g. Micro PDF417
    bc2.Symbology = Neodynamic.SDK.Barcode.Symbology.MicroPdf417;
    //set the value to encode e.g. ABCDE-1234567890-PDF
    bc2.Code = "ABCDE-1234567890-PDF";
    //set the barcode unit and sizes...
    bc2.BarcodeUnit = Neodynamic.SDK.Barcode.BarcodeUnit.Inch;
    bc2.BarWidth = 0.01; //the narrow bar width a.k.a. X value
    bc2.BarRatio = 3; //in MicroPdf417 each bars' heigh in a row is calculated as BarWidth * BarRatio

    //Generate the barcode image content for inserting it into the PDF page
    //For high quality, you can generated the barcode at, for instance, 300 dpi
    iTextSharp.text.Image barcodeImage2 = iTextSharp.text.Image.GetInstance(bc2.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png, dpi));
    //set the position of the barcode image. PDF uses Points as unit (1 point = 1/72 inch)
    //the starting point (x=0, y=0) in the PDF coord is the bottom-left corner!!!
    barcodeImage2.SetAbsolutePosition(72f + ((3.25f - (barcodeImage2.Width / dpi)) / 2) * 72f, pageHeight - ((barcodeImage2.Height / dpi) * 72f) - (6f * 72f));
    //scale the image based on the image dpi
    barcodeImage2.ScalePercent(72f / (float)dpi * 100f);
    //add the image object to the pdf page
    pdfPage.AddImage(barcodeImage2);
    }

    //4.3. Generate the GS1 DataBar-14 Stacked barcode.
    using (Neodynamic.SDK.Barcode.BarcodeProfessional bc3 = new Neodynamic.SDK.Barcode.BarcodeProfessional())
    {
    //set the desired barcode symbology e.g. DataBar-14 stacked
    bc3.Symbology = Neodynamic.SDK.Barcode.Symbology.GS1DataBar14Stacked;
    //set the value to encode e.g. 0061414199999
    bc3.Code = "0061414199999";
    //set the barcode unit and sizes...
    bc3.BarcodeUnit = Neodynamic.SDK.Barcode.BarcodeUnit.Inch;
    bc3.BarWidth = 0.0208; //the narrow bar width a.k.a. X value

    //Generate the barcode image content for inserting it into the PDF page
    //For high quality, you can generated the barcode at, for instance, 300 dpi
    iTextSharp.text.Image barcodeImage3 = iTextSharp.text.Image.GetInstance(bc3.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png, dpi));
    //set the position of the barcode image. PDF uses Points as unit (1 point = 1/72 inch)
    //the starting point (x=0, y=0) in the PDF coord is the bottom-left corner!!!
    barcodeImage3.SetAbsolutePosition(pageWidth - 4.25f * 72f + ((3.25f - (barcodeImage3.Width / dpi)) / 2) * 72f, pageHeight - ((barcodeImage3.Height / dpi) * 72f) - (6f * 72f));
    //scale the image based on the image dpi
    barcodeImage3.ScalePercent(72f / (float)dpi * 100f);
    //add the image object to the pdf page
    pdfPage.AddImage(barcodeImage3);
    }

    //close PdfStamper
    pdfStamper.Close();
    //close PdfReader
    pdfReader.Close();


Remember to download and install Barcode Professional SDK for .NET in order to reproduce this sample demo.


Links:
This Demos
Demos
Download Barcode Professional SDK for .NET
More Information about Neodynamic Barcode Professional SDK for .NET


Neodynamic
.NET Components & Controls
Neodynamic
 
Top Bottom