ActiveReports 报表 控件可以灵活的控制打印设置,本篇文章中我们将介绍以下内容:
- 直接打印报表
- 设置默认打印对话框
- 使用自定义打印对话框
下面,让我们来一一阐述实现方法:
直接打印报表
很多用户为了简化最终用户操作,希望直接打印报表,而不弹出打印设置对话框,针对这种情况下ActiveReports可以使用代码设置打印选项,为了避免用户点击工具条中的按钮而误操作,所以需要隐藏工具条中打印相关的按钮。以下是实现代码:
'Hiding the default print button
Viewer1.Toolbar.ToolStrip.Items(2).Visible = False '2 is the index of the PrintButton in the ToolStrip
'Setting the custom Printer Settings
'Option 1 :
Viewer1.Document.Printer.PrinterName = "Microsoft XPS Document Writer"
Viewer1.Document.Printer.Landscape = True
Viewer1.Document.Print(False, False)
'Option 2 :
Report1.Document.Printer.PrinterName = "Microsoft XPS Document Writer" 'give the desired printer name
Report1.Document.Printer.Landscape = True
Report1.Document.Print(False, False)
传递打印设置到默认打印对话框
这也是一种常见的用户使用场景,有些用户希望弹出打印设置对话框,并且希望对话框默认显示自定义设置。针对这种情况,我们不需要隐藏打印按钮,使用以下代码传递打印机设置:
'Option 1 :
Viewer1.Document.Printer.PrinterSettings.PrinterName = "Microsoft XPS Document Writer" 'give the desired printer name
Viewer1.Document.Printer.Landscape = True
'Option 2 :
Report1.Document.Printer.PrinterSettings.PrinterName = "Microsoft XPS Document Writer" 'give the desired printer name
Report1.Document.Printer.Landscape = True
使用自定义打印对话框
这种使用场景最简单的实现方法是,隐藏默认打印按钮,添加自定义按钮到工具条中,并添加点击事件用于调用自定义打印对话框。实现代码如下:
'Replacing the default Print Button
Viewer1.Toolbar.ToolStrip.Items(2).Visible = False
Dim prnt_btn As New ToolStripButton
prnt_ btn.Image = Image.FromFile("..\..\Print.jpg")
AddHandler btn.Click, AddressOf test
Viewer1.Toolbar.ToolStrip.Items.Insert(2, prnt_btn)
'Assign custom properties
If Me.PrintDialog1.ShowDialog() = DialogResult.OK Then
'Option 1 :
Viewer1.Document.Printer.PrinterSettings = Me.PrintDialog1.PrinterSettings
Viewer1.Document.Print(False, False)
'Option 2 :
Report1.Document.Printer.PrinterSettings = Me.PrintDialog1.PrinterSettings
Report1.Document.Print(False, False)
下载示例:
VS2010 + VB.NET + AR8 + .NET4.0
更多ActiveReports报表功能请参考:/developer/activereports