0 0

java如何获取客户端打印机 求教10

我的代码如下,但是只是获取服务端的打印机,如何获取客户端的打印机,求解。。。。
public static int print(QuickMarkPrintUtil qp,PaperSetting paper) {
		// ReadData();
		// 通俗理解就是书、文档
		Book book = new Book();
		// 设置成竖打
		PageFormat pf = new PageFormat();
		pf.setOrientation(PageFormat.PORTRAIT); // LANDSCAPE表示竖打;PORTRAIT表示横打;REVERSE_LANDSCAPE表示打印空白
		// 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
		Paper p = new Paper();
		if (paper != null) {
			p.setSize(Double.valueOf(paper.getP_paper_width()) * 72 / 25.4,
					Double.valueOf(paper.getP_pager_height()) * 72 / 25.4);
			p.setImageableArea(
					Double.valueOf(paper.getP_image_x()) * 72 / 25.4,
					Double.valueOf(paper.getP_image_y()) * 72 / 25.4,
					Double.valueOf(paper.getP_image_able_area_width()) * 72 / 25.4,
					Double.valueOf(paper.getP_code_image_height()) * 72 / 25.4);
		} else {
			p.setSize(240, 181); // 纸张大小(590, 840)表示A4纸
			// p.setImageableArea(70, 40, 125, 160); // A4(595 X
			// 842)设置打印区域,其实0,0应该是72,72
			// ,因为A4纸的默认X,Y边距是72
			p.setImageableArea(40, 40, 150, 170);
		}

		pf.setPaper(p);
		// 把 PageFormat 和 Printable 添加到书中,组成一个页面
		book.append(qp, pf);
		// 查找打印机==[color=red]这里只能获取服务端的打印机列表,如何获取客户端的 求指教啊[/color]
		PrintService pss[] = PrinterJob.lookupPrintServices();
		PrintService ps = null;
		if (pss == null) {
			LogerUtil.info("no PrintService in this computer");
			return 0;
		}
		for (int i = 0; i < pss.length; i++) {
			if ("TSC TTP-244 Plus".equals(pss[i].getName())) {
				ps = pss[i];
				break;
			}
		}
		if (ps == null) {
			LogerUtil
					.info("no available PrintService[TSC TTP-244 Plus] in this computer");
			return 0;
		}
		// 获取打印服务对象
		PrinterJob job = PrinterJob.getPrinterJob();
		// 设置打印类
		job.setPageable(book);

		try {
			// 可以用printDialog显示打印对话框,在用户确认后打印;也可以直接打印
			// boolean a = job.printDialog(); 
			// if (a) {
			job.print();
			return 1;
			// }else{
			//	 return 2;
			// }
		} catch (PrinterException e) {
			e.printStackTrace();
			return 0;
		}
	}

问题补充
没人帮忙回答下吗?
2013年6月03日 15:28

2个答案 按时间排序 按投票排序

0 0

Server端后台代码实现的打印,当然只能查找server所在的网络打印机咯。Server端最多知道client端IP相关的信息,要操作client端的事情,只能以此为延伸想办法吧。

2013年6月03日 16:14
0 0

首先,你这段代码应该是运行于web容器上,所以你的代码自然获取到的是服务端打印机。如果使用客户端打印机,可以使用浏览器的打印控件。下面给出几个链接,供你参考:
http://blog.sina.com.cn/s/blog_4c495b5f01011jgx.html
代码段为:

<OBJECT id=WebBrowser classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 width=0 VIEWASTEXT></OBJECT>
<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td align="center">
<input   type=button   value=打印       onclick= "document.all.WebBrowser.ExecWB(6,1) "   class= "NOPRINT "> 
<input   type=button   value=直接打印   onclick= "document.all.WebBrowser.ExecWB(6,6) "   class= "NOPRINT "> 
<input   type=button   value=页面设置   onclick= "document.all.WebBrowser.ExecWB(8,1) "   class= "NOPRINT "> 
<input   type=button   value=打印预览   onclick= "document.all.WebBrowser.ExecWB(7,1) "   class= "NOPRINT ">
</td>
</tr>
</table>

2013年6月03日 16:00

相关推荐

Global site tag (gtag.js) - Google Analytics