Click here to Skip to main content
15,897,519 members
Articles / Programming Languages / C#
Article

Use bar code fonts in C#.net

Rate me:
Please Sign up or sign in to vote.
1.87/5 (11 votes)
29 Jul 2007CPOL 61K   2.9K   33   6
Use bar code fonts in C#.net
Screenshot - BarCode.jpg

Introduction

I am just trying to explain, how can use "ean13.tff" font in c#.net.
First you downlaod / copy (from your source folder) ean13.tff font.
Then install this font your font directory ( C:\Windows\fonts). Then try to run project or
WINFOnt.exe file .

Using the code

In my sample Code have two methods

1. EAN8(string chaine)

C#
public string EAN8(string chaine)

{

int i;

double checksum;

string CodeBarre = "";

checksum = 0;

if (chaine.Length == 7)

{

for (i = 1; i <= 7; i++)

{

int L1 = Convert.ToChar(chaine.Substring(i - 1, 1));

if ((L1 < 48) || (L1 > 57))

{

i = 0;

break;

}

}

if (i == 8)

{

for (i = 7; i > 0; i = i - 2)

{

checksum = checksum + Convert.ToInt32(chaine.Substring(i - 1, 1));

}

checksum = checksum * 3;

for (i = 6; i > 0; i = i - 2)

{

checksum = checksum + Convert.ToInt32(chaine.Substring(i - 1, 1));

}

chaine = chaine + (10 - checksum % 10) % 10;

CodeBarre = ":";

for (i = 1; i <= 4; i++)

{

CodeBarre = CodeBarre + Convert.ToChar(65 + Convert.ToInt32(chaine.Substring(i - 1, 1)));

}

CodeBarre = CodeBarre + "*";

for (i = 5; i <= 8; i++)

{

CodeBarre = CodeBarre + Convert.ToChar(97 + Convert.ToInt32(chaine.Substring(i - 1, 1)));

}

CodeBarre = CodeBarre + "+";

}

}

return CodeBarre;

}

EAN13(string chaine)

C#
public object EAN13(string chaine)

{

object functionReturnValue = null;

int i;

int checksum = 0;

int first;

string CodeBarre;

bool tableA;

functionReturnValue = "";

if (chaine.Length == 12)

{

for (i = 1; i <= 12; i++)

{

int L1 = Convert.ToChar(chaine.Substring(i - 1, 1));

if (L1 < 48 || L1 > 57)

{

i = 0;

break;

}

}

if (i == 13)

{

for (i = 12; i >= 1; i += -2)

{

checksum = checksum + Convert.ToInt32(chaine.Substring(i - 1, 1));

}

checksum = checksum * 3;

for (i = 11; i >= 1; i += -2)

{

checksum = checksum + Convert.ToInt32(chaine.Substring(i - 1, 1));

}

chaine = chaine + (10 - checksum % 10) % 10;

CodeBarre = chaine.Substring(0, 1) + Convert.ToChar(65 + Convert.ToInt32((chaine.Substring(1, 1))));

first = Convert.ToInt32(chaine.Substring(0, 1));

for (i = 3; i <= 7; i++)

{

tableA = false;

switch (i)

{

case 3:

switch (first)

{

case 0:

case 1:

case 2:

case 3:

tableA = true;

break;

}

break;

case 4:

switch (first)

{

case 0:

case 4:

case 7:

case 8:

tableA = true;

break;

}

break;

case 5:

switch (first)

{

case 0:

case 1:

case 4:

case 5:

case 9:

tableA = true;

break;

}

break;

case 6:

switch (first)

{

case 0:

case 2:

case 5:

case 6:

case 7:

tableA = true;

break;

}

break;

case 7:

switch (first)

{

case 0:

case 3:

case 6:

case 8:

case 9:

tableA = true;

break;

}

break;

}

if (tableA)

{

CodeBarre = CodeBarre + Convert.ToChar(65 + Convert.ToInt32(chaine.Substring(i - 1, 1)));

}

else

{

CodeBarre = CodeBarre + Convert.ToChar(75 + Convert.ToInt32(chaine.Substring(i - 1, 1)));

}

}

CodeBarre = CodeBarre + "*";

for (i = 8; i <= 13; i++)

{

CodeBarre = CodeBarre + Convert.ToChar(97 + Convert.ToInt32(chaine.Substring(i - 1, 1)));

}

CodeBarre = CodeBarre + "+";

functionReturnValue = CodeBarre;

}

}

return functionReturnValue;

}

