Force target printer in Java

Is there any way to force the target printer to use hashprintrequestattributeset in Java?

I don't want users to be able to change printers in the printdialog

thank you

Solution

I have to think about it seriously, but for future generations, there are some problems

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);
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>