简体   繁体   中英

How to print to Windows USB printer from Java?

I have a kiosk application that runs under Windows. I would like to be able to print to a Windows printer connected via USB from a Java app. I can't use the JavaCOMM library as the printer is not serial or USB. I've read about the Java print API but it looks like it only prints images, I would like to be able to send raw text to the printer.

The Java Print API can do much more as just print images. Have a look at the SDK Printing tutorial , there is everything you need.

What about this, you just provide a char array with your chars:

char[] printdata = "hello world\n".toCharArray();
DocFlavor flavor = DocFlavor.CHAR_ARRAY.TEXT_PLAIN;
PrintService pservice = PrintServiceLookup.lookupDefaultPrintService();
DocPrintJob pjob = pservice.createPrintJob();
Doc doc= new SimpleDoc(printdata, flavor, null);
job.print(doc, null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM