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 = "[{\"ImageUrl\":\"static/picture/2315002468301572.jpg\"}]";
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 = GetSelfDraw();
// 設置打印參數(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 GetSelfDraw()
{
CCSelfdraw vSelfdraw = new CCSelfdraw();
// 數(shù)據字段
string[] FieldNames = { "ImageUrl" };
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;
// 圖片設置
ImageElement vImageElement = new ImageElement();
vImageElement.Name = "Image1";
vImageElement.X = 0;
vImageElement.Y = 0;
vImageElement.Width = 70;
vImageElement.Height = 50;
vImageElement.IsBind = 1;
vImageElement.BindField = "ImageUrl";
vImageElement.KeepRatio = 1;
vImageElement.ImageColor = 2;
vSelfdraw.XImageElements.Add(vImageElement);
return vSelfdraw.ToString();
}