DevExpress WinForm控件入门指南 - 如何在打印/导出时设置纸张格式
WinForm平台下的DevExpress Reporting控件允许您自定义纸张格式、方向,并将自定义信息添加到报表中。请注意,以下方法适用于实现 IPrintable 接口的控件(例如 XtraGrid、XtraPivotGrid、XtraScheduler、XtraTreeList、XtraCharts、Layout Control、XtraVerticalGrid 等)。
获取工具下载 - DevExpress WinForm v21.1
DevExpress技术交流群4:715863792 欢迎一起进群讨论
要开始学习本教程,请启动Microsoft Visual Studio并创建一个新的Windows Forms应用程序或打开一个现有应用程序,然后运行Toolbox并将实现IPrintable接口所需控件拖放到窗体上。

接下来,您可以将控件绑定到数据或手动填充它。
在运行时自定义打印选项
IPrintable接口允许您自定义打印设置并使用 PrintableComponentLink 打印控件,以下代码演示了如何创建 PrintableComponentLink、将控件分配给其 PrintableComponentLinkBase.Component 属性、调整其打印设置以及打印控件。
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
Imports DevExpress.LookAndFeel Imports DevExpress.XtraEditors Imports DevExpress.XtraPrinting Imports DevExpress.XtraPrinting.Links Imports DevExpress.XtraPrintingLinks '... Public Partial Class Form1 Inherits XtraForm '... Private Sub gridControl1_Load(sender As Object, e As EventArgs) PreviewPrintableComponent(gridControl1, gridControl1.LookAndFeel) End Sub Private Sub PreviewPrintableComponent(component As IPrintable, lookAndFeel As UserLookAndFeel) ' Create a link that will print a control. Dim link As New PrintableComponentLink() With { _ .PrintingSystemBase = New PrintingSystem(), _ .Component = component, _ .Landscape = True, _ .PaperKind = PaperKind.A5, _ .Margins = New Margins(20, 20, 20, 20) _ } ' Show the report. link.ShowRibbonPreview(lookAndFeel) End Sub End Class
在运行时向报表添加自定义信息
创建报表页眉或者页脚来向报表添加自定义信息,订阅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
下图说明了包含指定打印选项和附加自定义信息的结果报表。

在运行时将报表导出为指定格式
除了打印预览窗口中提供的导出功能外,您还可以通过PrintableComponentLink对象导出报表。
C#
PrintableComponentLink link = new PrintableComponentLink(); link.ExportToPdf(@"c:\gridcontrol.pdf");
VB.NET
Dim link As New PrintableComponentLink() link.ExportToPdf("c:\gridcontrol.pdf")
DevExpress WinForm拥有180+组件和UI库,能为Windows Forms平台创建具有影响力的业务解决方案。DevExpress WinForms能完美构建流畅、美观且易于使用的应用程序,无论是Office风格的界面,还是分析处理大批量的业务数据,它都能轻松胜任!
更多DevExpress线上公开课、中文教程资讯请上中文网获取
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2020-09-08 Kendo UI Web开发:看如何使用Rows
2020-09-08 WPF界面开发:如何在WPF Grid控件单元中建立绑定路径