C#调用快递鸟电子面单API实现批量打印电子面单功能
前期准备
安装一个打印插件(K-Print)
http://kdniao.com/documents-instrument
1. 判断你是否安装打印插件和是否升级插件
<asp:HiddenField ID="HiddenField1" runat="server" />
<object id="LODOP_OB" classid="clsid:2105C259-1E0C-4534-8141-A753534CB4CA" height="0"
width="0">
<embed id="LODOP_EM" type="application/x-print-lodop" pluginspage="install_lodop32.exe"
height="0" width="0">
</object>
引用打印的js
<script src="js/LodopFuncs.js"></script>
js内容:
var CreatedOKLodop7766 = null;
function getLodop(oOBJECT, oEMBED) {
/**************************
本函数根据浏览器类型决定采用哪个页面元素作为Lodop对象:
IE系列、IE内核系列的浏览器采用oOBJECT,
其它浏览器(Firefox系列、Chrome系列、Opera系列、Safari系列等)采用oEMBED,
如果页面没有相关对象元素,则新建一个或使用上次那个,避免重复生成。
64位浏览器指向64位的安装程序install_lodop64.exe。
**************************/
var strHtmInstall = "<br><font color='#FF00FF'>打印控件未安装!点击这里<a href='../yujian110/print/install_lodop32.zip' target='_self'>执行安装</a>,安装后请刷新页面或重新进入。</font>";
var strHtmUpdate = "<br><font color='#FF00FF'>打印控件需要升级!点击这里<a href='../yujian110/print/install_lodop32.zip' target='_self'>执行升级</a>,升级后请重新进入。</font>";
var strHtm64_Install = "<br><font color='#FF00FF'>打印控件未安装!点击这里<a href='../yujian110/print/install_lodop64.zip' target='_self'>执行安装</a>,安装后请刷新页面或重新进入。</font>";
var strHtm64_Update = "<br><font color='#FF00FF'>打印控件需要升级!点击这里<a href='../yujian110/print/install_lodop64.zip' target='_self'>执行升级</a>,升级后请重新进入。</font>";
var strHtmFireFox = "<br><br><font color='#FF00FF'>(注意:如曾安装过Lodop旧版附件npActiveXPLugin,请在【工具】->【附加组件】->【扩展】中先卸它)</font>";
var strHtmChrome = "<br><br><font color='#FF00FF'>(如果此前正常,仅因浏览器升级或重安装而出问题,需重新执行以上安装)</font>";
var LODOP;
try {
//=====判断浏览器类型:===============
var isIE = (navigator.userAgent.indexOf('MSIE') >= 0) || (navigator.userAgent.indexOf('Trident') >= 0);
var is64IE = isIE && (navigator.userAgent.indexOf('x64') >= 0);
//=====如果页面有Lodop就直接使用,没有则新建:==========
if (oOBJECT != undefined || oEMBED != undefined) {
if (isIE)
LODOP = oOBJECT;
else
LODOP = oEMBED;
} else {
if (CreatedOKLodop7766 == null) {
LODOP = document.createElement("object");
LODOP.setAttribute("width", 0);
LODOP.setAttribute("height", 0);
LODOP.setAttribute("style", "position:absolute;left:0px;top:-100px;width:0px;height:0px;");
if (isIE) LODOP.setAttribute("classid", "clsid:2105C259-1E0C-4534-8141-A753534CB4CA");
else LODOP.setAttribute("type", "application/x-print-lodop");
document.documentElement.appendChild(LODOP);
CreatedOKLodop7766 = LODOP;
} else
LODOP = CreatedOKLodop7766;
};
//=====判断Lodop插件是否安装过,没有安装或版本过低就提示下载安装:==========
if ((LODOP == null) || (typeof (LODOP.VERSION) == "undefined") || (LODOP.VERSION < "6.1.8.0")) {
if (is64IE) Version(64); else
if (isIE) Version(32); else
Version(32);
return LODOP;
};
//=====如下空白位置适合调用统一功能(如注册码、语言选择等):====
LODOP.SET_LICENSES("", "", "", "");
//============================================================
return LODOP;
} catch (err) {
if (is64IE)
document.documentElement.innerHTML = "Error:" + strHtm64_Install + document.documentElement.innerHTML; else
document.documentElement.innerHTML = "Error:" + strHtmInstall + document.documentElement.innerHTML;
return LODOP;
};
}
//num可选32或64,update有true升级或false不升级 提示升级内容
function Version(num, update) {
$('#myModal').modal('show');
};
写一个打印的方法
<script>
function myPrintSetupyundaBatchPrint() {//批量打印韵达快递单
LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'));
LODOP.PRINT_INITA(0, 0, 1500, 1100, "打印");
LODOP.SET_SHOW_MODE("BKIMG_TOP", -25);
LODOP.SET_PRINT_PAGESIZE(1, 2300, 1290, "CreateCustomPage");
// LODOP.SET_PRINTER_INDEX(getSelectedPrintIndex());
LODOP.ADD_PRINT_SETUP_BKIMG("<img border='0' src='imgs/yunda.jpg'>");
LODOP.SET_SHOW_MODE("BKIMG_IN_PREVIEW", true);
// LODOP.SET_PRINT_STYLE("FontName", "微软雅黑");
LODOP.SET_PRINT_STYLE("FontSize", 11);
//LODOP.SET_PRINT_STYLE("Bold", 1);
var slist = Array();
slist = document.getElementById("<%=HiddenField1.ClientID %>").value.toString().split('^');
for (i = 0; i < slist.length; i++) {
var s = Array();
s = slist[i].toString().split('~');
LODOP.NewPage();
LODOP.ADD_PRINT_TEXT(50, 59, 233, 23, s[0]);
LODOP.ADD_PRINT_TEXT(74, 129, 75, 23, s[10]);
LODOP.ADD_PRINT_TEXT(123, 114, 263, 70, s[7]);
LODOP.ADD_PRINT_TEXT(182, 609, 120, 21, s[4]);
LODOP.ADD_PRINT_TEXT(123, 403, 276, 68, s[3]);
LODOP.SET_PRINT_STYLEA(0, "LineSpacing", 10);
// LODOP.ADD_PRINT_TEXT(377, 462, 195, 50, s[5]);
// LODOP.SET_PRINT_STYLEA(0, "FontSize", 9);
LODOP.ADD_PRINT_TEXT(206, 154, 113, 23, s[9]);
LODOP.ADD_PRINT_TEXT(77, 657, 120, 38, s[2]);
LODOP.SET_PRINT_STYLEA(0, "LineSpacing", 1);
LODOP.ADD_PRINT_TEXT(77, 461, 110, 35, s[1]);
LODOP.ADD_PRINT_TEXT(77, 276, 100, 23, s[8]);
var mdate = new Date();
LODOP.ADD_PRINT_TEXT(362, 180, 45, 23, mdate.getFullYear());
LODOP.ADD_PRINT_TEXT(362, 230, 25, 23, mdate.getMonth() + 1);
LODOP.ADD_PRINT_TEXT(362, 260, 25, 23, mdate.getDate());
LODOP.ADD_PRINT_TEXT(362, 290, 25, 23, mdate.getHours());
}
// LODOP.PRINT_SETUP(); //维护模式
// LODOP.PRINT_DESIGN(); //设计模式
LODOP.PREVIEW(); //预览模式
};
</script>
执行这个方法就可以调取快递鸟打印插件进行打印
批量打印只是将读取的数据先分割成一个数组{[1,2],[3.4]},分别读取实现批量打印.