How to: Save, Load, or Export a Report

  • 3 minutes to read

This tutorial describes how to store a Snap report definition and export a published document to third-party formats.

This tutorial consists of the following sections.

Save and Restore a Report Layout

The following code illustrates how to save and open a document layout to the native Snap report format.


using DevExpress.Snap;
using DevExpress.Snap.Core.API;
// ...

// The following line saves a document in the Snap native format.
snapControl1.SaveDocument("1.snx", SnapDocumentFormat.Snap);

// The following line opens a specified document in a Snap application.
snapControl1.LoadDocument("1.snx", SnapDocumentFormat.Snap);
NOTE

Loading the mail merge templates with the Entity Framework data source may be unsafe if the data source is contained in a compiled assembly. Handle the SnapControl.BeforeLoadCustomAssembly event to allow loading a custom assembly. Otherwise, the data source throws the CustomAssemblyLoadingProhibitedException exception.

NOTE

Loading the mail merge templates with the ObjectDataSource data source may be unsafe if the data source is contained in a compiled assembly. Use the SnapControlOptions.DataSourceOptions property and a custom service that implements the IObjectDataSourceValidationService interface to validate an ObjectDataSource contained in the loaded document and prevent the data source from loading.

Open a Report as a Template

To apply the uniform layout and style settings of a specific document to multiple reports, open this document as a template.

In this mode, the Save command becomes disabled, to prevent opened templates from being overwritten.

To load a report as a template, use the LoadDocumentTemplate method.

The following code illustrates this functionality.


using DevExpress.Snap;
using DevExpress.Snap.Core.API;
// ...

snapControl1.LoadDocumentTemplate("1.snx", SnapDocumentFormat.Snap);

Save a Document to Third-Party Formats

A Snap report can be exported to the following file formats.

  • DOC (Microsoft® Word® 97 - 2003 document).

  • DOCX (Office® Open XML document);

  • HTML (HyperText Markup Language);

  • MHTML / MHT (Web archive, single file);

  • PDF (Portable Document Format);

  • RTF (Rich Text Format);

  • TXT (Plain text);

  • ODT (OpenDocument text format);

  • XML (Microsoft® Word® XML document);

  • Image (BMP, EMF, WMF, GIF, JPEG, PNG or TIFF format).

The following code illustrates how to export a document to PDF or PNG in Snap at runtime.


using DevExpress.Snap;
using DevExpress.XtraPrinting;
// ... 

private void SaveToPdf() {
    snapControl1.ExportToPdf("report.pdf");
}

private void SaveToPng() {
    PrintableComponentLink pcl = new PrintableComponentLink(new PrintingSystem());
    pcl.Component = snapControl1;
    pcl.CreateDocument(false);
    ImageExportOptions imgOptions = new ImageExportOptions();
    imgOptions.ExportMode = ImageExportMode.DifferentFiles;
    imgOptions.Format = System.Drawing.Imaging.ImageFormat.Png;
    imgOptions.Resolution = 150;
    imgOptions.PageRange = "1,3-5";
    pcl.ExportToImage("report.png", imgOptions);       
}