需求背景
业务打印需求需要使用TSC打印机进行条码二维码打印(由旧项目改造触发的需求)
前期准备
vs2022+WORK Service+TSCLIB.DLL
使用方式及部分代码示例
创建一个类PrinterHelper.cs。类中来使用TSCLIB进行模版的编辑及调用打印机进行打印。在需要使用打印功能的地方调用打印方法
dll调用代码如下:
[DllImport("TSCLIB.dll", EntryPoint = "about")]
public static extern int about();
[DllImport("TSCLIB.dll", EntryPoint = "openport")]
public static extern int openport(string printername);
[DllImport("TSCLIB.dll", EntryPoint = "barcode")]
public static extern int barcode(string x, string y, string type,
string height, string readable, string rotation,
string narrow, string wide, string code);
[DllImport("TSCLIB.dll", EntryPoint = "clearbuffer")]
public static extern int clearbuffer();
[DllImport("TSCLIB.dll", EntryPoint = "closeport")]
public static extern int closeport();
[DllImport("TSCLIB.dll", EntryPoint = "downloadpcx")]
public static extern int downloadpcx(string filename, string image_name);
[DllImport("TSCLIB.dll", EntryPoint = "formfeed")]
public static extern int formfeed();
[DllImport("TSCLIB.dll", EntryPoint = "nobackfeed")]
public static extern int nobackfeed();
[DllImport("TSCLIB.dll", EntryPoint = "printerfont")]
public static extern int printerfont(string x, string y, string fonttype,
string rotation, string xmul, string ymul,
string text);
[DllImport("TSCLIB.dll", EntryPoint = "printlabel")]
public static extern int printlabel(string set, string copy);
[DllImport("TSCLIB.dll", EntryPoint = "sendBinaryData")]
public static extern int sendBinaryData(byte[] content, int length);
//功能:繪製QRCODE二維條碼
//語法:
//QRCODE X, Y, ECC Level, cell width, mode, rotation, [model, mask,]"Data string”
//參數說明
//X QRCODE條碼左上角X座標
//Y QRCODE條碼左上角Y座標
//ECC level 錯誤糾正能力等級
//L 7%
//M 15%
//Q 25%
//H 30%
//cell width 1~10
//mode 自動生成編碼/手動生成編碼
//A Auto
//M Manual
//rotation 順時針旋轉角度
//0 不旋轉
//90 順時針旋轉90度
//180 順時針旋轉180度
//270 順時針旋轉270度
//model 條碼生成樣式
//1 (預設), 原始版本
//2 擴大版本
//mask 範圍:0~8,預設7
//Data string 條碼資料內容
//string printercommand = "QRCODE 176,8,Q,8,A,0,M2,S7,\"" + barCode + "\"";
[DllImport("TSCLIB.dll", EntryPoint = "sendcommand")]
public static extern int sendcommand(string printercommand);
/// <summary>
/// 宽度、高度、速度、浓度
// sensor为0:vertical 垂直间距距离 offset垂直间距的偏移
// sensor为1:vertical定义黑标高度和额外送出长度 offset黑标偏移量
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="speed">列印速度,1~6,6为最快速度</param>
/// <param name="density">打印浓度,1-15,数字越大越黑</param>
/// <param name="sensor"></param>
/// <param name="vertical"></param>
/// <param name="offset"></param>
/// <returns></returns>
[DllImport("TSCLIB.dll", EntryPoint = "setup")]
public static extern int setup(string width, string height, string speed, string density, string sensor, string vertical, string offset);
/// <summary>
/// windowsfont(a,b,c,d,e,f,g,h)
/// </summary>
/// <param name="x">整数型别,文字X方向起始点,以点(point)表示。 </param>
/// <param name="y">整数型别,文字Y方向起始点,以点(point)表示。</param>
/// <param name="fontheight">整数型别,字体高度,以点(point)表示。 因为是用java改造过来的,所以字体大小需要用原本的java设置的大小*0.8571并向上取整取偶数IsEven该方法就是进行原大小*0.8571向上取整取偶数</param>
/// <param name="rotation">整数型别,旋转角度,逆时钟方向旋转 0 -> 0 degree 90-> 90 degree 180-> 180 degree 270-> 270 degree</param>
/// <param name="fontstyle">整数型别,字体外形 0->标准(Normal) 1->斜体(Italic) 2->粗体(Bold) 3->粗斜体(Bold and Italic)</param>
/// <param name="fontunderline">整数型别,底线 0->无底线 1->加底线</param>
/// <param name="szFaceName">字串型别,字体名称。如: Arial, Times new Roman,细名体,标楷体</param>
/// <param name="content">字串型别,列印文字内容。 </param>
/// <returns></returns>
[DllImport("TSCLIB.dll", EntryPoint = "windowsfont")]
public static extern int windowsfont(int x, int y, int fontheight,
int rotation, int fontstyle, int fontunderline,
string szFaceName, string content);
/// <summary>
/// 获取打印机状态,
/// </summary>
/// <returns></returns>
/// 0=待机中
/// 1=印字头开启
/// 2=卡纸
/// 3
/// 4 =缺纸
/// 10= 暂停中
/// 20 =列印中
[DllImport("TSCLIB.dll", EntryPoint = "usbportqueryprinter")]
public static extern int usbportqueryprinter();
模版及调用打印如下:
/// <summary>
///
/// </summary>
/// <param name="engineCode"></param>
/// <param name="mac"></param>
/// <param name="printContentInfo"></param>
/// <returns></returns>
public List<PrintResult> waimaoYbd1(string Code)
{
///打开打印机
openport("打印机名称");
setup("102", "152", "0", "15", "0", "2", "0");
///清除打印机缓存
clearbuffer();
sendcommand("DIRECTION 0");
//图片地址:物理路径,暂不清楚能否从远程服务器拿取图片
downloadpcx(DllPath + "wmfdj.pcx", "wmfdj.pcx");
sendcommand("PUTPCX 1,1,\"wmfdj.pcx\"");
windowsfont(118, 146, 45, 0, 0, 0, "Arial", "内容填充");
windowsfont(513, 194, 97, 0, 0, 0, "Helvetica-Roman-SemiB", "内容填充");
windowsfont(30, 356, 34, 0, 0, 0, "Arial", "内容填充");
windowsfont(30, 402, 34, 0, 0, 0, "Arial", "内容填充");
windowsfont(392, 466, 30, 0, 0, 0, "Arial", "内容填充");
windowsfont(392, 514, 30, 0, 0, 0, "Arial", "内容填充");
windowsfont(158, 797, 20, 0, 0, 0, "Arial", "内容填充");
windowsfont(447, 797, 20, 0, 0, 0, "Arial", "内容填充");
windowsfont(158, 837, 20, 0, 0, 0, "Arial","内容填充");
windowsfont(447, 837, 20, 0, 0, 0, "Arial", "内容填充");
windowsfont(158, 872, 20, 0, 0, 0, "Arial", "内容填充");
windowsfont(447, 872, 20, 0, 0, 0, "Arial", "内容填充");
barcode("58", "1000", "128", "65", "0", "0", "2", "4",Code);
windowsfont(58, 1070, 36, 0, 2, 0, "Arial", Code);
///二维码地址
string url = ACTIVE_URL + Code;
//此处"QRCODE 后边参数大小位置根据自行需求进行调整sendcommand包含各个参数介绍
string qrcode = "QRCODE 500,950,L,5,A,0,M2,S3,\"" + url + "\"";
sendcommand(qrcode);
///启动打印
printlabel("1", "1");
///关闭打印机
closeport();
}
最后总结
总结:画模版麻烦,DLL无法正常引用到项目。以上就是c#使用TSC打印机进行条码及二维码打印的详细记录。资源下载:https://download.csdn.net/download/qq_43486995/89368559
标签:打印机,string,c#,TSC,dll,int,windowsfont,TSCLIB,public From: https://blog.csdn.net/qq_43486995/article/details/139263002