Guide for C#
Core Document Formats
Additional Features
Code 128

C# Imaging - C# Code 128 Generation Guide

Generate Code 128 Using C#.NET Barcode Image Creator DLL

Visual C#
Home > .NET Imaging SDK > C# > Generate Code 128 Barcode
If this is your first time to use our DocImageSDK, we strongly suggest you reading How to Start first!

C# Code 128 Barcode Overview
RasterEdge is a professional Code 128 barcode generating and reading SDK provider. With our SDK, developers will have ability to write 1D and 2D barcodes on image and document with accurate code at high speed. All the generated Code 128 barcodes are compatible with ISO / IEC+Code128+15417-2007 Code 128 barcode symbology specification and are suitable for all printers.
  • Code 128 A, Code 128 B, Code 128 C and auto mode supported for C# barcode generating
  • Include advanced algorithm to calculate and add checksum digit
  • Automatically add minimum left and right margins that go with specification
  • Optional to hide or display data text under Code 128 barcode images in C#
  • Easy to adjust Code 128 size in C# through BarcodeWidth, BarcodeHeight, X (bar width), Y (bar height)
  • Provide C# code to create single or multiple Code 128 barcodes on png, jpeg, gif, tiff and bmp image formats
  • Customize resolution to fit for all printers for high-quality Code 128 image output
This C#.NET Code 128 barcode generating tutorial is divided into three sections with respective example:
  • Draw Code 128 barcode on png, jpeg, jbig2, gif, tiff, bmp images, etc
  • Create Code 128 on PDF, multi-page TIFF, Word, Excel and PowerPoint documents
  • Code 128 property table and C# example codings
This tutorial focuses on how to generate Code 128 barcode images using C# class code in .NET Class Library application. In addition, here are more tutorials for Barcode Creator in VB.NET, Barcode Creator in ASP.NET, and Barcode Creator in .NET WinForms.
Draw Code 128 Barcode on Png, Jpeg, Jbig2, Gif, Tiff & Bmp in C#
Users can easily create single or multiple Code 128 image(s) independently, or you can draw, print Code 128 barcodes on various image files including popular formats like JPG, JBIG2, BMP, PNG, TIFF and GIF.
Users only need to load your local image and draw Code 128 barcode on the image (specify location and Code 128 setting), then the barcode is printed on the picture! Copy the C# sample codes below to your imaging program for a test now! If you have any questions concerning the barcode properties, refer to the table below or contact the customer support service!
REImage reImage = REFile.OpenImageFile("c:/Sample.png", new PNGDecoder());

Linear barcode = new Linear();// Create a barcode

barcode.Type = RasterEdge.Imaging.Barcode.Creator.BarcodeType.CODE128;// Select barcode type

barcode.Data = "123456789";// Set barcode data
barcode.X = 1.0F;// Set x
barcode.Y = 60.0F;// Set y
barcode.Resolution = 96;// Set resolution
barcode.Rotate = Rotate.Rotate0;// Set rotate

barcode.DrawBarcode(reImage, 300, 450);// Draw barcode on REImage with location x and y

REFile.SaveImageFile(reImage, "c:/code128.png", new PNGEncoder());
Create Code 128 on PDF, Multi-Page TIFF, Word, Excel and PPT
Demonstrated here is the Visual C# 2005 sample code to create Code 128 barcode images on these documents: PDF, multi-page TIFF, Word, Excel and PowerPoint. Developers are allowed to change the local document to be embedded, the barcode location on the specific page and also the Code 128 parameters like resolution, sizing, check digit, etc.

Create Code 128 on Defined PDF Document Area

REDocument doc = REFile.OpenDocumentFile("c:/Sample.pdf", new PDFDecoder());

REPage rePage = (REPage)doc.GetPage(0);

Linear barcode = new Linear();// Create a barcode

barcode.Type = BarcodeType.CODE128;// Select barcode type

barcode.Data = "123456789";// Set barcode data
barcode.X = 1.0F;// Set x
barcode.Y = 60.0F;// Set y
barcode.Resolution = 96;// Set resolution
barcode.Rotate = Rotate.Rotate0;// Set rotation

REImage reBarcode = barcode.ToImage();

