windowsforms-2424-build-an-application-printing-and-exporting-how-to-set-paper-format-and-add-custom-information-to-the-report-when-printing-exporting-a-control.md
DevExpress Reporting controls for WinForms allow you to customize paper format, orientation, and add custom information to a report. Note that the following approach applies to controls that implement the IPrintable interface (e.g. XtraGrid, XtraPivotGrid, XtraScheduler, XtraTreeList, XtraCharts, Layout Control, XtraVerticalGrid, etc.).
This document consists of the following sections.
To get started with this tutorial, start Microsoft Visual Studio and create a new Windows Forms Application or open an existing one. Then, run the Toolbox and drop the required control that implements the IPrintable interface onto the form.
Next, you can bind the control to data or populate it manually.
The IPrintable interface allows you to customize print settings and print a control using a PrintableComponentLink. The following code demonstrates how to create a PrintableComponentLink, assign a control to its PrintableComponentLinkBase.Component property, adjust its print settings, and print the control.
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);
}
}
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
Create a report header or footer to add custom information to a report. Subscribe to the CreateReportHeader event to add a report header, as shown below.
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);
}
}
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
Handle the CreateReportHeader event as follows.
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);
}
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
The following image illustrates a resulting report contains with the specified print options and additional custom information.
In addition to the export functionality provided in the Print Preview window, you can also export a report via the PrintableComponentLink object.
PrintableComponentLink link = new PrintableComponentLink();
link.ExportToPdf(@"c:\gridcontrol.pdf");
Dim link As New PrintableComponentLink()
link.ExportToPdf("c:\gridcontrol.pdf")
See Also
Export a Control in Various Formats (PDF, HTML, BMP, etc.) Using the XtraPrinting Library