Add barcodes to PDF documents in .NET

With the release of XFINIUM.PDF 3.5 we added support for QR barcodes. At this moment the XFINIUM.PDF library supports 40 1D barcodes and 6 2D barcodes.

We support the following barcode symbologies:

1D 2D
Generic barcodes
Codabar QR
Code 11 DataMatrix
Code 25 Codablock
Code 25 Interleaved Code 16K
Code 39 PDF417
Code 39 Extended Micro PDF417
Code 93
Code 93 Extended
Code 128 A/B/C
Matrix 25
Code 32
Pharma barcodes
EAN/UPC barcodes
EAN 128
EAN 13
JAN 13
Post and transportation barcodes
Fedex Ground 96
Singapore Post
USPS Horizontal

A barcode can be added to a page using a few simple steps: create the desired barcode object, set its Data property and then draw the barcode on the page.

The code below shows how to draw a Code 39 barcode on a PDF page:

Several properties can be set to customize the barcode appearance:

  • BarColor property sets the color of barcode bars
  • BackgroundColor property sets the color of the barcode background
  • BarcodeTextPosition property sets the position where the barcode text is displayed. This property is not available for 2D barcodes because they are designed to encode large quantities of data and displaying this data along with the barcode would have no meaning.

Barcodes are displayed on horizontal but they can be rotated with any angle. Drawing a rotated barcode can be done 2 ways: 1. the coordinate system is rotated and then the barcode is drawn or 2. the barcode is drawn on a form XObject and then the form XObject is drawn rotated on the page. Both ways are shown below:

1. Rotate the coordinate system.

2. Rotate the form XObject


8 thoughts on “Add barcodes to PDF documents in .NET”

  1. Hi, Thank you for the sample code.

    For function page.Graphics.DrawFormXObject, could it support Matrix? For example:

    PDFMatrix pdfMatrix = new PDFMatrix();


    page.Graphics.DrawFormXObject(formXObject, pdfMatrix);

    or page.Graphics.DrawFormXObject(formXObject, pdfMatrix.M11, pdfMatrix.M12,
    pdfMatrix.M21, pdfMatrix.M22,
    pdfMatrix.TX, pdfMatrix.TY);

    Or can you give me an example to decompose a PDFMatrix for function DrawFormXObject?

    Thanks a lot,

    1. You can try the following:
      page.Graphics.MultiplyTransform(pdfMatrix.M11, pdfMatrix.M12, pdfMatrix.M21, pdfMatrix.M22, pdfMatrix.TX, pdfMatrix.TY);
      page.Graphics.DrawFormXObject(formXObject, x, y, width, height);

      If the code above does not give you the expected results let us know and we’ll find a solution.

    1. The barcode size depends on the encoded data and the XDimension and YDimension properties. The size varies linearly with XDimension/YDimension.
      You have a few options: 1. set the XDimension and YDimension properties to values that will result in your desired size (Width and Height properties give you the barcode size). 2. Generate a barcode at some size and then apply a scale transform to get the desired size. 3. Create a form XObject that matches the barcode size, draw the barcode on it and then draw the form XObject at the desired barcode size (similar scaling with #2).

  2. Hi,

    Just wanted to ask about PDF417, what’s the best way to calculate the columns and rows?
    Thank you.

    1. The rows and columns depend on your size constraints. You can set either Rows or Columns to zero and the library will compute the required rows or columns to accommodate your data.

      1. Thanks for the reply, so can i just do rows = 0 and don’t set the value for column?

      2. No, you have to set at least one and the library will compute the other. Set Columns and leave Rows zero and the library will compute automatically the required Rows to encode the barcode.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.