在html页面上点击打印按钮,执行js方法
function Print() {
var dgRows = $('#dg').datagrid('getSelections');
if (dgRows.length <= 0) {
alert('请勾选需要打印的行!');
return;
}
var outArray = new Array();
for (var i = 0; i < dgRows.length; i++) {
outArray.push({ "cHBCode": dgRows[i].cHBCode, "cHBName": dgRows[i].cHBName });
}
var a = JSON.stringify(outArray);
//alert(a);
$.post("../../../GenerateQRCodeTag/TestBartenderPrint.ashx?cType=StorageBag", a, function (data) {
if(data=="error")
{
$.messager.alert("打印失败!");
}
});
}
一般处理程序TestBartenderPrint.ashx中的实现方法步骤为
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;
using bartenderClassLibrary;
namespace AdminLTE.GenerateQRCodeTag
{
/// <summary>
/// TestBartenderPrint 的摘要说明
/// </summary>
public class TestBartenderPrint : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
Class1 classTest = new Class1();//bartenderClassLibrary.dll中的类名
StringBuilder sb = new StringBuilder();
string test = context.Request.QueryString["cType"];
if (test == "HealthyBag")
{
List<SelectListHealthyBagTest> dicParameter = GetHealthyBagParameter(context);
for (int i = 0; i < dicParameter.Count; i++)
{
string str = dicParameter[i].cHBCode + ";" + dicParameter[i].cHBName;
string path = "F:\test.btw";
try
{
classTest.barTenderM(path, str);
}
catch(Exception ex)
{
context.Response.Write("error");
throw;
}
}
}
}
private List<SelectListHealthyBagTest> GetHealthyBagParameter(HttpContext context)
{
StreamReader reader = new StreamReader(context.Request.InputStream);
String strJson = HttpUtility.UrlDecode(reader.ReadToEnd());
JavaScriptSerializer jss = new JavaScriptSerializer();
List<SelectListHealthyBagTest> dicParameter = jss.Deserialize<List<SelectListHealthyBagTest>>(strJson);
return dicParameter;
}
public bool IsReusable
{
get
{
return false;
}
}
}
public partial class SelectListHealthyBagTest
{
public string cHBCode { get; set; }
public string cHBName { get; set; }
}
}
barTenderM方法存在于类库
bartenderClassLibrary.dll中,实现的是通过C#对bartender的打印操作
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace bartenderClassLibrary
{
public class Class1
{
private static BarTender.Application btApp;
private static BarTender.Format btFormat;
public void barTenderM(string path,string str)
{
string code, codeName;
btApp = new BarTender.Application();
string[] sArray = str.Split(';');
code = sArray[0];
codeName = sArray[1];
btFormat = btApp.Formats.Open(path, false, "");
btFormat.PrintSetup.NumberSerializedLabels = 1;
btFormat.SetNamedSubStringValue("QRCodeTest1",code);
btFormat.SetNamedSubStringValue("txtFont1",codeName);
btFormat.PrintOut(true, false);
btFormat.Close(BarTender.BtSaveOptions.btDoNotSaveChanges);
btApp.Quit(BarTender.BtSaveOptions.btSaveChanges);
}
}
}
