使用 C# 进行 label 打印 的两种方式

 最近在研究Label打印的东西,总结了两种打印方式:机器码打印、调用CodeSoft.

 调用CodeSoft打印
 利用第三方软件codesofe进行label设计,然后在程序中调用打印。

 这种方式维护起来比较方便,手动调整label各参数指标即可。

 准备工作:
 1.安装打印机驱动
 2.安装codeSoft
 3.设计label,设置label参数
 
 程序实现:
  注意首先添加引用:Lppx2.tlb (codesoft安装后文件中)

using LabelManager2;         //引入命名空间
private void btnPrint_Click(object sender, EventArgs e)
        
{
            ApplicationClass lbl 
= new ApplicationClass();

            
try
            
{
                lbl.Documents.Open(
@"D:label.Lab"false);// 调用设计好的label文件
                Document doc = lbl.ActiveDocument;
                doc.Variables.FormVariables.Item(
"Var0").Value = txtContent.Text.Trim(); //给参数传值
                doc.Variables.FormVariables.Item("Var1").Value = txtContent2.Text.Trim(); //给参数传值

                
int Num = Convert.ToInt32(txtQuentity.Text);        //打印数量
                doc.PrintDocument(Num);                             //打印
            }

            
catch (Exception ex)
            
{
                MessageBox.Show(ex.Message);
            }

            
finally
            
{
                lbl.Quit();                                         
//退出
            }

        }

机器码打印:
 这种方式直接使用打印机机器指令进行打印,调用系统接口函数实现。
 1.建立接口函数类 LPTControl
 

using   System;   
using   System.Collections.Generic;   
using   System.Text;   
using   System.Runtime.InteropServices;

namespace SMTOffLine
{
    
class LPTControl
    
{
        [StructLayout(LayoutKind.Sequential)]
        
private struct OVERLAPPED
        
{
            
int Internal;
            
int InternalHigh;
            
int Offset;
            
int OffSetHigh;
            
int hEvent;
        }


        [DllImport(
"kernel32.dll")]
        
private static extern int CreateFile(
        
string lpFileName,
        
uint dwDesiredAccess,
        
int dwShareMode,
        
int lpSecurityAttributes,
        
int dwCreationDisposition,
        
int dwFlagsAndAttributes,
        
int hTemplateFile
        );


        [DllImport(
"kernel32.dll")]
        
private static extern bool WriteFile(
        
int hFile,
        
byte[] lpBuffer,
        
int nNumberOfBytesToWrite,
        
out   int lpNumberOfBytesWritten,
        
out   OVERLAPPED lpOverlapped
        );


        [DllImport(
"kernel32.dll")]
        
private static extern bool CloseHandle(
        
int hObject
        );

        
private int iHandle;
        
public bool Open()
        
{
            iHandle 
= CreateFile("lpt1"0x4000000000300);
            
if (iHandle != -1)
            
{
                
return true;
            }

            
else
            
{
                
return false;
            }

        }


        
public bool Write(String Mystring)
        
{
            
if (iHandle != -1)
            
{
                
int i;
                OVERLAPPED x;
                
byte[] mybyte = System.Text.Encoding.Default.GetBytes(Mystring);
                
return WriteFile(iHandle, mybyte, mybyte.Length, out  i, out  x);
            }

            
else
            
{
                
throw new Exception("打印機端口未打開!");
            }

        }


        
public bool Close()
        
{
            
return CloseHandle(iHandle);
        }


    }

}

2.程序实现
 

 private void btnPrint_Click(object sender, EventArgs e)
        
{
            
string PtrStr = "";
            
string MaterialNo="";
            
string StartValue="";
            
int PrintNum ;

            LPTControl Print 
= new LPTControl();
            
try
            
{
                Print.Open();
                
for (int i = 0; i < dtPrint.Rows.Count; i++)
                
{
                     MaterialNo 
= dtPrint.Rows[i][0].ToString();
                     StartValue 
= dtPrint.Rows[i][1].ToString();

                    PrintNum 
= Convert.ToInt32(numericUpDown1.Value);

                    
for (int j = 0; j < PrintNum; j++)
                    
{
                        
int FlowNo =(Convert.ToInt32(StartValue) + j+1);
                        
string StrFlowNo = FlowNo.ToString("000000");
                        PtrStr 
= "";
                        PtrStr 
+= "^XA^LH40,20";

                        PtrStr 
+= "^FO10,10^BY3,2,90^B3,N,90,N,N^FD" + MaterialNo + "^FS";
                        PtrStr 
+= "^FO10,105^AE^FD" + MaterialNo + "^FS";

                        PtrStr 
+= "^FO10,145^BY3,2,90^B3,N,90,N,N^FD" + StrFlowNo + "^FS";
                        PtrStr 
+= "^FO10,245^AE^FD" + StrFlowNo + "^FS";

                        PtrStr 
+= "^FO10,280^BY3,2,90^B3,N,90,N,N^FD" + DateTime.Now.ToString("yyyy-MM-dd"+ "^FS";
                        PtrStr 
+= "^FO10,385^AE^FD" + DateTime.Now.ToString("yyyy-MM-dd"+ "^FS";

                        PtrStr 
+= "^XZ";

                        Print.Write(PtrStr);

                    }


                    
//update newest Num to DB  
                    int NewNum = Convert.ToInt32(StartValue) +PrintNum;
                    
string SqlUpdate = "update tablelabel set curren_value=" + NewNum + " where material_id='" + MaterialNo + "'";
                    
try
                    
{
                        DAL DbOp 
= new DAL();
                        DbOp.ExecDBOp(SqlUpdate);
                    }

                    
catch (Exception ex)
                    
{
                        MessageBox.Show(
"Error","information");
                    }
                 
                }
      
            }

            
catch (Exception ex)
            
{
                MessageBox.Show(ex.Message, 
"Information");
            }

            
finally
            
{
                Print.Close();
            }



        }

注:机器指令的含义要详细阅读打印机相关说明文档获得。

阅读更多
个人分类: c#2005
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