EmbeddedImageAnnotation obj = new EmbeddedImageAnnotation();

obj.X = 10.0F;
obj.Y = 25.0F;
obj.Width = reBarcode.GetWidth();
obj.Height = reBarcode.GetHeight();

obj.FillImage = reBarcode.ToBitmap(reBarcode.GetWidth(), reBarcode.GetHeight());

RasterEdge.Imaging.Drawing.REItemEx item = obj.CreateAnnotationItem(rePage);
rePage.AddFloatingItem(item);

rePage.MergeItemsToPage();

REFile.SaveDocumentFile(doc, "c:/code128.pdf", new PDFEncoder());

Create Code 128 on Certain TIFF Document Page

REDocument doc = REFile.OpenDocumentFile("c:/Sample.tif", new TIFDecoder());

REPage rePage = (REPage)doc.GetPage(0);

Linear barcode = new Linear();// Create a barcode

barcode.Type = BarcodeType.CODE128;// Select barcode type

barcode.Data = "123456789";// Set barcode data
barcode.X = 1.0F;// Set x
barcode.Y = 60.0F;// Set y
barcode.Resolution = 96;// Set resolution
barcode.Rotate = Rotate.Rotate0;// Set rotation

REImage reBarcode = barcode.ToImage();

EmbeddedImageAnnotation obj = new EmbeddedImageAnnotation();

obj.X = 10.0F;
obj.Y = 25.0F;
obj.Width = reBarcode.GetWidth();
obj.Height = reBarcode.GetHeight();

obj.FillImage = reBarcode.ToBitmap(reBarcode.GetWidth(), reBarcode.GetHeight());

RasterEdge.Imaging.Drawing.REItemEx item = obj.CreateAnnotationItem(rePage);
rePage.AddFloatingItem(item);

rePage.MergeItemsToPage();

REFile.SaveDocumentFile(doc, "c:/code128.tif", new TIFEncoder());

Create Code 128 on Microsoft Office Word Document

REDocument doc = REFile.OpenDocumentFile("c:/Sample.docx", new DOCXDecoder());

REPage rePage = (REPage)doc.GetPage(0);

Linear barcode = new Linear();// Create a barcode

barcode.Type = BarcodeType.CODE128;// Select barcode type

barcode.Data = "123456789";// Set barcode data
barcode.X = 1.0F;// Set x
barcode.Y = 60.0F;// Set y
barcode.Resolution = 96;// Set resolution
barcode.Rotate = Rotate.Rotate0;// Set rotation

REImage reBarcode = barcode.ToImage();

EmbeddedImageAnnotation obj = new EmbeddedImageAnnotation();

obj.X = 10.0F;
obj.Y = 25.0F;
obj.Width = reBarcode.GetWidth();
obj.Height = reBarcode.GetHeight();

obj.FillImage = reBarcode.ToBitmap(reBarcode.GetWidth(), reBarcode.GetHeight());

RasterEdge.Imaging.Drawing.REItemEx item = obj.CreateAnnotationItem(rePage);
rePage.AddFloatingItem(item);

rePage.MergeItemsToPage();

REFile.SaveDocumentFile(doc, "c:/code128.docx", new DOCXEncoder());

Create Code 128 on Microsoft Excel Document

/// <summary>
/// Create Code 128 barcode on Excel document.
/// </summary>
/// <param name="filePath"></param>
/// <param name="barcodeData"></param>
public void CreateCode128OnExcelDocument(String filePath, String barcodeData, float positionX, float positionY)
{
ExcelDocument doc = new ExcelDocument(filePath);

// Create linear Code 128 barcode.
RasterEdge.Imaging.Barcode.Creator.Linear barcode = new Linear();
barcode.Type = BarcodeType.CODE128;
barcode.Data = "123456789";// Set Code 128 barcode data
barcode.X = 5.0F;

barcode.Resolution = 72;
barcode.Rotate = Rotate.Rotate0;

REImage reBarcode = barcode.ToImage();

float h = reBarcode.GetResolutionHori();
float v = reBarcode.GetResolutionVert();
reBarcode.UpdateResolution(96F, 96F);

for (int i = 0; i < doc.GetPageCount(); i++)
{
((ExcelPage)(doc.GetPage(i))).AddBarcode(barcode, positionX, positionY);
}

doc.Save(@"C:\code128.xlsx");
}

