温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java调用TSC打印机进行打印的方法

发布时间:2021-02-07 14:45:48 来源:亿速云 阅读:811 作者:小新 栏目:编程语言

这篇文章主要介绍Java调用TSC打印机进行打印的方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

第一种后台打印:    

使用javax中的PrintServiceLookup类进行打印,可以直接调用默认的打印机,也可以使用下列的方法进行筛选打印: 

 PrintServiceLookup.lookupMultiDocPrintServices(flavors, attributes);

可执行代码如下:

public static void main(String[] args) {
 FileInputStream textStream = null;
 try {
 textStream = new FileInputStream("地址");
 } catch (FileNotFoundException e) {
 e.printStackTrace();
 } 
 if (textStream != null) // 当打印内容不为空时
 {
 // 指定打印输出格式
 DocFlavor flavor = DocFlavor.INPUT_STREAM.PDF;//SERVICE_FORMATTED.PRINTABLE
 // 定位默认的打印服务
 PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
 // 创建打印作业
 DocPrintJob job = printService.createPrintJob();
 // 设置打印属性
 PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
 // 设置纸张大小,也可以新建MediaSize类来自定义大小
 pras.add(MediaSizeName.ISO_A4);
 DocAttributeSet das = new HashDocAttributeSet();
 // 指定打印内容
 Doc doc = new SimpleDoc(textStream, flavor, das);
 // 不显示打印对话框,直接进行打印工作
 try {
  job.print(doc, pras); // 进行每一页的具体打印操作
 } catch (PrintException pe) {
  pe.printStackTrace();
 }
 } else {
 // 如果打印内容为空时,提示用户打印将取消
 JOptionPane.showConfirmDialog(null,
  "Sorry, Printer Job is Empty, Print Cancelled!",
  "Empty", JOptionPane.DEFAULT_OPTION,
  JOptionPane.WARNING_MESSAGE);
 }
 }

第二种后台打印:

注意:第二种跟第三种打印使用的是打印机的命令进行操作,这里需要jna的jar包,还有jdk要求是32位的,并且要要注册对应的dll,对应不同的系统要在不同的Windows下进行注册DLL,注册成功之后需要win+R ,调用并运行  Regsvr32.exe TSCActiveX.dll 指令

可执行代码如下:

public class JavaDemo {
 public interface TscLibDll extends Library {
 TscLibDll INSTANCE = (TscLibDll) Native.loadLibrary("TSCLIB", TscLibDll.class);
 int about();
 int openport(String pirnterName);
 int closeport();
 int sendcommand(String printerCommand);
 int setup(String width, String height, String speed, String density, String sensor, String vertical, String offset);
 int downloadpcx(String filename, String image_name);
 int barcode(String x, String y, String type, String height, String readable, String rotation, String narrow, String wide, String code);
 int printerfont(String x, String y, String fonttype, String rotation, String xmul, String ymul, String text);
 int clearbuffer();
 int printlabel(String set, String copy);
 int formfeed();
 int nobackfeed();
 int windowsfont(int x, int y, int fontheight, int rotation, int fontstyle, int fontunderline, String szFaceName, String content);
 }
 
 
 public static void main(String[] args) {
 System.setProperty("jna.encoding", "GBK");// 支持中文
 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 String time = df.format(new Date());
 String qrCode = "PD102011";
 TscLibDll.INSTANCE.openport("TSC TTP-244 Pro");//打印机型号
 TscLibDll.INSTANCE.setup("70","40","5","8","0","2","0");
 TscLibDll.INSTANCE.clearbuffer();
 String command = "QRCODE 120,90,Q,8,A,0,M1,S7,\"" + qrCode+"\""; //打印二维码的参数和内容
 TscLibDll.INSTANCE.sendcommand(command); //传送指令
 TscLibDll.INSTANCE.sendcommand("TEXT 300 70 36 0 0 0 arial"+ "办公耗材-标签纸");
 //TscLibDll.INSTANCE.windowsfont(300, 70, 36, 0, 0, 0, "arial", "办公耗材-标签纸");
 TscLibDll.INSTANCE.printlabel("1", "1");
 TscLibDll.INSTANCE.closeport();
 }
}

注意:这种打印方式,需要USB连接(ttp-244 pro),如果想在线生成,远程连接,需要使用第三种方式

第三种JS打印

在使用JS进行打印的时候要主要浏览器的限制,目前只有IE浏览器进行支持,在进行打印时,控制台出现Automation 服务器不能创建对象的,请调节浏览器的安全中的自定义级别中的ActiveX中相关的设置为启用,代码如下:

