用于WinForms的DevExpress报告控件允许您自定义纸张格式、方向,并将自定义信息添加到报告中。请注意,以下方法适用于实现可打印接口的控件(例如,XtraGrid、XtraPivotGrid、Xtra Scheduler、XtraTreeList、XtraCharts、Layout Control、XtraVerticalGrid等)。
启动VS2019并创建一个新的Windows窗体应用程序或打开一个现有的应用程序。然后,运行“工具箱”并将实现IPrintable接口的所需控件拖放到窗体上。
Customize Print Options at Runtime
运行时自定义打印选项
IPrintable界面允许您自定义打印设置,并使用PrintableComponentLink打印控件。以下代码演示如何创建PrintableComponentLink,并将控件分配给其PrintableComponentLinkBase。组件属性,调整其打印设置,然后打印控件。
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);
}
}
Add Custom Information to a Report at Runtime
在运行时向报表添加自定义信息
创建报表页眉或页脚以向报表中添加自定义信息。订阅CreateReportHeader事件以添加报告头,如下所示。
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);
}
}
如下方式处理CreateReportHeader事件。
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);
}
下图显示了包含指定打印选项和其他自定义信息的结果报告。
Export a Report to the Specified Format at Runtime
在运行时将报告导出为指定格式
除了“ Print Preview”窗口中提供的导出功能外,您还可以通过PrintableComponentLink对象导出报告。
PrintableComponentLink link = new PrintableComponentLink();
link.ExportToPdf(@"c:\gridcontrol.pdf");