Skip to main content
A newer version of this page is available. .

DocumentViewer.DocumentSource Property

Gets or sets the DocumentViewer‘s document source. This is a dependency property.

Namespace: DevExpress.WinUI.DocumentViewer

Assembly: DevExpress.WinUI.DocumentViewer.v21.2.dll

Declaration

public object DocumentSource { get; set; }

Property Value

Type Default Description
Object

null

The document source.

Remarks

You can use the DocumentViewer to preview, print, save, and export the following files and objects:

Objects implementing the IReport interface

A report layout.

The DevExpress Reports Suite subscription is required to display an object that implements the IReport interface in the DocumentViewer.

Refer to the following topic for more information on how to create a report layout: Create a Report in Visual Studio.

Objects implementing the ILink interface.

You can use the DocumentViewer to preview, print, and export the GridContol‘s data.

Refer to the following topic for more information on how to print a GridControl: Print and Export Data

Report Documents (.PRNX file)

A report document that is built from a report layout.

Refer to the following topic for more information on .PRNX files: Store Report Documents

Display a Report Layout (IReport Interface)

The DevExpress Reports Suite subscription is required to display an object that implements the IReport interface in the DocumentVeiwer.

Follow the steps below to display a Report Layout in the DocumentViewer:

  1. Reference the DevExpress.Reporting.Core NuGet package.
  2. Create an object that implements the IReport interface (in this case, the XtraReport class).
  3. Load a Report Layout (.REPX file) from a file or Stream. Refer to the following topic for more information on how to create a report layout: Create a Report in Visual Studio.
  4. Call any of the CreateDocument methods to create a Report Document from the Report Layout.
  5. Assign the created Report Document to the DocumentSource property.

Specify an Absolute File Path

The following code sample displays the Report Document generated from the .REPX file in the DocumentViewer:

<Window ...
    xmlns:dxdvr="using:DevExpress.WinUI.DocumentViewer">
    <dxdvr:DocumentViewer x:Name="documentviewer" Loaded="DocumentViewer_Loaded" />
</Window>
using Microsoft.UI.Xaml;
using DevExpress.XtraReports.UI;

// ...
private async void DocumentViewer_Loaded(object sender, RoutedEventArgs e) {
    // Create an XtraReport instance.
    XtraReport report = new XtraReport();
    // Load a Report Layout from a .REPX file.
    report.LoadLayoutFromXml(@"C:\Invoice.repx");
    // Create a Report Document from the Report Layout.
    report.CreateDocument();
    // Pass the Report Document to the DocumentViewer.
    documentviewer.DocumentSource = report;
}

Specify a File from Stream

The code sample below performs the following actions:

  • Gets a .REPX file (Report Layout) from an Uri.
  • Creates a Stream with the Report Layout.
  • Generates a Report Document from the Report Layout.
  • Displays the Report Document in the DocumentViewer.
<Window ...
    xmlns:dxdvr="using:DevExpress.WinUI.DocumentViewer">
    <dxdvr:DocumentViewer x:Name="documentviewer" Loaded="DocumentViewer_Loaded" />
</Window>
using Microsoft.UI.Xaml;
using DevExpress.XtraReports.UI;
using Windows.Storage;

private async void DocumentViewer_Loaded(object sender, RoutedEventArgs e) {
    // Create a Stream and load a .REPX file from URI.
    var data = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///data/SampleReport.repx"));
    // Open the Stream and read it.
    var stream_data = await data.OpenStreamForReadAsync();

    // Create an XtraReport instance.
    XtraReport report = new XtraReport();
    // Load a Report Layout from the stream.
    report.LoadLayoutFromXml(stream_data, false);
    // Create a Report Document from the Report Layout.
    report.CreateDocument();
    // Pass the Report Document to the DocumentViewer.
    documentviewer.DocumentSource = report;
}

The DocumentViewer allows you to display a print preview of the GridControl and save, export, or print the GridControl’s data.

The following code sample displays the print preview of the GridControl in the DocumentViewer:

<Window ...
    xmlns:dxg="using:DevExpress.WinUI.Grid"
    xmlns:dxdvr="using:DevExpress.WinUI.DocumentViewer">
    <StackPanel Orientation="Horizontal">
            <StackPanel>
                <dxg:GridControl x:Name="gridcontrol" AutoGenerateColumns="False" ItemsSource="{x:Bind ViewModel.Products}" Width="500">
                    <dxg:GridControl.Columns>
                        <dxg:GridTextColumn FieldName="ProductName"/>
                        <dxg:GridTextColumn FieldName="UnitPrice"/>
                        <dxg:GridTextColumn FieldName="Quantity"/>
                    </Grid:GridControl.Columns>
                </dxg:GridControl>
                <Button Click="Button_Click" Content="Show Print Preview" Padding="10,10,10,10" HorizontalAlignment="Center"/>
            </StackPanel>
            <dxdv:DocumentViewer x:Name="documentviewer" DocumentSource="{x:Bind PrintDocument, Mode=OneWay}" Visibility="Collapsed" Width="800" Height="700" />
        </StackPanel>
