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

  介绍

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

<强>第一种后台打印:,,,,

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

, PrintServiceLookup.lookupMultiDocPrintServices(口味,属性);

可执行代码如下:

public  static  void  main (String [], args), {   ,FileInputStream  textStream =,空;   ,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(文本,味道,,das);   ,//不显示打印对话框,直接进行打印工作   ,try  {   job.print才能(doc,普拉斯),,//,进行每一页的具体打印操作   ,}catch  (PrintException  pe), {   pe.printStackTrace才能();   ,}   ,}else  {   ,//如果打印内容为空时,提示用户打印将取消   ,JOptionPane.showConfirmDialog(空,   “对不起,才能,Printer  Job  is 空,,Print 取消了!“   “Empty",才能,JOptionPane.DEFAULT_OPTION,   JOptionPane.WARNING_MESSAGE才能);   ,}   以前,}

<强>第二种后台打印:

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

可执行代码如下:

public  class  JavaDemo  {   ,public  interface  TscLibDll  extends  Library  {   ,TscLibDll  INSTANCE =, (TscLibDll), Native.loadLibrary (“TSCLIB",, TscLibDll.class);   对(),int ;   ,int  openport (String  pirnterName);   ,int  closeport ();   ,int  sendcommand (String  printerCommand);   ,int 设置(String 宽度,String 高度,String 速度,String 密度,String 传感器,String 垂直,String 偏移量);   ,int  downloadpcx (String ,文件名,String  image_name);   ,int 条码(String  x,, String  y, String 类型,String 身高,,String 可读,,String 旋转,String 狭窄,String 宽,,String 代码);   ,int  printerfont (String  x,, String  y, String  fonttype,, String 旋转,String  xmul,, String  ymul,, String 文本);   ,int  clearbuffer ();   ,int  printlabel (String ,, String 复印件);   ,int 进纸键();   ,int  nobackfeed ();   ,int  windowsfont (int  x,, int  y, int  fontheight,, int 旋转,int  fontstyle,, int  fontunderline,, String  szFaceName,, String 内容);   ,}   ,   ,   ,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 日期());   ,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日,0,M1, S7, \““, +, qrCode +“\““,//打印二维码的参数和内容   ,TscLibDll.INSTANCE.sendcommand(命令);,//传送指令   ,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 ();   ,}   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

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