我有两段用Java打印的代码,如下所示:
第一段代码:
这段代码有一个打印对话框,并且可以按照预期在打印机上打印。 第二段代码:
第一段代码:
for(int i = 0; i < files.length; i++) {
String file = "C:\\images\\colour\\"+files[i].getName();
String filename = file;
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, DocFlavor.INPUT_STREAM.GIF, pras);
if (service != null) {
DocPrintJob job = service.createPrintJob();
PrintJobListener listener = new PrintJobAdapter() {
public void printDataTransferCompleted(PrintJobEvent e) {
System.exit(0);
}
};
job.addPrintJobListener(listener);
FileInputStream fis = new FileInputStream(filename);
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das);
job.print(doc, pras);
}
}
这段代码有一个打印对话框,并且可以按照预期在打印机上打印。 第二段代码:
try {
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(1));
PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.GIF, pras);
if (pss.length == 0)
throw new RuntimeException("No printer services available.");
PrintService ps = pss[3];
System.out.println("Printing to " + ps);
DocPrintJob job = ps.createPrintJob();
FileInputStream fin = new FileInputStream(files[i]);
Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.GIF, null);
job.print(doc, pras);
fin.close();
}
catch (IOException ie) {
ie.printStackTrace();
}
catch (PrintException pe) {
pe.printStackTrace();
}
打印不带打印对话框,这就是我想要的,但这会将一个空白页面打印到打印机上。
现在我的目标是只使用其中一个代码,但我已经提供了我尝试过的内容。我需要使代码1工作,但没有printerDialog。
如果我从代码1中删除printerDialog,那么基本上它与代码2相同(在此打印机上打印空白)。
我认为问题出在从PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, DocFlavor.INPUT_STREAM.GIF, pras);
传递给代码一的参数不再被传递了
有没有办法将PrintService服务= ServiceUI.printDialog(null、200、200、printService、defaultService、DocFlavor.INPUT_STREAM.GIF、pras)中的参数传递到打印机而不使用对话框,或者有没有一种方式可以跳过对话框,就像用户点击了“是”一样?
首先非常抱歉发了这么长的帖子。希望任何人能帮助我或给我一些建议。谢谢您提前。