日期:2014-05-17  浏览次数:20812 次

关于c#打印的疑问
现在急求啊,先上原有的代码,
C# code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Drawing.Printing;
using System.Windows.Forms;

namespace Code128Test
{
    public partial class FrmTest : Form
    {
        string inputString;
        int length=0;
        public FrmTest()
        {
            InitializeComponent();
        }

        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;
        }

        public void PrintLable()
        {
            PrintDocument pd = new PrintDocument();
            StandardPrintController controler = new StandardPrintController();

            try
            {
                pd.PrintPage += new PrintPageEventHandler(this.PrintCustomLable);
                pd.PrintController = controler;
                pd.Print();
                return;
            }
            catch (Exception err)
            {
                Console.WriteLine(err.Message);
                return;
            }
            finally
            {
                pd.Dispose();
            }

        }
        public void PrintCustomLable(Object Sender, PrintPageEventArgs av)
        {
            int tmdx = Convert.ToInt32(this.textBox2.Text);
            int tmX = Convert.ToInt32(this.textBox3.Text);
            int tmY = Convert.ToInt32(this.textBox4.Text);
            int zhX = Convert.ToInt32(this.textBox5.Text);
            int zhY = Convert.ToInt32(this.textBox6.Text);
            Font ft3 = new System.Drawing.Font("黑体", 18, FontStyle.Bold, GraphicsUnit.World);
            Font ft4 = new System.Drawing.Font("黑体", 17, FontStyle.Bold, GraphicsUnit.World);
            Font ft7 = new System.Drawing.Font("黑体", 14, FontStyle.Bold, GraphicsUnit.World);
            Font ft6 = new System.Drawing.Font("Times New Roman", 12, FontStyle.Regular, GraphicsUnit.World);
            Font ft1 = new System.Drawing.Font("Times New Roman", 12, FontStyle.Regular, GraphicsUnit.World);
            Font ft2 = new System.Drawing.Font("Code 128", 24, FontStyle.Regular, GraphicsUnit.World);
            Font ft5 = new System.Drawing.Font("黑体", 11, FontStyle.Regular, GraphicsUnit.World);
            Brush br = new SolidBrush(Color.Black);
            Margins margins = new Margins(0, 0, 0, 0);
            av.PageSettings.Margins = margins;

            av.Graphics.DrawString("我们的爱", ft3, br, 30, 58);
            av.Graphics.DrawString("GPRS", ft4, br, 30, 78);
            av.Graphics.DrawString("模块", ft7, br, 67,79);
            av.Graphics.DrawString("CMIIT ID:", ft6, br, 29, 96);
            av.Graphics.DrawString("IMEI:" + inputString, ft1, br, 29, 122);
            av.Graphics.DrawString(Get128CodeString(inputString.Replace("IMEI:", "")), ft2, br, 22, 133);
            av.Graphics.DrawString("科技有限公司", ft5, br, 29, 158);
            av.HasMorePages = false;
        }

        private void btnPrint_Click(o