Guide for C#
Core Document Formats
Additional Features
Code 39

C# Imaging - C# Code 39 Barcoding Tutorial

Draw Code 39 Barcode on Raster Images, PDF, Word, Excel and PowerPoint

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

C# Imaging Code 39 Barcode Add-on Overview
RasterEdge DocImage SDK for .NET includes this RasterEdge.Imaging.Barcode.Creator.dll for developers to generate and create Code 39 on PDF, Word, Excel and PowerPoint documents and raster image files using C# code. With the sample codes demonstrated below, users can freely create and print Code 39 bar codes on any page within PDF, Excel, PowerPoint or Word file, and at any location on a Jpeg, Gif, Png, Tiff or Bmp image. And below are main features overview.
  • Create Code 39, Code 39 Extension encoding alphanumeric data in .NET
  • C# Code 39 component DLL designed for Visual Studio 2005 or above
  • Resize Code 39 linear bar code image by controlling barcode width, barcode height
  • Include algorithm to automatically add Code 39 check digit character / checksum
  • Adjust Code 39 image resolution for high-quality output suitable for all printers
This tutorial will show you the following Visual C# examples on how to create Code 39 images in .NET:
  • How to draw Code 39 barcode on image
  • How to generate Code 39 on certain document page
  • Code 39 dimension settings and C# sample codes
  • Code 39 (barcode 3 of 9) FAQ
Written in C# managed code, this barcode writing library is compatible with multiple development environments such as VB.NET class library (Generating Code 39 in VB.NET)) , ASP.NET (Generating Barcode in ASP.NET aspx page) & .NET WinForms (Generating Barcode in .NET Windows Forms).) .
Draw Code 39 on Raster Image in C# Project
With the following Visual C# demo code, you are able to generate Code 39 on raster image files, including BMP, PNG, GIF, JPEG and TIFF formats. Below are some basic settings for Code 39. For more dimensions, please refer to the complete property table below. You will be able to adjust barcode location on image, and control Code 39 size, resolution, checksum, orientation, color, and text, etc.
REImage reImage = REFile.OpenImageFile("c:/Sample.png", new PNGDecoder());

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

barcode.Type = BarcodeType.CODE39;//select barcode type

barcode.Data = "0123456ABCD";//set barcode data
barcode.X = 1.0F;//set bar width
barcode.Y = 60.0F;//set bar height
barcode.Resolution = 96;//set resolution
barcode.Rotate = Rotate.Rotate0;//set rotate

barcode.DrawBarcode(reImage, 0, 0);//draw barcode on REImage with location x and y

REFile.SaveImageFile(reImage, "c:/code39.png", new PNGEncoder());
Generate Code 39 on Certain Document Page
This section will demonstrate how to generate & embed Code 39 on PDF, multi-page TIFF, Microsoft Office Word document (.docx), Excel sheet (.xlsx) and PowerPoint (.pptx). Users can print it at any location within on any page from the document, and adjust Code 39 parameters freely as well.

Write C# Code 39 on Defined Area in PDF Document

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.CODE39;//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

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:/code39.pdf", new PDFEncoder());

Create C# Code 39 on Multi-Page TIFF Document

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.CODE39;//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

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:/code39.tif", new TIFEncoder());

Print C# Code 39 on 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.CODE39;//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

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:/code39.docx", new DOCXEncoder());

Write C# Code 39 Image on Excel Sheet

/// <summary>
/// Create a Code 39 barcode on every Excel sheet
/// </summary>
/// <param name="filePath"></param>
/// <param name="barcodeData"></param>
public void CreateCODE39OnExcelDocument(String filePath, String barcodeData, float positionX, float positionY)
{
ExcelDocument doc = new ExcelDocument(filePath);

RasterEdge.Imaging.Barcode.Creator.CODE39 barcode = new CODE39();//create a Code 39 barcode

barcode.Data = "123456789";//set 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:\Code39.xlsx");
}

Create C# Code 39 Barcode on PowerPoint Slide

/// <summary>
/// Create a Code 39 barcode on every PowerPoint slide
/// </summary>
/// <param name="filePath"></param>
/// <param name="barcodeData"></param>
public void CreateCODE39OnPPTXDocument(String filePath, String barcodeData, float positionX, float positionY)
{
PPTXDocument doc = new PPTXDocument(filePath);

RasterEdge.Imaging.Barcode.Creator.CODE39 barcode = new CODE39();//create a Code 39 barcode

barcode.Data = "123456789";//set 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++)
{
((PPTXPage)(doc.GetPage(i))).AddBarcode(barcode, positionX, positionY);
}

doc.Save(@"C:\Code39.pptx");
}
Code 39 Settings and C# Sample Codes
BCode 39 parameters listed below can ensure you to create a high-quality, scannable Code 39 with customized settings.
CODE 39 SETTING+
CODE 39 SETTING-
Type CODE39
Data Format
UOM(Unit of Measure)
Rotate
I N(2.0 - 3.0 inclusive)
ShowStarStopInText ProcessTilde
BarAlignment Resolution(DPI)
AutoResize AddCheckSum
X(Bar Width) ShowText
Y(Bar Height) TextFontSize
BarcodeWidth TextMargin
BarcodeHeight TextFontName
TextColor TextFontStyle
TopMargin BackColor
BottomMargin ForeColor
  • ShowStarStopInText: As specified by ISO/IEC 16388 standard, Code 39 encodes an obligatory start character and stop character, graphically represented in human readable text as "*", i.e. the star sign. You can set this Boolean value false to hide them in the text below barcode.
  • N: Code 39 barcode is composed of narrow bars and wide bars. N dimension regulates the ratio between widths of wide bar to narrow bar.
  • I: Each one of the Code 39 data character is represented by 9 bars in the symbol image. I dimension, the inter-character space, specifies the space between each of these 9-bar groups in image.
  • AddChecksum: Code 39 is a self-checking linear barcode. But you can choose to add checkum for better reading security.

Generate Code 39 with C# Code

RasterEdge offers free and accurate C# example code to create customized Code 39 on image and document files, by using the properties listed above.
REImage reImage = REFile.OpenImageFile("c:/Sample.png", new PNGDecoder());

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

barcode.Type = BarcodeType.CODE39;//select barcode type

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

//Code39 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 = 72;//set resolution
barcode.Rotate = Rotate.Rotate0;//set rotate
barcode.AddCheckSum = false;
barcode.UOM = RasterEdge.Imaging.Barcode.Creator.UnitOfMeasure.PIXEL;

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

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

//Code39 Special
barcode.I = 1.0f;
barcode.N = 2.0f;
barcode.ShowStartStopInText = true;
barcode.ProcessTilde = false;

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

REFile.SaveImageFile(reImage, "c:/code39create.png", new PNGEncoder());
Code 39 Barcode FAQ

You barcode DLL supports Code 39 and Code 39 Extension. Any difference?

A typical Code 39 can encode only: alphanumeric A to Z and 0 to 9, and several special characters. (You can view from the table.) But a Code 39 Extension can also encode lower-case letters from a to z, apart from those mentioned in the former. Thus it is advised to use Code 39 Extension if lower-case letter is involved for your data encoding.

Why are there star signs in my Code 39 text from the generated image?

The star signs are actually the start and stop characters compulsorily encoded in Code 39 as specified by corresponding standard. You can hide them by setting ShowStarStopInText to false.


Recommend this to Google+