CCPrintingApi CCPrintingAPI = null;
CCPrintingAPI = new CCPrintingApi();
//獲取所有打印機(jī)
GetPrinters(m_OpenId, m_Tokens);
//指定打印機(jī)
String printer = "TSC TTP-244 Pro";
//指定傳入的數(shù)據(jù)
String prtdata = "[{\"Barcode\":\"2315002468301572\",\"Printdate\":\"2023-06-05 12:28:30\"}]";
//執(zhí)行打印
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ù)據(jù)字段
String[] FieldNames = {"Barcode","Printdate"};
vSelfdraw.XDataBase.SetFieldNames(FieldNames);
vSelfdraw.XPageSettings.PrinterPaperAutoSet =1;
vSelfdraw.XPageSettings.UseQuickSetTool = 1;
//標(biāo)簽尺寸
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 = "黑體";
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 = "黑體";
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 = "黑體";
vSelfdraw.XBarcodeElements.add(barcode1) ;
return vSelfdraw.toString();
}
public void Print(String openid,String token,String printer,String prtdata, int ispreview){
//構(gòu)建模板
String selfdraw = GetSelfDraw();
//設(shè)置打印參數(shù)
ConfigPrintSettings cfgData = GetPrintSettings(openid, token,printer,prtdata,selfdraw,ispreview);
//執(zhí)行打印命令
CCPrintingAPI.Excute(cfgData, new CCPrintingApiCallBack() {
/*打印命令執(zhí)行回調(diào) */
@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ù)據(jù)設(shè)置*/
ConfigPrintSettings cfgData = new ConfigPrintSettings();
cfgData.XAction = "Print";
cfgData.XOpenId = openid;
cfgData.XTokens = token;
/*指定打印機(jī)名稱,不使用默認(rèn)打印機(jī)時(shí),為空,則使用配置的打印機(jī)*/
cfgData.PrinterName = printer;
/*0-打印*/
cfgData.Preview = ispreview;
/*模板方式,0-客戶端模板,1-遠(yuǎn)程模板,2-如果本地模板存在,使用本地,不存在使用遠(yuǎn)程模板,3-不使用模板*/
cfgData.TemplateMode = 3;
cfgData.SelfDraw = selfdraw;
/*打印數(shù)據(jù)的類型,0-數(shù)組,1-Json數(shù)組,元素為JSON對象,字段:值 , JSON.stringify(prtData)*/
cfgData.PrintDataType = 1;
/*打印數(shù)據(jù)*/
cfgData.PrintData = prtdata;
return cfgData;
}