C# tutorial: generate Code-128 barcodes in vector PDF file.

Code 128 is a continuous, multilevel, high-density linear symbology that encodes text, numbers, numerous functions and the entire 128 ASCII character set (from ASCII 0 to ASCII 128.)

Bar codes made with XsPDF library can be part of any PDF file. We encourage the use of our barcode library to make PDF files with bar codes in them. The Code128 generator supports the whole code alphabet, including checksum generation.

In this example we generate Code-128 barcode in a PDF document using C# .NET. XsPDF provides suppport for Barcode128, Barcode39, UPC/EAN, QRCode, DataMatrix, PDF-417 and Aztec Code barcodes. With the help of the following sample code, it is very easy to create an Image from the barcode and then position the image anywhere on the PDF page.


// Create a new PDF document.
PdfDocument document = new PdfDocument();

// Create a page.
PdfPage page = document.AddPage();

// Get graphics object from pdf page
XGraphics g = XGraphics.FromPdfPage(page);

// Create a pdf barcode object
PdfBarcode barcode = new PdfBarcode();

// Set the barcode type to code 128 symbol
barcode.BarType = BarCodeType.Code128;

// Input the barcode data content
barcode.Data = "123456789";

// Change foreground and background color of barcode
barcode.BarcodeColor = XColors.Black;
barcode.BackgroundColor = XColors.White;

// Set the barcode Position and location
barcode.Location = new XPoint(100, 100);

// Set the barcode width and height
barcode.Size = new XSize(200, 100);

// Only 1D barcodes support show the data text under the barcode graphics
barcode.ShowText = true;
            
barcode.DrawBarcode(g);
                        
// Save and show the document
document.Save("Barcode.pdf");
Process.Start("Barcode.pdf");

More PDF tutorial