</Window>
using Microsoft.UI.Xaml;
using DevExpress.XtraReports.UI;
using Windows.Storage;

private async void DocumentViewer_Loaded(object sender, RoutedEventArgs e) {
    PrintableLink printDocument;

    public PrintableLink PrintDocument {
        get => printDocument;
        private set {
            if (printDocument != value) {
                printDocument = value;
            }
        }
    }

    private void Button_Click(object sender, RoutedEventArgs e) {
        var printer = new GridControlPrinter(gridcontrol);
        PrintDocument = new PrintableLink(printer);
        documentviewer.Visibility = Visibility.Visible;
        documentviewer.DocumentSource = PrintDocument;
    }
}

Display a Report Document (.PRNX File)

You can pass a .PRNX file (Report Document) to the DocumentSource property in the following formats:

You can use the Open dialog to open .PRNX files in the DocumentViewer. Use the ShowOpenDialogCommand or click the Open button in the UI to open the Open dialog.

Pass a File’s Absolute Path

The following code sample displays the local .PRNX file passed to the DocumentViewer instance as Path.

In XAML
<Window ...
    xmlns:dxdvr="using:DevExpress.WinUI.DocumentViewer">
    <dxdvr:DocumentViewer DocumentSource="C:\data\Invoice.prnx"/>
</Window>
In Code-Behind
<Window ...
    xmlns:dxdvr="using:DevExpress.WinUI.DocumentViewer">
    <dxdvr:DocumentViewer x:Name="documentviewer" Loaded="DocumentViewer_Loaded" />
</Window>
using Microsoft.UI.Xaml;
using System;
using System.IO;
using Windows.Storage;

private void DocumentViewer_Loaded(object sender, RoutedEventArgs e) {
    documentviewer.DocumentSource = @"C:\data\Invoice.prnx";
}

Pass a File as URI

When you display a file from an Uri, set the project .PRNX file’s Build Action and Copy to Output Directory properties to Content and Copy if newer (or Copy always), respectively.

WinUI DocumentViewer - Report Properties

The code samples below displays the local .PRNX file passed to the DocumentViewer instance as Uri.

In XAML
<Window ...
    xmlns:dxdvr="using:DevExpress.WinUI.DocumentViewer">
    <dxdvr:DocumentViewer DocumentSource="ms-appx:///data/Invoice.prnx"/>
</Window>
In Code-Behind
<Window ...
    xmlns:dxdvr="using:DevExpress.WinUI.DocumentViewer">
    <dxdvr:DocumentViewer x:Name="documentviewer" Loaded="DocumentViewer_Loaded" />
</Window>
using Microsoft.UI.Xaml;
using System;
using System.IO;
using Windows.Storage;

// ...
private void DocumentViewer_Loaded(object sender, RoutedEventArgs e) {
    documentviewer.DocumentSource = new Uri("ms-appx:///data/Invoice.prnx");
}

Pass a File as Stream (Code-Behind Only)

The following code sample displays the local .PRNX file from a Stream:

<Window ...
    xmlns:dxdvr="using:DevExpress.WinUI.DocumentViewer">
    <dxdvr:DocumentViewer x:Name="documentviewer" Loaded="DocumentViewer_Loaded" />
</Window>
using Microsoft.UI.Xaml;
using System;
using System.IO;
using Windows.Storage;

// ...
private async void DocumentViewer_Loaded(object sender, RoutedEventArgs e) {
    var data = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///data/Invoice.prnx"));
    documentviewer.DocumentSource = await data.OpenStreamForReadAsync();
}

The DocumentViewer raises the DocumentLoaded event when a document is loaded.

Next Steps

Use the DevExpress Report Suite’s API to specify a .REPX parameter value. Refer to the following topic for more information on the report parameters: Specify Parameter Values.

Use the Save(String) method to save the opened document. You can also use the ShowSaveDialogCommand property to display the Save dialog.

Use the Export(ExportFormats, String) method to export the opened document as a file in different formats. You can also use the ShowExportDialogCommand property to display the Export dialog.

Use the ShowPrintDialogCommand to display the print dialog where you can print the opened document.

See Also