Next Step is ' add on digit method' for add extra digit in barcode

AddOn(string chaine)

C#
public string AddOn(string chaine)

{

int i;

int checksum = 0;

string AddOnn = "";

bool tableA;

if (chaine.Length == 2 || chaine.Length == 5)

{

for (i = 1; i < chaine.Length; i++)

{

int L1 = Convert.ToChar(chaine.Substring(i - 1, 1));

if (L1 < 48 || L1 > 57)

{

break;

}

if (chaine.Length == 2)

{

checksum = 10 + Convert.ToInt32(chaine) % 4;

}

else if (chaine.Length == 5)

{

for (i = 1; i == 5; i = i - 2)

{

checksum = checksum + Convert.ToInt32(chaine.Substring(i - 1, 1));

}

checksum = (checksum * 3 + Convert.ToInt32(chaine.Substring(2, 1)) * 9 + Convert.ToInt32(chaine.Substring(4, 1)) * 9) % 10;

}

AddOnn = "[";

for (i = 1; i <= chaine.Length; i++)

{

tableA = false;

switch (i)

{

case 1:

int[] str = { 4, 9, 10, 11 };

for (int j = 0; j < str.Length; j++)

{

if (str[j] == checksum)

{

tableA = false;

break;

}

}

break;

case 2:

int[] str1 = { 1, 2, 3, 5, 6, 7, 10, 12 };

for (int j = 0; j < str1.Length; j++)

{

if (str1[j] == checksum)

{

tableA = false;

break;

}

}

break;

case 3:

int[] str2 = { 0, 2, 3, 6, 7 };

for (int j = 0; j < str2.Length; j++)

{

if (str2[j] == checksum)

{

tableA = false;

break;

}

}

break;

case 4:

int[] str3 = { 1, 3, 4, 8, 9 };

for (int j = 0; j < str3.Length; j++)

{

if (str3[j] == checksum)

{

tableA = false;

break;

}

}

break;

case 5:

int[] str4 = { 0, 1, 2, 4, 5, 7 };

for (int j = 0; j < str4.Length; j++)

{

if (str4[j] == checksum)

{

tableA = false;

break;

}

}

break;

}

if (tableA)

AddOnn = AddOnn + Convert.ToChar(65 + Convert.ToInt32(chaine.Substring(i - 1, 1)));

else

AddOnn = AddOnn + Convert.ToChar(75 + Convert.ToInt32(chaine.Substring(i - 1, 1)));

if ((chaine.Length == 2 && i == 1) || (chaine.Length == 5 && i < 5))

AddOnn = AddOnn + Convert.ToChar(92);

}

 

}

}

return AddOnn;

}

Last Word

I hope you enjoyed this article and will be using some ideas from here in your projects...

Best of luck and thanks a lot!

<script src="http://www.codeproject.com/script/togglePre.js" type="text/javascript"></script>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionhow to print barcode EAN8 in C# Pin
Member 108830446-Jun-16 4:48
Member 108830446-Jun-16 4:48 
GeneralThank you. I had some changes and have an barcode creator. Pin
alidayan23-Jul-14 1:55
alidayan23-Jul-14 1:55 
Thank you this is great. I just use some of your code and create an barcode creator with database.
Questioncode39 Pin
Member 792522011-Jul-11 2:16
Member 792522011-Jul-11 2:16 
AnswerMessage Closed Pin
22-Apr-13 16:51
Nikeduban22-Apr-13 16:51 
GeneralMy vote of 5 Pin
Mojtaba Rezaeian22-Dec-10 6:44
Mojtaba Rezaeian22-Dec-10 6:44 
GeneralNote: EAN-13 and number of digits Pin
Geir Danielsen3-Oct-07 3:02
Geir Danielsen3-Oct-07 3:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.