Java 調用打印機 - APISampleJava 标簽打印,,java 打印, java實現打印,java 調用打印機, java 打印API,java 打印報表,java 标簽打印,java 單據打印 ,CCPrintingAPI , Java 打印範例代碼,java标簽打印,條碼,二維碼标簽,java單據打印,很(hěn)容易集成。
CCPrintingAPI XI
輕量、簡潔、穩定、可(kě)靠,通(tōng)用的打印機API接口,支持近百個(gè)品牌,數(shù)千個(gè)型号打印機。
CCPrintingAPI 打印範例代碼
Java
打印機接口 Java API打印範例代碼,java調用打印機,調用CCPrintingAPI打印機接口API。

1. 引入 CCPrintingAPI.jar
引入CCPrintingAPI.jar後,即可(kě)使用API的相關方法,進行(xíng)設置和(hé)打印操作(zuò)。
CCPrintingApi CCPrintingAPI = null;
CCPrintingAPI = new CCPrintingApi();
//獲取HID
GetHID(m_OpenId, m_Tokens);
//獲取所有(yǒu)打印機
GetPrinters(m_OpenId, m_Tokens,6789);    
//打印
Print(m_OpenId, m_Tokens,6789);  
2. 獲取打印機
獲取計(jì)算(suàn)機上(shàng)所有(yǒu)打印機, 打印時(shí),指定該列表的打印機名稱。如果能确保打印機名稱不變,該操作(zuò)也可(kě)省去。
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, new CCPrintingApiCallBack() { 
       @Override
       public void OnFinished(int status, String[] arg0, String sessionid){        
          if (arg0 == null || arg0.length ==0 ) return;
             if (status == 1) {
                    System.out.println(  String.format("ALL Printers:\r\n%s  ",  CCPrintingAPI.join(",", arg0) ));
              } else{
                    System.out.println( String.format("GetPrinters Fail: %s", arg0[0]));
             }
         }
    });
}
3. 打印
進行(xíng)相關參數(shù)設置後(指定打印機,設置模闆,設置打印數(shù)據),将指令通(tōng)過API傳給打印機。
    public void Print(String openid,String token,int itemid){
        ConfigPrintSettings cfgData = GetPrintSettings(openid, token, itemid);
        CCPrintingAPI.Excute(cfgData, new CCPrintingApiCallBack() {
            @Override
            public void  OnFinished(int status, String[] arg0, String sessionid){       
                if (arg0 == null || arg0.length ==0 ) return;
                if (status == 1) {
                    System.out.println(  String.format("Print:%s",  CCPrintingAPI.join(",", arg0) )  );
                }else{
                    System.out.println( String.format("Print Fail: %s", arg0[0]));
                }
            }
        });
    }

  
    private  ConfigPrintSettings  GetPrintSettings(String openid,String token,int itemid) {
        String prtdata = "[" +
            "{\"Field1\":\"CCPrintingAPI\", \"Field2\":\"Java 标簽打印開(kāi)發接口\", \"Field3\":\"9876543210128\"},"  + 
            "{\"Field1\":\"CCPrintingAPI\", \"Field2\":\"Java 标簽打印開(kāi)發接口\", \"Field3\":\"9876543210129\"}"  +                     
            "]";

        //指定打印機名稱
        String selPrinter = "Deli DL-888C";
        selPrinter ="Honeywell PC42t (203 dpi) - DP";
        /*打印參數(shù)配置,打印數(shù)據設置*/
        ConfigPrintSettings cfgData = new ConfigPrintSettings();
        cfgData.XAction = "Print";
        cfgData.XOpenId = openid;
        cfgData.XTokens = token;
        cfgData.ItemID = itemid;
        /*指定打印機名稱,不使用默認打印機時(shí),為(wèi)空(kōng),則使用配置的打印機*/
        cfgData.PrinterName = selPrinter;
        /*0-打印*/
        cfgData.Preview = 0;
        /*模闆方式,0-客戶端模闆,1-遠程模闆,2-如果本地模闆存在,使用本地,不存在使用遠程模闆,3-不使用模闆*/
        cfgData.TemplateMode = 1;
        /*遠程模闆URL*/
        cfgData.TemplateURL = "http://img.51321.cn/web/chicore/VPrinting/API";
        //cfgData.TemplateURL = "E:\\臨時(shí)數(shù)據\\打印\\打印樣本\\智能制(zhì)造";
        cfgData.TemplateName = "984.Lblx";
        /*打印數(shù)據的類型,0-數(shù)組,1-Json數(shù)組,元素為(wèi)JSON對象,字段:值 , JSON.stringify(prtData)*/
        cfgData.PrintDataType = 1;
        /*打印數(shù)據*/
        cfgData.PrintData = prtdata;
        return cfgData;
    }
4. 讀取客戶端HID
讀取安裝CCPrintingAPI的客戶端計(jì)算(suàn)機HID,該HID可(kě)以用來(lái)标識計(jì)算(suàn)機。該方法,根據需要使用。
    public void GetHID(String openid,String token){
        ConfigPrintSettings cfgData = new ConfigPrintSettings() ;
        cfgData.XAction = "HID";
        cfgData.XOpenId = openid;
        cfgData.XTokens = token;
        CCPrintingAPI.Excute(cfgData, new CCPrintingApiCallBack() { 
            @Override
            public void OnFinished(int status, String[] arg0, String sessionid) {        
                if (arg0 == null || arg0.length ==0 ) return;
                if (status == 1) {
                    System.out.println(  String.format("HID:%s",   CCPrintingAPI.join(",", arg0) ) );
                }else{
                    System.out.println( String.format("HID Fail: %s", arg0[0]));
                }
            }
        });
    }