我正在使用以下方法將PDF文檔打印到我的打印機。如果我只想打印一次,它工作正常,但打印多次需要更長的時間。將多個文檔賦予Java PrintService打印作業?
public static void print()
{
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintService[] services = PrintServiceLookup.lookupPrintServices(flavor, null);
FileInputStream psStream = null;
try
{
psStream = new FileInputStream(System.getProperty("user.home")+"\\My Documents\\document.txt");
}
catch (FileNotFoundException e)
{
System.out.println(e);
}
if (psStream == null)
return;
if (services.length > 0)
{
PrintService myService = null;
for (PrintService service : services)
{
System.out.println(service.getName());
if (service.getName().contains("printer_name"))
{
myService = service;
break;
}
}
DocPrintJob printJob = myService.createPrintJob();
Doc document = new SimpleDoc(psStream, flavor, null);
try
{
printJob.print(document, null);
} catch (PrintException e)
{
System.out.println(e);
}
} else
{
System.out.println("No PDF printer available.");
}
}
所以,如果我想打印文檔五次,我把print()
在一個循環,然後還有文檔的每個印刷之間的一個相當長的延遲。這種情況對我來說很有意義,因爲必須爲每個文檔重新建立與打印機的連接,並且必須重新發送文檔。有沒有什麼辦法可以使用這個API多次給打印作業提供相同的文檔?
我想這是值得注意的,我試過把printJob.print(document,null)
命令放在一個產生PrintException: already printing
的循環中。也許如果有辦法讓它在發送下一個文件之前等待當前文件完成,它可以工作?在這裏輸了。謝謝。
@mthmulders:我試了下面,只打印了一份。我做錯了什麼?
Doc document = new SimpleDoc(psStream, flavor, null);
PrintRequestAttributeSet set = new HashPrintRequestAttributeSet();
set.add(new Copies(5));
try
{
printJob.print(document, set);
} catch (PrintException e)
{
System.out.println(e);
}
請參閱對原始問題的編輯。 – ericjv
Javadoc說:「在許多設備上,支持的分類副本數量將受到設備上物理輸出分檔數量的限制,並且可能與可支持的未分卷副本數量不同。」不知道爲什麼,但也許這是解釋? – mthmulders