[英]Connecting and printing to a printer in Java
Is there an easy way in Java to do the following? Java 中是否有一种简单的方法可以执行以下操作?
Some quick hints:一些快速提示:
print from java: see A Basic Printing Program从 java 打印:请参阅基本打印程序
status of printing job: you might be able to get something useful by using a PrintJobListener :打印作业的状态:您可以通过使用PrintJobListener获得一些有用的信息:
Implementations of this listener interface should be attached to a DocPrintJob to monitor the status of the printer job.此侦听器接口的实现应附加到 DocPrintJob 以监视打印机作业的状态。 These callback methods may be invoked on the thread processing the print job, or a service created notification thread.可以在处理打印作业的线程或服务创建的通知线程上调用这些回调方法。 In either case the client should not perform lengthy processing in these callbacks.在任何一种情况下,客户端都不应该在这些回调中执行冗长的处理。
A very good printing tutorial: http://download.oracle.com/javase/tutorial/2d/printing/index.html很不错的打印教程: http://download.oracle.com/javase/tutorial/2d/printing/index.html
Also check answers to my question about printers, the Printer Job API is what are you looking for, but checking this out will also help:还要检查我关于打印机的问题的答案,打印机作业 API 是你要找的,但检查一下也有帮助:
How to Send JTable data to Print Job from Java Application? 如何从 Java 应用程序将 JTable 数据发送到打印作业?
A simple java program to get byte Array printed一个简单的 java 程序来打印字节数组
public class Main {
public static void main(String[] args) throws IOException, InterruptedException {
String str = "ABCGVDVJHBDKDLNJOKBUHODVWFCVDHGSBDKS";
byte[] byteArr = str.getBytes();
ByteArrayInputStream fis = new ByteArrayInputStream(byteArr);
String printerID; // = give printer ID here
System.out.println("printerID"+printerID);
String command = "lp -d " + printerID;
Process child = Runtime.getRuntime().exec(command);
OutputStream childOut = child.getOutputStream();
byte[] buffer = new byte[100000000];
int bytesRead;
while ((bytesRead = fis.read(buffer)) > 0)
{
childOut.write(buffer, 0, bytesRead);
}
childOut.close();
int exitVal = child.waitFor();
InputStream childIn = child.getInputStream();
BufferedReader is = new BufferedReader(new InputStreamReader(childIn));
String line;
boolean retval;
while ((line = is.readLine()) != null)
{
String finalLine = line;
}
childIn.close();
if (exitVal == 0)
{
retval = true;
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.