Create Code 128 on Microsoft PowerPoint Document

/// <summary>
/// Create Code 128 barcode on PowerPoint document.
/// </summary>
/// <param name="filePath"></param>
/// <param name="barcodeData"></param>
public void CreateCode128OnPPTXDocument(String filePath, String barcodeData, float positionX, float positionY)
{
PPTXDocument doc = new PPTXDocument(filePath);

// Create 1d Code 128 barcode.
RasterEdge.Imaging.Barcode.Creator.Linear barcode = new Linear();
barcode.Type = BarcodeType.CODE128;

barcode.Data = "123456789";// Set Code 128 barcode data.
barcode.X = 5.0F;
barcode.Resolution = 96;

REImage reBarcode = barcode.ToImage();

// Update the Code 128 barcode resolution
reBarcode.UpdateResolution(96F, 96F);

for (int i = 0; i < doc.GetPageCount(); i++)
{
// Add Code 128 barcode at position (100, 100) and the default unit is pixel.
((PPTXPage)(doc.GetPage(i))).AddImage(reBarcode, 100, 100);
}

doc.Save(@"C:\code128.pptx");
}
Code 128 Property Table and C# Codes
With the help of following table, developers can set Code 128 parameters with a few steps.
CODE 128 SETTING+
CODE 128 SETTING-
Type CODE128
Data Format
UOM(Unit of Measure)
Rotate
BarAlignment Resolution(DPI)
AutoResize AddCheckSum
X(Bar Width) ShowText
Y(Bar Height) TextFontSize
BarcodeWidth TextMargin
BarcodeHeight TextFontName
LeftMargin TextColor
RightMargin TextFontStyle
TopMargin BackColor
BottomMargin ForeColor
ProcessTilde
  • Resolution: The image resolution for Code 128 image. Default value is 72 DPI for screen display. If you want to output the barcode image with printer, please adjust this property according to your printer resolution.
  • ShowCheckSumChar: Code 128 is self-checking linear bar code symbol, but an obligatory checksum character is required by the ISO / IEC specification as well. It is automatically encoded into the image, and you can set this value to false to hide it.
  • ShowText: This is a Boolean value. You can set this value to false to hide the human readable data text under the generated Code 128 bar code image.
Draw Code 128 on Image & Document in C# Code
Generate Code 128 on image and document with mature C#.NET example code. All the properties above are included in the sample code here. Please make any change to these properties if necessary.
REImage reImage = REFile.OpenImageFile("c:/Sample.png", new PNGDecoder());

Linear barcode = new Linear();// Create a barcode

barcode.Type = BarcodeType.CODE128;// Select barcode type

barcode.Data = "123456789";// Set barcode data

//Code128 barcode size related
barcode.X = 1.0F;// Set x
barcode.Y = 60.0F;// Set y
barcode.BarcodeHeight = 60;
barcode.BarcodeWidth = 0;
barcode.BottomMargin = 0;
barcode.LeftMargin = 0;
barcode.RightMargin = 0;
barcode.BarAlignment = AlignmentHori.Left;
barcode.Resolution = 96;// Set resolution
barcode.Rotate = Rotate.Rotate0;// Set rotation
barcode.ShowCheckSumChar = false;
barcode.UOM = RasterEdge.Imaging.Barcode.Creator.UnitOfMeasure.PIXEL;

// Code128 colors
barcode.BackColor = System.Drawing.Color.White;
barcode.ForeColor = System.Drawing.Color.Black;

// Code128 font style
barcode.TextFont = new Font("Arial", 9f, FontStyle.Regular);
barcode.TextColor = System.Drawing.Color.Black;
barcode.TextMargin = 6;
barcode.ShowText = true;

// Code128 Special
barcode.ProcessTilde = false;

barcode.DrawBarcode(reImage, 300, 450);// Draw barcode on REImage with location x and y

REFile.SaveImageFile(reImage, "c:/code128create.png", new PNGEncoder());


Recommend this to Google+