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.
Printing Link | Description |
---|---|
Simple |
Renders data objects that contain linear data. |
Collection |
Renders data objects that contain hierarchical data and implement the ICollection |
Printable |
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.
#Page Settings
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.
Property | Description |
---|---|
Link |
Gets or sets a size of custom paper (measured in hundredths of an inch). |
Link |
Gets or sets the name of the document. |
Link |
Gets or sets a value indicating whether the page orientation is landscape. |
Link |
Gets or sets the margins of a report page (measured in hundredths of an inch). |
Link |
Specifies the minimum printer margin values. |
Link |
Gets or sets the type of paper for the document. |
Link |
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.
Method | Description |
---|---|
Link |
Exports a report to the specified stream in CSV format using the specified CSV-specific options. |
Link |
Exports a report to the specified stream in HTML format using the specified HTML-specific options. |
Link |
|
Link |
Exports a report to the specified stream in MHT format using the specified MHT-specific options. |
Link |
Exports a report to the specified stream in PDF format using the specified PDF-specific options. |
Link |
Exports a report to the specified stream in RTF format using the specified RTF-specific options. |
Link |
Exports a report to the specified stream in text format using the specified TXT-specific options. |
Link |
Exports a report to the specified stream in XLS format using the specified XLS-specific options. |
Link |
Exports a report to the specified stream in XLSX format using the specified XLSX-specific options. |
Link |
Exports a report to the specified stream in XPS format using the specified XPS-specific options. |