<script type="text/javascript">
 
function printQRcode(){
 
 var TSCObj = new ActiveXObject("TSCActiveX.TSCLIB");//打印机使用的dll
 //TSCObj.ActiveXabout(); // 打印机信息
 TSCObj.ActiveXopenport ("TSC TTP-244 Pro");//打印机名称,不能写错
 //TSCObj.ActiveXdownloadpcx ("D:/myeclipseproject/filesManager/trunk/doc/02-参考资料/03-二维码打印机/01-开发帮助/PHP调用范例/TSCActiveX.DLL-PHP-Example/","123.PCX");
 TSCObj.ActiveXsetup("70","40","4","15","0","2.5","0");//打印机设置
 //TSCObj.ActiveXsetup("标签宽度","标签高度","打印速度","打印浓度(0-15)","感应器类别字串型,0 表示使用垂直間距感測器(gap sensor), 1 表示使用黑標感測器(black mark senso)","Gap/Black mark垂直间距(mm)","Gap/Black mark偏移距离(mm)");
 //TSCObj.ActiveXformfeed();
 //TSCObj.ActiveXnobackfeed();
 TSCObj.ActiveXsendcommand("DIRECTION 1"); //设置标签方向 ,DIRECTION 1 左上角 (x,y)={0,0};DIRECTION 0 右下角 (x,y)={0,0};
 TSCObj.ActiveXsendcommand ("SET TEAR ON");
 TSCObj.ActiveXclearbuffer();
 //TSCObj.ActiveXsendcommand ("PUTPCX 0,0,\"123.PCX\"");
 /*
 1、ActiveXprinterfont采用机器内置编码通常用来打英文。
 2、ActiveXwindowsfont可以输出汉字,但是必须是系统中存在的字体。
 TSCObj.ActiveXprinterfont ("a","b","c","d","e","f","g");
 a:字符串,文字X方向起始点,以点表示。
 b:字符串,文字Y方向起始点,以点表示。
 c:內建字型名称,共12种(1: 8*12 dots 2: 12*20 dots 3: 16*24 dots 4: 24*32 dots 5: 32*48 dots TST24.BF2: 繁體中文 24*24 TST16.BF2: 繁體中文 16*16 TTT24.BF2: 繁體中文 24*24 (電信碼) TSS24.BF2: 簡體中文 24*24 TSS16.BF2: 簡體中文 16*16 K: 韓文 24*24 L: 韓文 16*16 )
 d:字符串,旋转角度
 e:字符串,X方向放大倍率1-8
 f:字符串,Y方向放大倍率1-8
 g:字符串,打印内容
 ActiveXwindowsfont(a,b,c,d,e,f,g,h)
 说明:使用Windows TTF字体打印文字。
 参数:
 a:整数类型,文字X方向起始点,以点表示。
 b:整数类型,文字Y方向起始点,以点表示。
 c:整数类型,字体高度,以点表示。
 d:整数类型,旋转角度,逆时针方向旋转。0-旋转0°,90-旋转90°,180-旋转180°,270-旋转270°。
 e:整数类型,字体外形。0:标签;1:斜体;2:粗体;3:粗斜体。
 f:整数类型,下划线,0:无下划线;1:加下划线。
 g:字符串类型,字体名称。如:Arial,Times new Roman。
 h:字符串类型,打印文字内容。
 */
// TSCObj.ActiveXwindowsfont (500, 200, 48, 90, 0, 0, "Arial", "\u7f16\u7801");
 TSCObj.ActiveXwindowsfont (260, 60, 60, 0, 2, 0, "Arial", "XX专用");
 
 //var cmd = 'QRCODE 条码X方向起始点,条码Y方向起始点,纠错级别,二维码高度,A(A和M),旋转角度,M2(分为类型1和类型2),S1 (s1-s8,默认s7),\"1231你好2421341325454353\"';
 var cmd = 'QRCODE 80,80,H,7,A,0,M2,S1,\"'+"123456789"+'\"';
 
 TSCObj.ActiveXsendcommand(cmd);
 TSCObj.ActiveXwindowsfont (280, 150, 40, 0, 0, 0, "Arial", "123456789");
 TSCObj.ActiveXwindowsfont (180, 260, 30, 0, 0, 0, "Arial", "XXXXXXXX");
 TSCObj.ActiveXprintlabel ("1","1");//(打印份数,每页打印张数)
 TSCObj.ActiveXclearbuffer();//清除
 TSCObj.ActiveXcloseport();//关闭打印端口
}
</script>

以上是“Java调用TSC打印机进行打印的方法”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI