Windowsフォームアプリでレポートを印刷する際、印刷処理の終了を判断する方法は?
対象製品
ActiveReports for .NET 9.0J
詳細
印刷処理の完了を判断する方法としては、.NET FrameworkのSystem.Drawing.Printing.PrintDocumentクラスから継承される、PageDocumentまたはSectionDocumentクラスのPrinterプロパティのEndPrintイベントを利用する方法が考えられます。
なお、EndPrintイベントを使用するには、イベントハンドラを関連付ける必要がありますが、具体的な実装方法は、レポートの形式によって異なります。
◆サンプルコード(C#)
◆サンプルコード(C#)
なお、上記のような処理で、「ActiveReports側の印刷処理が完了したかどうか」を判断できますが、これは「ActiveReportsから(プリンタスプーラなどへの)印刷データの出力が完了したかどうか」という判断です。
実際に「プリンタ側で印刷が行われたかどうか」(プリンタ側での印刷完了やエラー)を判断する機能は、ActiveReportsには用意されておりません。
なお、EndPrintイベントを使用するには、イベントハンドラを関連付ける必要がありますが、具体的な実装方法は、レポートの形式によって異なります。
セクションレポートの場合
◆サンプルコード(VB.NET)Private Sub Form1_Load(...) Handles MyBase.Load
' セクションレポートをViewerに表示します。
Dim sectionReport As New SectionReport1
sectionReport.Run()
Dim sectionDocument As GrapeCity.ActiveReports.Document.SectionDocument
sectionDocument = sectionReport.Document
Me.Viewer1.LoadDocument(sectionDocument)
' EndPrintイベントをイベントハンドラに関連付けます。
AddHandler sectionDocument.Printer.EndPrint, AddressOf Me.onEndPrint
End Sub
' 印刷処理の終了時に発生するイベント
Private Sub onEndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs)
Console.WriteLine("印刷完了")
End Sub
' セクションレポートをViewerに表示します。
Dim sectionReport As New SectionReport1
sectionReport.Run()
Dim sectionDocument As GrapeCity.ActiveReports.Document.SectionDocument
sectionDocument = sectionReport.Document
Me.Viewer1.LoadDocument(sectionDocument)
' EndPrintイベントをイベントハンドラに関連付けます。
AddHandler sectionDocument.Printer.EndPrint, AddressOf Me.onEndPrint
End Sub
' 印刷処理の終了時に発生するイベント
Private Sub onEndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs)
Console.WriteLine("印刷完了")
End Sub
◆サンプルコード(C#)
private void Form1_Load(object sender, EventArgs e)
{
// セクションレポートをViewerに表示します。
SectionReport1 sectionReport = new SectionReport1();
sectionReport.Run();
GrapeCity.ActiveReports.Document.SectionDocument sectionDocument _
= sectionReport.Document;
this.viewer1.LoadDocument(sectionDocument);
// EndPrintイベントをイベントハンドラに関連付けます。
sectionDocument.Printer.EndPrint += new System.Drawing.Printing.PrintEventHandler(onEndPrint);
}
// 印刷処理の終了時に発生するイベント
private void onEndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
Console.WriteLine("印刷完了");
}
{
// セクションレポートをViewerに表示します。
SectionReport1 sectionReport = new SectionReport1();
sectionReport.Run();
GrapeCity.ActiveReports.Document.SectionDocument sectionDocument _
= sectionReport.Document;
this.viewer1.LoadDocument(sectionDocument);
// EndPrintイベントをイベントハンドラに関連付けます。
sectionDocument.Printer.EndPrint += new System.Drawing.Printing.PrintEventHandler(onEndPrint);
}
// 印刷処理の終了時に発生するイベント
private void onEndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
Console.WriteLine("印刷完了");
}
ページレポート・RDLレポートの場合
◆サンプルコード(VB.NET)Private Sub Form1_Load(...) Handles MyBase.Load
' レポートをViewerに表示します。
Dim file_name As String = "..¥..¥PageReport1.rdlx"
Dim pageReport As New GrapeCity.ActiveReports.PageReport(New System.IO.FileInfo(file_name))
Dim pageDocument As New GrapeCity.ActiveReports.Document.PageDocument(pageReport)
Me.Viewer1.LoadDocument(pageDocument)
' EndPrintイベントをイベントハンドラに関連付けます。
AddHandler pageDocument.Printer.EndPrint, AddressOf Me.onEndPrint
End Sub
' 印刷処理の終了時に発生するイベント
Private Sub onEndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs)
Console.WriteLine("印刷完了")
End Sub
' レポートをViewerに表示します。
Dim file_name As String = "..¥..¥PageReport1.rdlx"
Dim pageReport As New GrapeCity.ActiveReports.PageReport(New System.IO.FileInfo(file_name))
Dim pageDocument As New GrapeCity.ActiveReports.Document.PageDocument(pageReport)
Me.Viewer1.LoadDocument(pageDocument)
' EndPrintイベントをイベントハンドラに関連付けます。
AddHandler pageDocument.Printer.EndPrint, AddressOf Me.onEndPrint
End Sub
' 印刷処理の終了時に発生するイベント
Private Sub onEndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs)
Console.WriteLine("印刷完了")
End Sub
◆サンプルコード(C#)
private void Form1_Load(object sender, EventArgs e)
{
// レポートをViewerに表示します。
string file_name = @"..¥..¥PageReport1.rdlx";
GrapeCity.ActiveReports.PageReport pageReport
= new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(file_name));
GrapeCity.ActiveReports.Document.PageDocument pageDocument
= new GrapeCity.ActiveReports.Document.PageDocument(pageReport);
this.viewer1.LoadDocument(pageDocument);
// EndPrintイベントをイベントハンドラに関連付けます。
pageDocument.Printer.EndPrint += new System.Drawing.Printing.PrintEventHandler(onEndPrint);
}
// 印刷処理の終了時に発生するイベント
private void onEndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
Console.WriteLine("印刷完了");
}
{
// レポートをViewerに表示します。
string file_name = @"..¥..¥PageReport1.rdlx";
GrapeCity.ActiveReports.PageReport pageReport
= new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(file_name));
GrapeCity.ActiveReports.Document.PageDocument pageDocument
= new GrapeCity.ActiveReports.Document.PageDocument(pageReport);
this.viewer1.LoadDocument(pageDocument);
// EndPrintイベントをイベントハンドラに関連付けます。
pageDocument.Printer.EndPrint += new System.Drawing.Printing.PrintEventHandler(onEndPrint);
}
// 印刷処理の終了時に発生するイベント
private void onEndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
Console.WriteLine("印刷完了");
}
なお、上記のような処理で、「ActiveReports側の印刷処理が完了したかどうか」を判断できますが、これは「ActiveReportsから(プリンタスプーラなどへの)印刷データの出力が完了したかどうか」という判断です。
実際に「プリンタ側で印刷が行われたかどうか」(プリンタ側での印刷完了やエラー)を判断する機能は、ActiveReportsには用意されておりません。
関連情報
- Windowsフォームアプリケーションでプレビュー表示せずにレポートを直接印刷する方法は?
- Windowsフォームアプリでレポートを印刷する際、印刷ダイアログ上で押されたボタンを判断する方法は?
- Viewerコントロールの[印刷]ボタンをクリックしたときの動作を変更する方法は?
キーワード
HowTo 印刷・プレビュー