在 Java 中强制目标打印机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4177531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:10:51  来源:igfitidea点击:

Force target printer in Java

javaprinting

提问by jtnire

Is there a way to force the target printer in java, using HashPrintRequestAttributeSet ?

有没有办法在 java 中使用 HashPrintRequestAttributeSet 强制目标打印机?

I don't want the user to be able to change the printer in the printdialog

我不希望用户能够在打印对话框中更改打印机

Thanks

谢谢

回答by Indrek Ruubel

Had to figure this out the hard way, but for the future generations, here's some of my code:

不得不以艰难的方式解决这个问题,但对于后代,这是我的一些代码:

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);
}

回答by user3532430

My code to solve this :

我的代码来解决这个问题:

String printerNameDesired = "My Printer";

PrintService[] service = PrinterJob.lookupPrintServices(); // list of printers
DocPrintJob docPrintJob = null;

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();

回答by matthew

I just solved this problem by running cmd command in Java

我刚刚通过在 Java 中运行 cmd 命令解决了这个问题

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);
    }
}

And It's Wroked for Me :D

这对我来说很糟糕:D