- 论坛徽章:
- 0
|
you mean your code can't find the network printer?
java support printing to network printer,because the print service is invoke the OS native
service
try the following code
- import java.io.*;
- import javax.print.*;
- import javax.print.attribute.*;
- public class printTest
- {
- public static void main(String[] args) throws Exception
- {
-
- String text = args[0];
- DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
- HashPrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
- PrintService printService[] =
- PrintServiceLookup.lookupPrintServices(flavor, null);
- PrintService defaultService =
- PrintServiceLookup.lookupDefaultPrintService();
- PrintService service = ServiceUI.printDialog(null, 500, 300,
- printService, defaultService, null, pras);
- if (service != null) {
- text += (char) 12;
- InputStream stream = new ByteArrayInputStream(text.getBytes());
- DocPrintJob job = service.createPrintJob();
- Doc doc = new SimpleDoc(stream, flavor, new HashDocAttributeSet());
- try {
- job.print(doc, pras);
- } catch (PrintException ex) {
- ex.printStackTrace();
- }
- try {
- stream.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }else{
- System.out.println("can't find the printer");
- }
- }
- }
复制代码 |
|