使用Java中的power shell设置默认打印机

时间:2015-05-18 13:14:50

标签: java powershell printing

我有以下问题。我想使用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();
    }

我是否可以插入命令以设置默认打印机(按名称)?

1 个答案:

答案 0 :(得分:0)

是的,您可以添加这些代码以获取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";