Click here to Skip to main content
Licence CPOL
First Posted 29 Jul 2007
Views 25,908
Downloads 936
Bookmarked 29 times

Use bar code fonts in C#.net

By | 29 Jul 2007 | Article
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)

 
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)

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)

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)

About the Author

anishmm

Software Developer (Senior)

India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questioncode39 PinmemberMember 79252202:16 11 Jul '11  
GeneralMy vote of 5 Pinmembermojtaba4606:44 22 Dec '10  
GeneralNote: EAN-13 and number of digits PinmemberGeir Danielsen3:02 3 Oct '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 30 Jul 2007
Article Copyright 2007 by anishmm
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid