Create a Custom Print Preview
- 3 minutes to read
If you want to preview a report and then print or export it, you can invoke the built-in Print Preview dialog, as described in the following help topic: Invoke the Built-in Print Preview Form. A Reporting subscription is required to use the built-in dialog in your application.
An alternative method is to place the DocumentViewer control on the form and bind it to a report. A WinForms subscription is required in order to complete this custom print preview.
If your WinForms application targets .NET, add the DevExpress.Win.Design NuGet package to this application to use the DocumentViewer control.
Step by Step Guide
Create a custom Print Preview form with the DocumentViewer control. For this, do the following:
Switch to the application’s main form in Visual Studio and press CTRL+ALT+X to open the Toolbox. Expand the DX.24.2: Reporting category and drop the DocumentViewer control onto the form.
Click the Document Viewer’s smart tag and select the report in the Document Source drop-down menu to assign a report to the DocumentViewer.DocumentSource property.
Note
Rebuild the project so that the newly added reports appear in the Document Source drop-down menu.
NET Core projects do not display reports in this list. You should assign the DocumentSource property in code:
documentViewer1.DocumentSource = new XtraReport1();
Optional. If you want to preview a large report that might consume significant memory, bind the DocumentViewer control to the CachedReportSource component instead. This component stores document pages in a file system or database when the document is generated, and thus allows you to avoid issues with memory consumption.
Check the Cache Document Source option in the Document Viewer’s smart tag after you set the Document Source property:
This adds the CachedReportSource component to the form.
You can assign one of the following ready-to-use document storages to the CachedReportSource.Storage property:
Click the Document Viewer’s smart tag and select the Create Ribbon Toolbar action to create the Ribbon toolbar:
Run the application. A form with the DocumentViewer control that displays a sample report is shown in the following image:
Localization
A built-in Print Preview dialog uses a standard localization mechanism that involves satellite resource assemblies. For more information, review the following help topic: Localizing WinForms Controls with Satellite Resource Assemblies.
To localize a custom form that contains the DocumentViewer control, you must use localizable resources and satellite resource assemblies. This is necessary to localize static and dynamic elements, such as export dialogs.
You can set the form’s Localizable
property to true
and specify the Language
property to have Visual Studio automatically generate a resource file for the specified language. Subsequently, you can manually translate strings in the resource file. For more information about how to use localization resources in a .NET application, review the following help topic: Walkthrough: Localizing Windows Forms.