始终选择默认打印机而不是指定的打印机JAVA

时间:2013-03-07 15:13:08

标签: java printing

我在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

由于

1 个答案:

答案 0 :(得分:1)

好的解决了,打印机,我已经选择了哪个打印机可以使用,0个用于第一个等等,我把它设置为printinx =&#34; 3&#34;所以它总是选择第四台打印机,这已被删除,每台打印机都给出了正确的printinx,现在它的工作原理应该