Export to RTF
- 8 minutes to read
This document describes how to export a report document to RTF format.
You can export a report from the Report Designer’s Preview and the Document Viewer on all supported platforms (WinForms, WPF, ASP.NET Web Forms, ASP.NET MVC, and ASP.NET Core). To do this, expand the Export Document drop-down list and select RTF File. Specify export options in the invoked dialog, and click OK.
To export a report document to RTF format in code, use one of the following approaches:
Export a Report
Call an XtraReport.ExportToRtf overloaded method to export a report. To export a report asynchronously in a separate task, call an XtraReport.ExportToRtfAsync overloaded method instead. To specify export options, create a RtfExportOptions class instance and pass this instance to the invoked method, or use XtraReport‘s ExportOptions.Rtf property.
using System;
using DevExpress.XtraPrinting;
using DevExpress.XtraReports.UI;
// ...
XtraReport report = new XtraReport() {
Name = "HelloWorld",
Bands = {
new DetailBand(){
Controls={
new XRLabel(){
Text="Hello World!"
}
}
}
}
};
// rtfExportFile specifies the exported file's name and path. The user's Downloads folder is used as the default path.
string rtfExportFile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\Downloads\" + report.Name + ".rtf";
// Specify export options to export the report.
// Uncomment the lines below to specify export options in an RtfExportOptions class instance.
// RtfExportOptions RtfExportOptions = new RtfExportOptions();
// RtfExportOptions.ExportMode = RtfExportMode.SingleFile;
// report.ExportToRtf(rtfExportFile, RtfExportOptions);
// Comment the lines below if you specified export options in an RtfExportOptions class instance above.
report.ExportOptions.Rtf.ExportMode = RtfExportMode.SingleFile;
report.ExportToRtf(rtfExportFile);
Export a Document
Call a PrintingSystem.ExportToRtf overloaded method to export a document. To specify export options, create a RtfExportOptions class instance and pass this instance to the invoked method, or use PrintingSystem‘s ExportOptions.Rtf property.
using System;
using DevExpress.XtraPrinting;
using DevExpress.XtraReports.UI;
// ...
XtraReport report = new XtraReport() {
Name = "HelloWorld",
Bands = {
new DetailBand(){
Controls={
new XRLabel(){
Text="Hello World!"
}
}
}
}
};
// Create a document from the report.
report.CreateDocument();
// rtfExportFile specifies the exported file's name and path. The user's Downloads folder is used as the default path.
string rtfExportFile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\Downloads\" + report.Name + ".rtf";
// Specify export options and export the document.
// Uncomment the lines below to specify export options in an RtfExportOptions class instance.
// RtfExportOptions RtfExportOptions = new RtfExportOptions();
// RtfExportOptions.ExportMode = RtfExportMode.SingleFile;
// report.PrintingSystem.ExportToRtf(rtfExportFile, RtfExportOptions);
// Comment the lines below if you specified export options in an RtfExportOptions class instance above.
report.PrintingSystem.ExportOptions.Rtf.ExportMode = RtfExportMode.SingleFile;
report.PrintingSystem.ExportToRtf(rtfExportFile);
Export a Control
Call a PrintingLink.ExportToRtf overloaded method to export a control. To specify export options, create a RtfExportOptions class instance and pass this instance to the invoked method.
using System;
using DevExpress.XtraPrinting;
using DevExpress.XtraReports.UI;
// ...
DevExpress.XtraCharts.ChartControl chart = new DevExpress.XtraCharts.ChartControl() {
Name = "IncomeByQuarter",
Series = {
new DevExpress.XtraCharts.Series("2019", DevExpress.XtraCharts.ViewType.Bar)
}
};
// Create a printing link.
PrintingSystem printingSystem = new PrintingSystem();
PrintableComponentLink link = new PrintableComponentLink();
printingSystem.Links.Add(link);
link.Component = chart;
// rtfExportFile specifies the exported file's name and path. The user's Downloads folder is used as the default path.
string rtfExportFile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\Downloads\" + chart.Name + ".rtf";
// Specify export options and export the control.
RtfExportOptions RtfExportOptions = new RtfExportOptions();
RtfExportOptions.ExportMode = RtfExportMode.SingleFile;
link.ExportToRtf(rtfExportFile, RtfExportOptions);
Specify RTF Export Options
The RtfExportOptions class exposes options for report export to to RTF. Use the report’s ExportOptions.Rtf property to access these options.
The RtfExportOptions.ExportMode property specifies how a document is exported to RTF:
-
The export follows the WYSIWYG paradigm: the report’s page headers and footers, and top and bottom margins appear on every page of the resulting document as they appear in the report’s Print Preview. Elements residing in your report are treated as frames in the resulting file.
-
A multi-page report is converted into a single document with a table-like layout: the report’s page headers and footers are repeated only once - at the beginning and end of the resulting document.
Note
Not all programs that can be used to view and edit RTF files can correctly process the RTF export files. For instance, Microsoft® Word® displays these files correctly but WordPad® does not. This is because WordPad does not fully support the RTF standard.
The following table lists RTF-specific options and export modes where these options are supported:
Option | Single File (Continuous) | Single File Page-by-Page | Description |
---|---|---|---|
Specifies whether to include the text and image watermarks in the RTF file. | |||
Specifies the page range to export to RTF. | |||
Specifies whether the table row heights should be fixed in the export document. | |||
Defines whether to display the header and footer contents on the exported document’s first page. | |||
Specifies whether to include page breaks in the exported file. |
The following specifics apply to documents exported in the RtfExportMode.SingleFile mode:
You cannot export reports merged page by page in the Single File export mode. As a workaround:
- Use subreports to merge multiple reports into a single document.
- Export all reports to separate files and join these files in a single file.
To ensure that report controls are exported correctly, they should not intersect. Otherwise, the file layout can be corrupted.
Overlapped controls are highlighted in reports. Users can move the mouse pointer over these controls to see what needs to be fixed.
Use a report’s HasExportWarningControls collection to check if there are controls that have export warnings.
Disable a report’s DesignerOptions.ShowExportWarnings property to remove highlights on overlapped controls.
When a document is exported to RTF in a table-like layout, Microsoft® Word® can set table row heights to fit content. As a result, the exported document can differ from the initial document in Print Preview. To avoid this effect, enable the FormattedTextExportOptions.KeepRowHeight property.
If you disable this property or leave it at the default value, row heights are not fixed. A new line of text added to a cell’s content increases the row height.
KeepRowHeight = false KeepRowHeight = true To locate controls’ content in the export file’s native header and footer sections, place these controls in the TopMarginBand or BottomMarginBand rather than in the page footer and header.
If a report uses a CachedReportSource, changes made in Preview are not included in the exported file.
Vector images (for instance, pictures, charts, or bar codes) are rasterized in the exported file. Use the PageByPageExportOptionsBase.RasterizationResolution property to specify the image resolution.
Post-Process RTF Files
You can post-process the resulting RTF files with the help of a dedicated DevExpress library: Word Processing Document API. This product helps you edit, merge, split, and password-protect RTF files.
You may also use Word Processing Document API to generate rich text documents from scratch. If that code-only approach suits your requirements, you don’t need to construct a report in the designer at all.
Word Processing Document API works in desktop and web applications that target a variety of platforms (Windows Forms, WPF, ASP.NET Web Forms, ASP.NET MVC, ASP.NET Core, Blazor, MAUI) and operating systems (Windows, Linux, macOS).
Document generation/editing API is available in different DevExpress subscriptions. The following list briefly outlines what products you need to license to access required functionality. For complete information on DevExpress subscriptions and included functionality, please visit our Subscription Comparison page.
- To manage rich text, spreadsheet, and PDF files: Office File API or Universal subscription.
- To manage rich text documents only: WinForms, WPF, ASP.NET, or DXperience subscription.
If you haven’t yet done so, download our fully-functional 30-day trial version to try out DevExpress controls and libraries in your projects: