Add barcodes to PDF documents in .NET 2

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:

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

 

2 thoughts on “Add barcodes to PDF documents in .NET

  1. Reply David Dec 4,2015 5:15 pm

    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

    • Reply xfinium.pdf Dec 7,2015 10:43 am

      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.

Leave a Reply