【发布时间】:2016-05-20 08:56:24
【问题描述】:
我想打印一个文件。我为此编写了这段代码。但它不打印我的文件。 选择服务:
String printerName = "Canon MF4320-4350".toLowerCase();
PrintService service = null;
PrintService[] services = PrinterJob.lookupPrintServices();
// Retrieve a print service from the array
for (int index = 0; service == null && index < services.length; index++) {
if (services[index].getName().toLowerCase().indexOf(printerName) >= 0) {
service = services[index];
}
}
用于打印:
byte[] bytes = null;
Path path = FileSystems.getDefault().getPath("D:\\Test.docx");
bytes = Files.readAllBytes(path);
DocFlavor docFlavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
PrintRequestAttributeSet faset = new HashPrintRequestAttributeSet();
faset.add(new Copies(5));
faset.add(Sides.ONE_SIDED);
DocAttributeSet daset = new HashDocAttributeSet();
daset.add(OrientationRequested.LANDSCAPE);
daset.add(Sides.ONE_SIDED);
Doc myDoc = new SimpleDoc(bytes, docFlavor, daset);
//create the DocPrintJob
DocPrintJob job = service.createPrintJob();
job.print(myDoc, faset);
【问题讨论】: