Printing Custom Reports
- 5 minutes to read
The DXPrinting Library provides an easy approach to creating custom printed reports. This approach is based on special printing procedures called printing links. The DXPrinting Library introduces several types of printing links, each designed for rendering specific data objects, creating reports, and exporting and printing reports. You can create a custom printing link by inheriting your class from the LinkBase abstract class.
The following table lists the built-in types of printing links.
|SimpleLink||Renders data objects that contain linear data.|
|CollectionViewLink||Renders data objects that contain hierarchical data and implement the ICollectionView interface.|
|PrintableControlLink||Renders DevExpress controls.|
Creating Simple Links
Simple printing links are usually used to present non-hierarchical data in a report. Use these kind of links at such a time when data items do not need to be divided into groups.
Once created, simple links must be supplied with data. To do this, specify the number of data items via the SimpleLink.DetailCount property and handle the SimpleLink.CreateDetail event. The event is raised each time a new data item is required and as many times as specified by the SimpleLink.DetailCount property. In the handler, assign the new data item to the CreateAreaEventArgs.Data event parameter’s property. The index of the required data item can be obtained using the CreateAreaEventArgs.DetailIndex property.
Along with data items, visual presentation must be specified. To do this, create a DataTemplate, which should be applied to each data item, and then assign it to the link’s SimpleLink.DetailTemplate property.
Creating Collection View Links
Collection view printing links are useful when you need to display data from a hierarchical data source in your report or when data items should be shown split into several groups.
To assign data to a collection view link, create an object that implements the ICollectionView interface. This can be a System.Windows.Data.CollectionViewSource object. After supplying this object with data, assign it to the link’s CollectionViewLink.CollectionView property.
When data is specified, you should define its visual presentation. To do this, create a DataTemplate that should be applied to each data item and assign it to the link’s CollectionViewLink.DetailTemplate property. To provide the link with the information about item grouping, create a GroupInfo object for each grouping level, and specify a template that should be applied to each group header in the current grouping level via the GroupInfo.HeaderTemplate property. Then, add the GroupInfo objects to the link’s CollectionViewLink.GroupInfos collection.
Creating Printable Control Links
Printable control links are used to render controls and data objects that implement the IPrintableControl or IRootDataNode interface. For instance, the TableView object can be rendered by a printable control link.
To create a printable control link, pass the appropriate control or data object to the PrintableControlLink constructor.
The DXPrinting Library provides a number of settings that allow you to customize the page options of the report.
You can include page headers and footers into your report, as well as the header and footer of the whole document. To do this, specify their templates via the TemplatedLink.PageHeaderTemplate, TemplatedLink.PageFooterTemplate, TemplatedLink.ReportHeaderTemplate and TemplatedLink.ReportFooterTemplate properties, respectively. These templates can also be provided with some custom data by assigning the appropriate data objects to the TemplatedLink.PageHeaderData, TemplatedLink.PageFooterData, TemplatedLink.ReportHeaderData and TemplatedLink.ReportFooterData properties, respectively.
The following table lists other properties used to specify report page settings.
|LinkBase.CustomPaperSize||Gets or sets a size of custom paper (measured in hundredths of an inch).|
|LinkBase.DocumentName||Gets or sets the name of the document.|
|LinkBase.Landscape||Gets or sets a value indicating whether the page orientation is landscape.|
|LinkBase.Margins||Gets or sets the margins of a report page (measured in hundredths of an inch).|
|LinkBase.MinMargins||Specifies the minimum printer margin values.|
|LinkBase.PaperKind||Gets or sets the type of paper for the document.|
|LinkBase.SuppressAutoRebuildOnPageSettingsChange||Gets or sets a value indicating whether it is necessary to suppress rebuilding the document every time a link’s page settings are changed.|
Exporting and Printing Reports
After the link object is created and supplied with data, you can create a document from the link by calling the LinkBase.CreateDocument method. Now the report is generated, and you can print and export it, as well as display it in the DocumentPreview control. To do this, it is necessary to create a new LinkPreviewModel instance with the current link as an initialization parameter, and assign it to the DocumentPreview.Model property.
Use the LinkBase.ShowPrintPreview method to open the report preview window. To open a modal preview window, use the LinkBase.ShowPrintPreviewDialog method. The printing dialog can be invoked via the LinkBase.Print method. To print the report without opening any dialogs, use the LinkBase.PrintDirect method.
The following table lists methods used to export a report into different formats.
|LinkBase.ExportToCsv||Exports a report to the specified stream in CSV format.|
|LinkBase.ExportToHtml||Exports a report to the specified stream in HTML format.|
|LinkBase.ExportToImage||Exports a report to the specified stream as an image.|
|LinkBase.ExportToMht||Exports a report to the specified stream in MHT format.|
|LinkBase.ExportToPdf||Exports a report to the specified stream in PDF format.|
|LinkBase.ExportToRtf||Exports a report to the specified stream in RTF format.|
|LinkBase.ExportToText||Exports a report to the specified stream in Text format.|
|LinkBase.ExportToXls||Exports a report to the specified stream in XLS format.|
|LinkBase.ExportToXlsx||Exports a report to the specified stream in XLSX format.|
|LinkBase.ExportToXps||Exports a report to the specified stream in XPS format using the specified XPS-specific options.|