在 Java 中使用电源 shell 设置默认打印机
Set default printer using power shell in Java
我有以下问题。我想在 Java 中使用 power shell 命令自动打印我的 docx 输出。
到目前为止,我正在使用这个片段来打印
private static void printOut() throws IOException {
Runtime runtime = Runtime.getRuntime();
String command = "powershell Start-Process –FilePath \"" + yourDocXPath + "\" –Verb Print";
System.out.println(command);
Process proc = runtime.exec(command);
InputStream is = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(isr);
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
proc.getOutputStream().close();
}
是否有我可以插入的命令来设置默认打印机(按名称)?
是的,您可以添加这些代码以获得 java 中的默认打印机:
PrintService defaultPrinter = PrintServiceLookup.lookupDefaultPrintService();
String printerName = defaultPrinter.getName();
编辑:
使用此 powershell 命令设置打印机并打印:
String command = "powershell Set-Printer –Name \"" + printerName + "\" -KeepPrintedJobs $true; powershell Start-Process –FilePath \"" + yourDocXPath + "\" –Verb Print";
我有以下问题。我想在 Java 中使用 power shell 命令自动打印我的 docx 输出。
到目前为止,我正在使用这个片段来打印
private static void printOut() throws IOException {
Runtime runtime = Runtime.getRuntime();
String command = "powershell Start-Process –FilePath \"" + yourDocXPath + "\" –Verb Print";
System.out.println(command);
Process proc = runtime.exec(command);
InputStream is = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(isr);
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
proc.getOutputStream().close();
}
是否有我可以插入的命令来设置默认打印机(按名称)?
是的,您可以添加这些代码以获得 java 中的默认打印机:
PrintService defaultPrinter = PrintServiceLookup.lookupDefaultPrintService();
String printerName = defaultPrinter.getName();
编辑:
使用此 powershell 命令设置打印机并打印:
String command = "powershell Set-Printer –Name \"" + printerName + "\" -KeepPrintedJobs $true; powershell Start-Process –FilePath \"" + yourDocXPath + "\" –Verb Print";