1

我在java中有一个打印程序,但由于某种原因它总是选择默认打印机(8600),即使我为打印机传递了一个xps的值,我做错了什么,我怎样才能让它选择所需的打印机

这是我的代码:

 PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
        System.out.println("Number of print services: " + printServices.length); //tells me how many print services are isntalled on the server.

        for (PrintService printer2 : printServices) {
            System.out.println("Printer: " + printer2.getName()); //prints out the names of all printers on the server, testing purposes 
        } 


        PrintUtility.findPrintService(printer); // selects only the 8600 printer // EDIT now selects any printer with the name provided


        PrintService[] services =
                PrintServiceLookup.lookupPrintServices(psInFormat, null);
        //System.out.println("Printers avialiable are " + services);
        System.out.println("Printer Selected " + services[Printerinx]);

        //PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();

        DocFlavor[] docFalvor = services[Printerinx].getSupportedDocFlavors();
        for (int i = 0; i < docFalvor.length; i++) {
            System.out.println(docFalvor[i].getMimeType());
        }
        if (services.length > 0) {
            DocPrintJob job = services[Printerinx].createPrintJob();
            try {
                job.print(myDoc, aset);
                System.out.print("Printing Doc");
            } catch (PrintException pe) {
                System.out.print(pe);
            }
        }

这是控制台:

INFO: Number of print services: 7
INFO: Printer: Send To OneNote 2013
INFO: Printer: Microsoft XPS Document Writer
INFO: Printer: HP988FD1 (HP Officejet Pro 8600)
INFO: Printer: HP Officejet Pro 8600 (Network)
INFO: Printer: Fax - HP Officejet Pro 8600 (Network)
INFO: Printer: Fax
INFO: Printer: Adobe PDF
INFO: Printer Selected Win32 Printer : HP Officejet Pro 8600 (Network)
INFO: image/gif

谢谢

4

1 回答 1

1

好的,printinx,我已经选择了可以使用的打印机,0 表示第一台等,我已经将它设置为 printinx = "3" 所以它总是选择第 4 台打印机,这已被删除,并且每台打印机都给出了正确的 printinx,现在它应该正常工作

于 2013-03-07T17:07:40.343 回答