次の方法を使用して、pdf ドキュメントをプリンターに印刷しています。何かを 1 回だけ印刷したい場合は問題なく動作しますが、何かを複数回印刷するには時間がかかります。
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.");
}
}
そのため、ドキュメントを 5 回印刷する必要がprint()
あり、ループを挿入すると、ドキュメントの各印刷の間にかなり長い遅延が発生します。ドキュメントごとにプリンタへの接続を再確立する必要があり、ドキュメントを再送信する必要があるため、この種のことは私には理にかなっています。この API を使用して印刷ジョブに同じドキュメントを複数回与える方法はありますか?
printJob.print(document,null)
を生成したループにコマンドを入れようとしたことは注目に値すると思いますPrintException: already printing
。次のドキュメントを送信する前に、現在のドキュメントが完了するまで待機させる方法があれば、うまくいくでしょうか? ここで途方にくれました。ありがとう。
@mthmulders: 以下を試してみましたが、1 部だけ印刷されました。私が間違ったことはありますか?
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);
}