9
A
回答
11
只好艱難地算出來,但對於後人,這裏是我的一些 的代碼:
PrintService[] printServices;
PrintService printService;
PageFormat pageFormat;
String printerName = "Your printer name in Devices and Printers";
PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();
printServiceAttributeSet.add(new PrinterName(printerName, null));
printServices = PrintServiceLookup.lookupPrintServices(null, printServiceAttributeSet);
pageFormat = new PageFormat(); // If you want to adjust heigh and width etc. of your paper.
pageFormat = printerjob.defaultPage();
PrinterJob printerjob = PrinterJob.getPrinterJob();
printerjob.setPrintable(new Server(), pageFormat); // Server was my class's name, you use yours.
try {
printService = printServices[0];
printerjob.setPrintService(printService); // Try setting the printer you want
} catch (ArrayIndexOutOfBoundsException e) {
System.err.println("Error: No printer named '" + printerName + "', using default printer.");
pageFormat = printerjob.defaultPage(); // Set the default printer instead.
} catch (PrinterException exception) {
System.err.println("Printing error: " + exception);
}
try {
printerjob.print(); // Actual print command
} catch (PrinterException exception) {
System.err.println("Printing error: " + exception);
}
7
我的代碼來解決這個問題:
String printerNameDesired = "My Printer";
PrintService[] service = PrinterJob.lookupPrintServices(); // list of printers
int count = service.length;
for (int i = 0; i < count; i++) {
if (service[i].getName().equalsIgnoreCase(printerNameDesired)) {
docPrintJob = service[i].createPrintJob();
i = count;
}
}
PrinterJob pjob = PrinterJob.getPrinterJob();
pjob.setPrintService(docPrintJob.getPrintService());
pjob.setJobName("job");
pjob.print();
+2
這一個爲我工作,只有你忘了初始化docPrintJob。我建議添加以下代碼以使其功能完整:'DocPrintJob docPrintJob = null;''PrintService [] service = PrinterJob.lookupPrintServices();''之後''。 – bashoogzaad 2014-11-13 11:28:21
+0
更新時間:http://stackoverflow.com/q/35535589/285594 – YumYumYum 2016-02-21 12:28:47
0
我只是在Java中
static void changeWindowsDefaultPrinter(String printerName) {
String cmdLine = String.format("RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n \"%s\"", printerName);
ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", cmdLine);
builder.redirectErrorStream(true);
Process p = null;
try { p = builder.start(); }
catch (IOException e) { e.printStackTrace(); }
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = new String();
while (true) {
try {
line = r.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (line == null) { break; }
System.out.println("result " + line);
}
}
運行cmd命令解決了這個問題,它的Wroked對我來說:d
相關問題
- 1. 在ZEBRA標籤打印機中打印
- 2. 在Java中打印到標籤打印機
- 3. 以編程方式強制打印機打印副本
- 4. 僅在打印機中打印標籤,而不在C#中打印標籤
- 5. 用Java打印到不是默認打印機的標籤打印機
- 6. 在法律文件中強制打印
- 7. Java打印機api
- 8. SSRS強制單面打印
- 9. Java打印 - 在少量打印機上放大打印輸出
- 10. 打印的熱敏打印機的Java
- 11. Java - 打印到收據打印機
- 12. 打印機控制語言 - 打印圖像x座標
- 13. 在java中獲取打印機ID
- 14. 從Python打印到標準打印機?
- 15. 可以強制打印機設置(紙張大小)在JavaScript?
- 16. 如何在javafx中的PrinterJob中設置目標打印機
- 17. 在打印機中停止紙張 - Java打印問題
- 18. 如何在Java中打印收據打印機的支票?
- 19. 使用java在網絡打印機中打印pdf
- 20. 在java中使用熱敏打印機打印收件人
- 21. 使用java applet在打印機中打印渲染的網頁
- 22. 如何使用打印機API在JAVA中打印用於打印的文本?
- 23. Java:在強制執行程序之前打印消息
- 24. 在PHP中打印到打印機
- 25. 在愛普生打印機中打印
- 26. p:打印機中的p:tabView目標特定標籤
- 27. 強制打開在Chrome中打印的詳細信息/摘要標記
- 28. java sql隨機行打印
- 29. 獲取Java打印機GraphicsDevice
- 30. Java與點陣打印機
爲什麼........... – 2010-11-14 13:06:01
沒有用戶....? ... – Phil 2014-01-29 05:47:30