C#調用CodeSoft Label檔打印Label及預覽Demo


一.布局如下

 

二.后端代碼實現如下

        
        public string str = "887451254";
        public string filePath=null;

        string labFile = "";

        LabelManager2.ApplicationClass labApp = null;
        LabelManager2.ApplicationClass labApp2 = null; 
        LabelManager2.Document doc = null;
        
        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog(); //選擇要打印的Label檔
            ofd.ShowDialog();
            textBox1.Text = ofd.FileName;
            if (ilabelLx == 0)
            {
                labFile = ofd.FileName;
                try
                {
                    labApp = new LabelManager2.ApplicationClass();
                    labApp.Documents.Open(labFile, false);
                    filePath = "D:\\" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + ".bmp";//保存圖片的路徑  為后面預覽圖片做准備
                    doc = labApp.ActiveDocument;
                    doc.Variables.FormVariables.Item("data").Value = str.ToString();//向Label檔傳入變量
                    string st = doc.CopyImageToFile(8, "BMP", 0, 100, filePath);
                    pictureBox1.Load(filePath);  //以圖片的形式瀏覽打印的Label
                    doc.PrintDocument(1);
                }
                catch(Exception ex)
                {
                    MessageBox.Show("出錯了,原因"+ex.Message);
                }
                finally
                {
                    labApp.Documents.CloseAll(true);
                    labApp.Quit();
                    labApp = null;
                    doc = null;
                }
            }

        }

        //調用本地CodeSoft顯示Label
        private void button2_Click(object sender, EventArgs e)
        {
            
            try
            { 
                labApp2 = new LabelManager2.ApplicationClass();
                labApp2.Documents.Open(labFile, false);
                doc = labApp2.ActiveDocument;
                doc.Variables.FormVariables.Item("data").Value = str.ToString();
                doc.Application.Visible = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("出錯了,原因" + ex.Message);
            }
           
        }

 

三.運行結果如下

 

四.引用的第三方程序集

Interop.LabelManager2.dll


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM