1,首先需要了解各种条形码生成的原理,此处只用了EAN-13,以下是返回条形码显示状态的字符串,0表示白色,1表示黑色
代码
public static string getEAN13(string s)
{
int checkcode_input = -1;//输入的校验码
if (!Regex.IsMatch(s, @"^\d{12}$"))
{
if (!Regex.IsMatch(s, @"^\d{13}$"))
{
return "存在不允许的字符!";
}
else
{
checkcode_input = int.Parse(s[12].ToString());
s = s.Substring(0, 12);
}
}
int sum_even = 0;//偶数位之和
int sum_odd = 0;//奇数位之和
for (int i = 0; i < 12; i++)
{
if (i % 2 == 0)
{
sum_odd += int.Parse(s[i].ToString());
}
else
{
sum_even += int.Parse(s[i].ToString());
}
}
int checkcode = (10 - (sum_even * 3 + sum_odd) % 10) % 10;//校验码
if (checkcode_input > 0 && checkcode_input != checkcode)
{
return "输入的校验码错误!";
}
s += checkcode;//变成13位
// 000000000101 左侧42个 01010 右侧35个 校验7个 101000000000
// 6 101 左侧6位 01010 右侧5位 校验1位 101000000000
string result_bin = "";//二进制串
result_bin += "000000000101";
string type = ean13type(s[0]);
for (int i = 1; i < 7; i++)
{
result_bin += ean13(s[i], type[i - 1]);
}
result_bin += "01010";
for (int i = 7; i < 13; i++)
{
result_bin += ean13(s[i], 'C');
}
result_bin += "101000000000";
return result_bin;
}
private static string ean13(char c, char type)
{
switch (type)
{
case 'A':
{
switch (c)
{
case '0': return "0001101";
case '1': return "0011001";
case '2': return "0010011";
case '3': return "0111101";//011101
case '4': return "0100011";
case '5': return "0110001";
case '6': return "0101111";
case '7': return "0111011";
case '8': return "0110111";
case '9': return "0001011";
default: return "Error!";
}
}
case 'B':
{
switch (c)
{
case '0': return "0100111";
case '1': return "0110011";
case '2': return "0011011";
case '3': return "0100001";
case '4': return "0011101";
case '5': return "0111001";
case '6': return "0000101";//000101
case '7': return "0010001";
case '8': return "0001001";
case '9': return "0010111";
default: return "Error!";
}
}
case 'C':
{
switch (c)
{
case '0': return "1110010";
case '1': return "1100110";
case '2': return "1101100";
case '3': return "1000010";
case '4': return "1011100";
case '5': return "1001110";
case '6': return "1010000";
case '7': return "1000100";
case '8': return "1001000";
case '9': return "1110100";
default: return "Error!";
}
}
default: return "Error!";
}
}
private static string ean13type(char c)
{
switch (c)
{
case '0': return "AAAAAA";
case '1': return "AABABB";
case '2': return "AABBAB";
case '3': return "AABBBA";
case '4': return "ABAABB";
case '5': return "ABBAAB";
case '6': return "ABBBAA";//中国
case '7': return "ABABAB";
case '8': return "ABABBA";
case '9': return "ABBABA";
default: return "Error!";
}
}
2,是需要将以上的字符串转成相应的图形,为了能在word中显示此图像,设计思路是一种xml格式的矩形,一种是将条形码转成图片的Base64
xml格式的矩形表示格式是,其中fillcolor是表示黑色或者白色
代码
private static string cell = "<w:r><w:rPr><w:noProof/></w:rPr><w:pict><v:rect id=\"_x0000_s2051\" style=\"border:0px;position:absolute;left:0;text-align:left;margin-left:{0};margin-top:{1}px;width:{2}px;height:{3}px;z-index:2\" fillcolor=\"{4}\"/></w:pict></w:r>";
在word中显示图片base64的方法,以下第一个参数插入base64
代码
<w:binData w:name=\"wordml://01000001.jpg\">{0}</w:binData><v:shape id=\"_x0000_i1025\" type=\"#_x0000_t75\" style=\"width: {1}px;height:{2}px\"><v:imagedata src=\"wordml://01000001.jpg\" o:title=\"3_oJYvYzrGpaeK\"/></v:shape>
3,在word中插入书签,然后另存为xml格式,并将此xml文件的后缀改为.doc,便可以对在word的书签位置插入条形码了。
xml中的书签表达方式为
<aml:annotation aml:id="0" w:type="Word.Bookmark.Start" w:name="条形码" />
<aml:annotation aml:id="0" w:type="Word.Bookmark.End" />
只要在两者之间插入第2步中的字符串,便可以成功插入条形码了。
插入矩形的xml格式需要绝对定位,而插入图片不用定位,不过图片的质量会有所损失而变的有点模糊。
此方法也可以在书签位置插入文字。

posted on
浙公网安备 33010602011771号