关于条形码生成 及在打印页显示不出来的问题(BarCodeToHTML.cs)

如果只需要在电脑显示不打印 用BarCodeToHTML.cs包的方法getEAN13()即可;

如果需要在电脑显示并且打印 需要修改 BarCodeToHTML.cs包的方法getEAN13();

把所有background改成以背景图片显示即可;

以下代码是修改getEAN13()background后的代码:

public static string getEAN13(string s, int width, int height)
        {
            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";

            string result_html = ""; //HTML代码
            string color = "";       //颜色
            int height_bottom = width * 5;
            foreach (char c in result_bin)
            {
                color = c == '0' ? "#FFFFFF" : "#000000";
                result_html += "<div style="" + width + "px;height:" + height + "px;float:left;background:" + color + ";"></div>";
            }
            result_html += "<div style="clear:both"></div>";

            result_html += "<div style="float:left;color:#000000;" + (width * 9) + "px;text-align:center;">" + s[0] + "</div>";
            result_html += "<div style="float:left;" + width + "px;height:" + height_bottom + "px;background:#000000;"></div>";
            result_html += "<div style="float:left;" + width + "px;height:" + height_bottom + "px;background:#FFFFFF;"></div>";
            result_html += "<div style="float:left;" + width + "px;height:" + height_bottom + "px;background:#000000;"></div>";
            for (int i = 1; i < 7; i++)
            {
                result_html += "<div style="float:left;" + (width * 7) + "px;color:#000000;text-align:center;">" + s[i] + "</div>";
            }
            result_html += "<div style="float:left;" + width + "px;height:" + height_bottom + "px;background:#FFFFFF;"></div>";
            result_html += "<div style="float:left;" + width + "px;height:" + height_bottom + "px;background:#000000;"></div>";
            result_html += "<div style="float:left;" + width + "px;height:" + height_bottom + "px;background:#FFFFFF;"></div>";
            result_html += "<div style="float:left;" + width + "px;height:" + height_bottom + "px;background:#000000;"></div>";
            result_html += "<div style="float:left;" + width + "px;height:" + height_bottom + "px;background:#FFFFFF;"></div>";
            for (int i = 7; i < 13; i++)
            {
                result_html += "<div style="float:left;" + (width * 7) + "px;color:#000000;text-align:center;">" + s[i] + "</div>";
            }
            result_html += "<div style="float:left;" + width + "px;height:" + height_bottom + "px;background:#000000;"></div>";
            result_html += "<div style="float:left;" + width + "px;height:" + height_bottom + "px;background:#FFFFFF;"></div>";
            result_html += "<div style="float:left;" + width + "px;height:" + height_bottom + "px;background:#000000;"></div>";
            result_html += "<div style="float:left;color:#000000;" + (width * 9) + "px;"></div>";
            result_html += "<div style="clear:both"></div>";

            return "<div style="background:#FFFFFF;padding:0px;font-size:" + (width * 10) + "px;font-family:'楷体';">" + result_html + "</div>";
        }

  

使用方法:

//调用时传入 12位 未校验的数字字符串("140504203323"),宽度(2),高度(50)

//BarCodeToHTML.getEAN13("140504203323", 2, 50);    

//BarCodeToHTML.getEAN13("140504203323", 1, 30);    

使用方法BarCodeToHTML.getEAN13(string s, int width, int height)

 getEAN13()方法中会自动校验,返回13位条码

原文地址:https://www.cnblogs.com/lengv10/p/3711427.html