如何在打印/导出控件时设置纸张格式并向报表中添加自定义信息
用于WinForms的DevExpress报表控件允许您自定义纸张格式、方向并向报表添加自定义信息,注意下面的方法适用于实现接口的控件(例如XtraGrid, XtraPivotGrid, XtraScheduler, XtraTreeList, XtraCharts, Layout Control, XtraVerticalGrid等)。
本文档由以下几部分组成:
要开始学习本教程,请启动Microsoft Visual Studio并创建一个新的Windows Forms Application或打开一个现有的表单应用程序,然后运行工具箱并将实现IPrintable接口的所需控件拖放到表单上。
接下来,您可以将控件绑定到数据或手动填充它。
运行时自定义打印选项
IPrintable接口允许您自定义打印设置,并使用打印控件。下面的代码演示了如何创建PrintableComponentLink,将控件分配给属性,调整其打印设置然后打印控件。
C#:
using DevExpress.LookAndFeel; using DevExpress.XtraEditors; using DevExpress.XtraPrinting; using DevExpress.XtraPrinting.Links; using DevExpress.XtraPrintingLinks; //... public partial class Form1 : XtraForm { //... private void gridControl1_Load(object sender, EventArgs e) { PreviewPrintableComponent(gridControl1, gridControl1.LookAndFeel); } void PreviewPrintableComponent(IPrintable component, UserLookAndFeel lookAndFeel) { // Create a link that will print a control. PrintableComponentLink link = new PrintableComponentLink() { PrintingSystemBase = new PrintingSystemBase(), Component = component, Landscape = true, PaperKind = PaperKind.A5, Margins = new Margins(20,20,20,20) }; // Show the report. link.ShowRibbonPreview(lookAndFeel); } }
VB.NET:
using DevExpress.LookAndFeel; using DevExpress.XtraEditors; using DevExpress.XtraPrinting; using DevExpress.XtraPrinting.Links; using DevExpress.XtraPrintingLinks; //... public partial class Form1 : XtraForm { //... private void gridControl1_Load(object sender, EventArgs e) { PreviewPrintableComponent(gridControl1, gridControl1.LookAndFeel); } void PreviewPrintableComponent(IPrintable component, UserLookAndFeel lookAndFeel) { // Create a link that will print a control. PrintableComponentLink link = new PrintableComponentLink() { PrintingSystemBase = new PrintingSystemBase(), Component = component, Landscape = true, PaperKind = PaperKind.A5, Margins = new Margins(20,20,20,20) }; // Show the report. link.ShowRibbonPreview(lookAndFeel); } }
在运行时向报表添加自定义信息
创建报表页眉或页脚,以便向报表添加自定义信息,订阅CreateReportHeader事件来添加报告标题,如下所示。
C#:
using DevExpress.LookAndFeel; using DevExpress.XtraEditors; using DevExpress.XtraPrinting; using DevExpress.XtraPrinting.Links; using DevExpress.XtraPrintingLinks; //... public partial class Form1 : XtraForm { //... void PreviewPrintableComponent(IPrintable component, UserLookAndFeel lookAndFeel) { // Create a link that will print a control. //... // Subscribe to the CreateReportHeaderArea event used to generate the report header. link.CreateReportHeaderArea += link_CreateReportHeaderArea; // Show the report. link.ShowRibbonPreview(lookAndFeel); } }
VB.NET:
Imports DevExpress.LookAndFeel Imports DevExpress.XtraEditors Imports DevExpress.XtraPrinting Imports DevExpress.XtraPrinting.Links Imports DevExpress.XtraPrintingLinks '... Public Partial Class Form1 Inherits XtraForm '... Private Sub PreviewPrintableComponent(component As IPrintable, lookAndFeel As UserLookAndFeel) ' Create a link that will print a control. '... ' Subscribe to the CreateReportHeaderArea event used to generate the report header. AddHandler link.CreateReportHeaderArea, AddressOf Link_CreateReportHeaderArea ' Show the report. link.ShowRibbonPreview(lookAndFeel) End Sub End Class
按如下方式处理CreateReportHeader事件:
C#:
using System.Drawing; using DevExpress.XtraPrinting; private void link_CreateReportHeaderArea(object sender, CreateAreaEventArgs e) { string reportHeader = "Categories Report"; e.Graph.StringFormat = new BrickStringFormat(StringAlignment.Center); e.Graph.Font = new Font("Tahoma", 14, FontStyle.Bold); RectangleF rec = new RectangleF(0, 0, e.Graph.ClientPageSize.Width, 50); e.Graph.DrawString(reportHeader, Color.Black, rec, BorderSide.None); }
VB.NET:
Imports System.Drawing Imports DevExpress.XtraPrinting Private Sub link_CreateReportHeaderArea(ByVal sender As System.Object, _ ByVal e As CreateAreaEventArgs) _ Handles PrintableComponentLink1.CreateReportHeaderArea Dim reportHeader As String = "Categories Report" e.Graph.StringFormat = New BrickStringFormat(StringAlignment.Center) e.Graph.Font = New Font("Tahoma", 14, FontStyle.Bold) Dim rec As RectangleF = New RectangleF(0, 0, e.Graph.ClientPageSize.Width, 50) e.Graph.DrawString(reportHeader, Color.Black, rec, BorderSide.None) End Sub
下图显示了包含指定打印选项和其他自定义信息的结果报告。
在运行时将报表导出为指定格式
除了窗口中提供的导出功能之外,您还可以通过对象导出报告。
C#:
PrintableComponentLink link = new PrintableComponentLink(); link.ExportToPdf(@"c:\gridcontrol.pdf");
VB.NET:
Dim link As New PrintableComponentLink() link.ExportToPdf("c:\gridcontrol.pdf")