gpt4 book ai didi

java - 在 Java 中强制目标打印机

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:09:52 24 4
gpt4 key购买 nike

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

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

谢谢

最佳答案

必须通过艰难的方式来解决这个问题,但为了子孙后代,这是我的一些代码:

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

关于java - 在 Java 中强制目标打印机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4177531/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com