Skip to main content

How to: Set Paper Format and Add Custom Information to the Report when Printing/Exporting a Control

  • 4 minutes to read

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.

how-to-set-paper-format-add-grid-control

Next, you can bind the control to data or populate it manually.

Customize Print Options at Runtime

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);
    }
}

Add Custom Information to a Report at Runtime

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);
    }
}

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);
}

The following image illustrates a resulting report contains with the specified print options and additional custom information.

how-to-set-paper-format-preview

Export a Report to the Specified Format at Runtime

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"); 
See Also