Export to MHT

  • 7 minutes to read

This document describes how to export a report document to MHT 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 MHT File. Specify export options in the invoked dialog, and click OK.

To export a report document to MHT format in code, use one of the following approaches:

  • Export a Report

    Call an XtraReport.ExportToMht overloaded method to export a report. To export a report asynchronously in a separate task, call an XtraReport.ExportToMhtAsync overloaded method instead. To specify export options, create a MhtExportOptions class instance and pass this instance to the invoked method, or use XtraReport's ExportOptions.Mht 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!"
                    }
                }
            }
        }
    };
    // mhtExportFile specifies the exported file's name and path. The user's Downloads folder is used as the default path.
    string mhtExportFile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\Downloads\" + report.Name + ".mht";
    // Specify export options to export the report.
    // Uncomment the lines below to specify export options in an MhtExportOptions class instance.
    // MhtExportOptions MhtExportOptions = new MhtExportOptions();
    // MhtExportOptions.ExportMode = MhtExportMode.SingleFile;
    // report.ExportToMht(mhtExportFile, MhtExportOptions);
    
    // Comment the lines below if you specified export options in an MhtExportOptions class instance above.
    report.ExportOptions.Mht.ExportMode = MhtExportMode.SingleFile;
    report.ExportToMht(mhtExportFile);
    
  • Export a Document

    Call a PrintingSystem.ExportToMht overloaded method to export a document. To specify export options, create a MhtExportOptions class instance and pass this instance to the invoked method, or use PrintingSystem's ExportOptions.Mht 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();
    // mhtExportFile specifies the exported file's name and path. The user's Downloads folder is used as the default path.
    string mhtExportFile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\Downloads\" + report.Name + ".mht";
    // Specify export options and export the document.
    // Uncomment the lines below to specify export options in an MhtExportOptions class instance.
    // MhtExportOptions MhtExportOptions = new MhtExportOptions();
    // MhtExportOptions.ExportMode = MhtExportMode.SingleFile;
    // report.PrintingSystem.ExportToMht(mhtExportFile, MhtExportOptions);
    
    // Comment the lines below if you specified export options in an MhtExportOptions class instance above.
    report.PrintingSystem.ExportOptions.Mht.ExportMode = MhtExportMode.SingleFile;
    report.PrintingSystem.ExportToMht(mhtExportFile);
    
  • Export a Control

    Call a PrintingLink.ExportToMht overloaded method to export a control. To specify export options, create a MhtExportOptions 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;
    // mhtExportFile specifies the exported file's name and path. The user's Downloads folder is used as the default path.
    string mhtExportFile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\Downloads\" + chart.Name + ".mht";
    // Specify export options and export the control.
    MhtExportOptions MhtExportOptions = new MhtExportOptions();
    MhtExportOptions.ExportMode = MhtExportMode.SingleFile;
    link.ExportToMht(mhtExportFile, MhtExportOptions);
    

Export Options

The options that can be specified for a document exported to an MHT file are stored in the MhtExportOptions class. Use the report's ExportOptions.Mht property to access these options.

The HtmlExportOptionsBase.ExportMode property determines how a document is exported to MHT. For instance, it may be exported to a single file (with a single page header at the start and a single page footer at the end) or it may be exported page-by-page to a single file or different files.

The HtmlExportOptionsBase.TableLayout property determines whether to use the table layout in the exported MHT file. The table layout is recommended, as it uses a more compact and efficient markup.

Overlapped Controls

To ensure that report controls are exported, 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.

ShowExportWarnings

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.

To export a report that contains overlapped controls, use the non-table layout.

Document Compatibility Options

The document styles are added to the MHT document's <head> section. Certain mail programs may ignore styles listed in this section. To maintain a consistent look for your reports, enable the HtmlExportOptionsBase.InlineCss property to specify style properties at the same place where styles are assigned.

This property also affects how the XRPictureBox and XRBarCode controls are rendered on a web page. Images in these controls are assigned to the background-image style property. Enable the HtmlExportOptionsBase.InlineCss property to render these controls as <img> elements in the document body and store the images in the src attribute in the base64 format.

Vector images (for instance, pictures, charts, or bar codes) are always rasterized on export to MHT. You can use the PageByPageExportOptionsBase.RasterizationResolution property to specify the image resolution.

Document navigation uses scripts. To use HTML link-based navigation instead, enable the HtmlExportOptionsBase.UseHRefHyperlinks property. When users view such a document in a client web browser, script security messages are not displayed. Ensure that the controls' Text properties are specfiied - otherwise links contain empty text and users cannot click them.

Limitations

If a report uses a CachedReportSource, changes made in Preview are not included in single-file exports.