C# 调用打印机API - APISample c# 标签打印范例代码C# 打印, C#实现打印,C#调用打印机, C# 打印API,C# 打印报表,CSharp标签打印,CSharp单据打印CCPrintingAPI , C# 打印范例代码,C#标签打印,条码,二维码标签,C# 单据打印,很容易集成。
CCPrintingAPI XI
轻量、简洁、稳定、可靠,通用的打印机API接口,支持近百个品牌,数千个型号打印机。
打印机接口API C# 范例代码
C#
CCPrintingAPI C# API打印范例代码,C#调用打印机,调用CCPrintingAPI打印机接口API。

1. 引入 CCPrintingApiNet.dll
引入CCPrintingApiNet.dll后,即可使用API的相关方法,进行设置和打印操作。
CCPrintingApiNet CCPrintingAPI = null;
CCPrintingAPI = new CCPrintingApiNet();
//获取HID
GetHID(m_OpenId, m_Tokens);
//获取所有打印机
GetPrinters(m_OpenId, m_Tokens,6789);    
//打印
Print(m_OpenId, m_Tokens,6789);  
2. 获取打印机
获取计算机上所有打印机, 打印时,指定该列表的打印机名称。如果能确保打印机名称不变,该操作也可省去。
public void GetPrinters(String openid,String token,int itemid){
     ConfigPrintSettings cfgData = new ConfigPrintSettings() ;
     cfgData.XAction = "Printers";
     cfgData.XOpenId = openid;
     cfgData.XTokens = token;
     cfgData.ItemID = itemid;
     CCPrintingAPI.Excute(cfgData, (status, arg0, sessionid) =>
            {
                Console.WriteLine(string.Format("Excute Status:{0}, SessionId:{1}", status, sessionid));
                if (arg0 == null || arg0.Length == 0) return;
                Console.WriteLine(string.Format("Excute Result:{0}", string.Join(" ", arg0)));
            });
}
3. 打印
进行相关参数设置后(指定打印机,设置模板,设置打印数据),将指令通过API传给打印机。
public void Print(String openid,String token,int itemid){
ConfigPrintSettings cfgData = GetPrintSettings(openid, token, itemid);
CCPrintingAPI.Excute(cfgData, (status, arg0, sessionid) =>
{
     Console.WriteLine(string.Format("Excute Status:{0}, SessionId:{1}", status, sessionid));
      if (arg0 == null || arg0.Length == 0) return;
      Console.WriteLine(string.Format("Excute Result:{0}", string.Join(" ", arg0)));
  });
}

  
private ConfigPrintSettings GetPrintSettings(string openid, string token, int itemid)
{
        DataTable prtdata = new DataTable("PrintData");
        prtdata.Columns.Add("Field1", typeof(string));
        prtdata.Columns.Add("Field2", typeof(string));
        prtdata.Columns.Add("Field3", typeof(string));
        prtdata.Columns.Add("Field4", typeof(string));

        DataRow row = prtdata.NewRow();
        row["Field1"] = "CCPrintingAPI";
        row["Field2"] = ".Net 标签打印开发接口";
        row["Field3"] = "901201251002404";
        row["Field4"] = "325435";
        prtdata.Rows.Add(row);
        prtdata.AcceptChanges();

        //指定打印机名称
        string selPrinter = "Deli DL-888C";
        /*打印参数配置,打印数据设置*/
        ConfigPrintSettings cfgData = new ConfigPrintSettings();
        cfgData.XAction = "Print";
        cfgData.XOpenId = openid;
        cfgData.XTokens = token;
        cfgData.ItemID = itemid;
        /*指定打印机名称,不使用默认打印机时,为空,则使用配置的打印机*/
        cfgData.PrinterName = selPrinter;
        /*0-打印,3-获取打印预览*/
        cfgData.Preview = 0;
        /*模板方式,0-客户端模板,1-远程模板,2-如果本地模板存在,使用本地,不存在使用远程模板,3-不使用模板*/
        cfgData.TemplateMode = 1;
        /*远程模板URL*/
        cfgData.TemplateURL = "http://img.51321.cn/web/chicore/VPrinting/API";
        //cfgPrintSettings.TemplateURL = "E:\\临时数据\\打印\\打印样本\\智能制造";
        cfgData.TemplateName = "984.Lblx";
        /*打印数据的类型,0-数组,1-Json数组,元素为JSON对象,字段:值 , JSON.stringify(prtData) 2 Datatable*/
        cfgData.PrintDataType = 2;
        /*打印数据*/
        cfgData.PrintData = prtdata;
        return cfgData;
}
4. 读取客户端HID
读取安装CCPrintingAPI的客户端计算机HID,该HID可以用来标识计算机。该方法,根据需要使用。
public void GetHID(String openid,String token){
ConfigPrintSettings cfgData = new ConfigPrintSettings() ;
cfgData.XAction = "HID";
cfgData.XOpenId = openid;
cfgData.XTokens = token;
CCPrintingAPI.Excute(cfgData, (status, arg0, sessionid) =>
 {
        Console.WriteLine(string.Format("Excute Status:{0}, SessionId:{1}", status, sessionid));
         if (arg0 == null || arg0.Length == 0) return;
         Console.WriteLine(string.Format("Excute Result:{0}", string.Join(" ", arg0)));
   });
}