128条形码计算,利用Code 128字体实现条码打印

此方法利用Code 128字体实现条码打印,所以需要提前安装Code 128字体,下载地址如下:
http://pan.baidu.com/share/link?shareid=1787017019&uk=2382364834

        /// <summary>
        /// 获取128条码数据
        /// </summary>
        private string Get128CodeString(string inputData)
        {
            string result;
            //计算校验位
            int checksum = 104;
            for (int ii = 0; ii < inputData.Length; ii++)
            {
                if (inputData[ii] >= 32)
                {
                    checksum += (inputData[ii] - 32) * (ii + 1);
                }
                else
                {
                    checksum += (inputData[ii] + 64) * (ii + 1);
                }
            }
            checksum = checksum % 103;
            if (checksum < 95)
            {
                checksum += 32;
            }
            else
            {
                checksum += 100;
            }
            //条码由开始位、数据、校验位、停止位组成
            result = Convert.ToChar(204) + inputData.ToString() + Convert.ToChar(checksum) + Convert.ToChar(206);
            return result;
        }

        /// <summary>
        /// 打印条形码
        /// </summary>
        public void PrintBarCode()
        {
            PrintDocument printDoc = new PrintDocument();
            StandardPrintController controler = new StandardPrintController();

            printDoc.PrintPage += new PrintPageEventHandler(this.PrintCustom);
            printDoc.PrintController = controler;
            printDoc.Print();
        }

        /// <summary>
        /// 事件:打印设置
        /// </summary>
        public void PrintCustom(Object Sender, PrintPageEventArgs e)
        {
            Font ft1 = new System.Drawing.Font("Times New Roman", 18, FontStyle.Regular, GraphicsUnit.World);
            Font ft2 = new System.Drawing.Font("Code 128", 64, FontStyle.Regular, GraphicsUnit.World);
            Brush br = new SolidBrush(Color.Black);
            Margins margins = new Margins(50, 50, 50, 145);

            e.PageSettings.Margins = margins;
            e.Graphics.DrawString(this.Get128CodeString(m_InputString), ft2, br, 50, -3);
            e.Graphics.DrawString(this.m_InputString, ft1, br, 110, 60);
            e.HasMorePages = false;
        }

 

posted on 2013-11-28 15:08  ExDevilLee  阅读(1230)  评论(0编辑  收藏  举报

导航