CCPrintingApi CCPrintingAPI = null;
CCPrintingAPI = new CCPrintingApi();
//獲取所有(yǒu)打印機
GetPrinters(m_OpenId, m_Tokens);
//指定打印機
String printer = "TSC TTP-244 Pro";
//指定傳入的數(shù)據
String prtdata = "[{\"Barcode\":\"2315002468301572\",\"Printdate\":\"2023-06-05 12:28:30\"}]";
//執行(xíng)打印
Print(m_OpenId, m_Tokens, printer, prtdata, 0)
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]));
}
}
});
}
private String GetSelfDraw(){
CCSelfdraw vSelfdraw = new CCSelfdraw();
//數(shù)據字段
String[] FieldNames = {"Barcode","Printdate"};
vSelfdraw.XDataBase.SetFieldNames(FieldNames);
vSelfdraw.XPageSettings.PrinterPaperAutoSet =1;
vSelfdraw.XPageSettings.UseQuickSetTool = 1;
//标簽尺寸
vSelfdraw.XLabelDetail .Width = 70;
vSelfdraw.XLabelDetail .Height = 50;
vSelfdraw.XLabelDetail .ColumnSpan = 2;
vSelfdraw.XLabelDetail .RowSpan = 2;
//文本1
TextElement text1 = new TextElement();
text1.LabelType = 1;
text1.Name ="text1";
text1.X = 0;
text1.Y = 3;
text1.Width = 70;
text1.Height = 8;
text1.IsBind = 0;
text1.BindField = "";
text1.Data = "庫位編碼";
text1.FontSize = 14;
text1.FontName = "黑(hēi)體(tǐ)";
text1.HAlignment = 1;
text1.VAlignment = 1;
vSelfdraw.XTextElements.add(text1) ;
//文本2
TextElement text2 = new TextElement();
text2.LabelType = 1;
text2.Name ="text2";
text2.X = 0;
text2.Y = 38;
text2.Width = 70;
text2.Height = 8;
text2.IsBind = 1;
text2.BindField = "Printdate";
text2.Data = "";
text2.FontSize = 12;
text2.FontName = "黑(hēi)體(tǐ)";
text2.HAlignment = 1;
text2.VAlignment = 1;
vSelfdraw.XTextElements.add(text2) ;
//條碼
BarcodeElement barcode1 = new BarcodeElement();
barcode1.LabelType = 1;
barcode1.Name ="barcode1";
barcode1.X = 6;
barcode1.Y = 15;
barcode1.Width = 56;
barcode1.Height = 20;
barcode1.IsBind = 1;
barcode1.BindField = "Barcode";
barcode1.Data = "";
barcode1.FontSize = 18;
barcode1.FontName = "黑(hēi)體(tǐ)";
vSelfdraw.XBarcodeElements.add(barcode1) ;
return vSelfdraw.toString();
}
public void Print(String openid,String token,String printer,String prtdata, int ispreview){
//構建模闆
String selfdraw = GetSelfDraw();
//設置打印參數(shù)
ConfigPrintSettings cfgData = GetPrintSettings(openid, token,printer,prtdata,selfdraw,ispreview);
//執行(xíng)打印命令
CCPrintingAPI.Excute(cfgData, new CCPrintingApiCallBack() {
/*打印命令執行(xíng)回調 */
@Override
public void OnFinished(int status, String[] arg0, String sessionid, String[] Data){
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,String printer,String prtdata,String selfdraw,int ispreview) {
/*打印參數(shù)配置,打印數(shù)據設置*/
ConfigPrintSettings cfgData = new ConfigPrintSettings();
cfgData.XAction = "Print";
cfgData.XOpenId = openid;
cfgData.XTokens = token;
/*指定打印機名稱,不使用默認打印機時(shí),為(wèi)空(kōng),則使用配置的打印機*/
cfgData.PrinterName = printer;
/*0-打印*/
cfgData.Preview = ispreview;
/*模闆方式,0-客戶端模闆,1-遠程模闆,2-如果本地模闆存在,使用本地,不存在使用遠程模闆,3-不使用模闆*/
cfgData.TemplateMode = 3;
cfgData.SelfDraw = selfdraw;
/*打印數(shù)據的類型,0-數(shù)組,1-Json數(shù)組,元素為(wèi)JSON對象,字段:值 , JSON.stringify(prtData)*/
cfgData.PrintDataType = 1;
/*打印數(shù)據*/
cfgData.PrintData = prtdata;
return cfgData;
}