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
COOP 25
Matrix 25
MSI/Plessey
Code 32
Pharma barcodes
Pharmacode
PZN
EAN/UPC barcodes
EAN 128
EAN 13
EAN 8
ISBN
ISMN
ISSN
JAN 13
UPC-A
UPC-E
Post and transportation barcodes
Fedex Ground 96
IATA 25
Identcode
Leitcode
KIX
Planet
PostNet
RM4SCC
SCC-14
Singapore Post
SSCC-18
USPS FIM
USPS Horizontal
USPS PIC

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:

PdfCode39Barcode code39Barcode = new PdfCode39Barcode();
code39Barcode.Data = "0123456789";
page.Graphics.DrawBarcode(code39Barcode, x, y);
Dim code39Barcode As New PdfCode39Barcode()
code39Barcode.Data = "0123456789"
page.Graphics.DrawBarcode(code39Barcode, x, y)

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.

PdfFixedDocument doc = new PdfFixedDocument();
PdfPage page = doc.Pages.Add();

PdfCode39Barcode code39Barcode = new PdfCode39Barcode();
code39Barcode.Data = "0123456789";
code39Barcode.BarcodeTextPosition = PdfBarcodeTextPosition.Bottom;
// Draw the barcode in the top left corner of the page.
page.Graphics.DrawBarcode(code39Barcode, 5, 5);

page.Graphics.SaveGraphicsState();
// Translate the coordinate system in the bottom left corner and rotate it with 90 degrees.
page.Graphics.TranslateTransform(0, page.Height);
page.Graphics.RotateTransform(90);
// Draw the barcode. On the page it will appear in the bottom left corner rotated with 90 degrees.
page.Graphics.DrawBarcode(code39Barcode, 5, 5);
page.Graphics.RestoreGraphicsState();

doc.Save("Code39Barcode.pdf");
Dim doc As New PdfFixedDocument()
Dim page As PdfPage = doc.Pages.Add()

Dim code39Barcode As New PdfCode39Barcode()
code39Barcode.Data = "0123456789"
code39Barcode.BarcodeTextPosition = PdfBarcodeTextPosition.Bottom
' Draw the barcode in the top left corner of the page.
page.Graphics.DrawBarcode(code39Barcode, 5, 5)

page.Graphics.SaveGraphicsState()
' Translate the coordinate system in the bottom left corner and rotate it with 90 degrees.
page.Graphics.TranslateTransform(0, page.Height)
page.Graphics.RotateTransform(90)
' Draw the barcode. On the page it will appear in the bottom left corner rotated with 90 degrees.
page.Graphics.DrawBarcode(code39Barcode, 5, 5)
page.Graphics.RestoreGraphicsState()

doc.Save("Code39Barcode.pdf")

2. Rotate the form XObject

PdfFixedDocument doc = new PdfFixedDocument();
PdfPage page = doc.Pages.Add();

PdfCode39Barcode code39Barcode = new PdfCode39Barcode();
code39Barcode.Data = "0123456789";
code39Barcode.BarcodeTextPosition = PdfBarcodeTextPosition.Bottom;
// Draw the barcode in the top left corner of the page.
page.Graphics.DrawBarcode(code39Barcode, 5, 5);

// Draw the barcode on the form XObject.
PdfFormXObject formXObject = 
    new PdfFormXObject(code39Barcode.Width, code39Barcode.Height);
formXObject.Graphics.DrawBarcode(code39Barcode, 0, 0);

// Draw the rotated form XObject on the page.
page.Graphics.DrawFormXObject(formXObject, 
    0, page.Height, formXObject.Width, formXObject.Height, 90);

doc.Save("Code39Barcode.pdf");
Dim doc As New PdfFixedDocument()
Dim page As PdfPage = doc.Pages.Add()

Dim code39Barcode As New PdfCode39Barcode()
code39Barcode.Data = "0123456789"
code39Barcode.BarcodeTextPosition = PdfBarcodeTextPosition.Bottom
' Draw the barcode in the top left corner of the page.
page.Graphics.DrawBarcode(code39Barcode, 5, 5)

' Draw the barcode on the form XObject.
Dim formXObject As New PdfFormXObject(code39Barcode.Width, code39Barcode.Height)
formXObject.Graphics.DrawBarcode(code39Barcode, 0, 0)

' Draw the rotated form XObject on the page.
page.Graphics.DrawFormXObject(formXObject, 
    0, page.Height, formXObject.Width, formXObject.Height, 90)

doc.Save("Code39Barcode.pdf")

 

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,
    David

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

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

  2. Hi,

    Is it possible to resize or change the size of the barcode?
    e.g. set the width and height?

    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).

  3. 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.

%d bloggers like this: