Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

Manage Report Documents in Code

  • 4 minutes to read

#Reporting for WinForms

Code snippets in this section show how to use the report’s SaveDocument and LoadDocument methods to save and load report documents.

#Save a Document to a File

// Specify the path to a report document file.
string filePath = @"C:\Temp\Report1.prnx";

// Create a report instance. 
XtraReport1 report = new XtraReport1();

// Create a report document.
report.CreateDocument();

// Save the created report document to a file. 
report.PrintingSystem.SaveDocument(filePath);

#Load a Document from a File

using DevExpress.XtraReports.UI;
// ...

// Specify the path to a report document file. 
string documentPath = @"C:\Temp\Report1.prnx";

// Specify the file to which the report document should be exported.
string exportPath = @"C:\Temp\Report1.docx";

// Create a new report instance.
XtraReport report = new XtraReport();

// Load the report document.
if (System.IO.File.Exists(documentPath)) {
    report.PrintingSystem.LoadDocument(documentPath);
}
else {
    System.Console.WriteLine("The source file does not exist.");
}

// Export the loaded report document to DOCX format.
report.ExportToDocx(exportPath);

#Save a Document to a Stream

// Create a Memory Stream.
using (MemoryStream stream = new MemoryStream()) {

    // Create a report instance. 
    XtraReport1 report = new XtraReport1();

    // Create a report document.
    report.CreateDocument();

    // Save the created report document to the stream. 
    report.PrintingSystem.SaveDocument(stream);
}

#Load a Document from a Stream

using DevExpress.XtraReports.UI;
// ...

// Declare a data byte array that should store the document.
byte[] reportBytes;

// Create a report instance. 
XtraReport report = new XtraReport();

// Create a Memory Stream.
using (MemoryStream stream = new MemoryStream(reportBytes)) {

    // Load the report document.
    report.PrintingSystem.LoadDocument(stream);
}

// Send the loaded report document to the specified printer.
report.Print("PrinterName");

#Reporting for WPF

Add the DocumentViewerControl component to your project. Assign a document source to the DocumentSource property.

The following types of document sources are supported:

  • An object that implements the IReport interface.
  • An object that implements the ILink interface.
  • A Stream that contains report document data.
  • A string that contains a path to a file that stores report document data (a PRNX file).

If you use the IReport or ILink object as a document source, call the document source’s CreateDocument method or set the DocumentPreviewControl’s RequestDocumentCreation property to True to generate a preview.

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" xmlns:dxp="http://schemas.devexpress.com/winfx/2008/xaml/printing" Loaded="Window_Loaded">
    <Grid>
        <dxp:DocumentPreviewControl x:Name="documentPreview" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
    </Grid>
</Window>