[英]How to print out any text content at client side by java or javascript?
i have a web application which is coded in Java. 我有一个用Java编码的Web应用程序。 I need to print out a text content(not a web page) at client side by choosing the printer. 我需要通过选择打印机在客户端打印出文本内容(而不是网页)。 I can do this at server side with Java but how should i overcome at client side? 我可以在服务器端使用Java来做到这一点,但是在客户端应该如何克服呢?
Should i prefer javascript or applet? 我应该使用javascript还是applet? And can i have the solution choose a printer among all printers? 我可以让解决方案在所有打印机中选择一个打印机吗?
Thanks in advance... 提前致谢...
Printer selection is in the OS. 打印机选择在OS中。 I don't know if you can do it with Java, but I know for fact you can't do it in JavaScript. 我不知道您是否可以使用Java来做到这一点,但是我知道您实际上无法在JavaScript中做到这一点。
You can use applet for client side printing.` 您可以使用小程序进行客户端打印。
public void paint(Graphics g) {
TextArea display = new TextArea(1, 80);
try {
PrintService printService = getPrintService("printerName");
if(printService == null)
printService = PrintServiceLookup
.lookupDefaultPrintService();
printData(printService , "Printing text");
g.drawString(
" \n The Print was Successfull.. ",
10, 10);
} catch (Exception e) {
System.out.println("Exception was thrown. Exception is \t : " + e);
}
}
Print text to selected print device 将文本打印到选定的打印设备
private boolean printData(PrintService printService , String printText) {
try {
SimpleDoc doc;
doc = new SimpleDoc(printText.getBytes(),
javax.print.DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
DocPrintJob job = printService.createPrintJob();
job.print(doc, new HashPrintRequestAttributeSet());
System.out.println("Job sent to printer succesfully");
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
This code for choose a printer among all printers 该代码用于在所有打印机中选择一个打印机
private PrintService getPrintService(String name) {
PrintService pService = null;
if (name == null || name.trim().length() == 0)
return null;
PrintService pServices[] = PrintServiceLookup.lookupPrintServices(null, null);;
int i = 0;
do {
if (i >= pServices.length)
break;
String pServiceName = pServices[i].getName();
if (name.equalsIgnoreCase(pServiceName)) {
pService = pServices[i];
break;
}
i++;
} while (true);
return pService;
}`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.