CCPrintingApi CCPrintingAPI = null;
CCPrintingAPI = new CCPrintingApi();
//獲取所有(yǒu)打印機
GetPrinters(m_OpenId, m_Tokens,6789);
//獲取預覽
PrintPreview(m_OpenId, m_Tokens,6789);
//打印
String printer = "TSC TTP-244 Pro";
String prtdata = "[{\"Barcode\":\"2468301572\",\"Printdate\":\"2023-06-05 12:28:30\"}]";
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, (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)));
});
}
public void Print(string openid, string token, string printer, string prtdata, int ispreview)
{
// 構建模闆
string selfdraw = GetSelfDrawTextBarcode();
// 設置打印參數(shù)
ConfigPrintSettings cfgData = GetPrintSettings(openid, token, printer, prtdata, selfdraw, ispreview);
// 執行(xíng)打印命令
/* 打印命令執行(xíng)回調 */
CCPrintingAPI.Excute(cfgData, (int status, string[] arg0, string sessionid, string[] Data) =>
{
if (arg0 == null || arg0.Length == 0)
return;
if (status == 1)
{
Console.WriteLine(string.Format("Print:{0}", string.Join(",", arg0)));
}
else
{
Console.WriteLine(string.Format("Print Fail: {0}", 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;
}
private string GetSelfDrawTextBarcode()
{
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();
}