如何在打印/導(dǎo)出控件時設(shè)置紙張格式并向報表中添加自定義信息
用于WinForms的DevExpress報表控件允許您自定義紙張格式、方向并向報表添加自定義信息,注意下面的方法適用于實現(xiàn)IPrintable接口的控件(例如XtraGrid, XtraPivotGrid, XtraScheduler, XtraTreeList, XtraCharts, Layout Control, XtraVerticalGrid等)。
本文檔由以下幾部分組成:
要開始學(xué)習(xí)本教程,請啟動Microsoft Visual Studio并創(chuàng)建一個新的Windows Forms Application或打開一個現(xiàn)有的表單應(yīng)用程序,然后運行工具箱并將實現(xiàn)IPrintable接口的所需控件拖放到表單上。
接下來,您可以將控件綁定到數(shù)據(jù)或手動填充它。
運行時自定義打印選項
IPrintable接口允許您自定義打印設(shè)置,并使用PrintableComponentLink打印控件。下面的代碼演示了如何創(chuàng)建PrintableComponentLink,將控件分配給PrintableComponentLinkBaseComponent屬性,調(diào)整其打印設(shè)置然后打印控件。
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); } }
在運行時向報表添加自定義信息
創(chuàng)建報表頁眉或頁腳,以便向報表添加自定義信息,訂閱CreateReportHeader事件來添加報告標(biāo)題,如下所示。
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
下圖顯示了包含指定打印選項和其他自定義信息的結(jié)果報告。
在運行時將報表導(dǎo)出為指定格式
除了Print Preview窗口中提供的導(dǎo)出功能之外,您還可以通過PrintableComponentLink對象導(dǎo)出報告。
C#:
PrintableComponentLink link = new PrintableComponentLink(); link.ExportToPdf(@"c:\gridcontrol.pdf");
VB.NET:
Dim link As New PrintableComponentLink() link.ExportToPdf("c:\gridcontrol.pdf")