iTextSharp
プロジェクトのWindowsアプリケーションからdllを使用してpdfを印刷しようとしています..
しかし、今まで使ってきた方法は信頼できそうにありません。(効く時と効かない時がある)
プロセス全体を Process クラスに結合し、次のコードを記述しています。
printProcess = new Process[noOfCopies];
// Print number of copies specified in configuration file
for (int counter = 0; counter < noOfCopies; counter++)
{
printProcess[counter] = new Process();
// Set the process information
printProcess[counter].StartInfo = new ProcessStartInfo()
{
CreateNoWindow = true,
Verb = "Print",
FileName = pdfFilePath,
ErrorDialog = false,
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = true,
};
// Start the printing process and wait for exit for 7 seconds
printProcess[counter].Start();
// printProcess[counter].WaitForInputIdle(waitTimeForPrintOut);
printProcess[counter].WaitForExit(waitTimeForPrintOut); //<--**This is line for discussion**
if (!printProcess[counter].HasExited)
{
printProcess[counter].Kill();
}
}
// Delete the file before showing any message for security reason
File.Delete(pdfFilePath);
問題は、pdf が開いていない場合、プロセスが正常に動作することです...(うまく印刷されます)。ただし、pdf が開いている場合、WaitForExit(..)
メソッドはプロセスが終了するのを待ちません。したがって、プロセスも実行されます。高速で、印刷後にファイルを削除しているため、レポートを印刷するためのカウンター(回数..)が複数回ある場合、エラーが発生します..
プロセスを遅くすることもありTimer
ましたが、うまくいきません。理由はわかりません。Sleepコマンドも使用しました..しかし、それはメインスレッドをスリープ状態にするため、私にも適していません。
そうするための本当に信頼できる方法を私に提案してください..:)