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

Lesson 2 - Load a Document

  • 3 minutes to read

This is the second tutorial in the Getting Started series for the PDF Viewer control. It describes how to load a document in the PDF Viewer.

The tutorial consists of the following sections.

Prerequisites

Before loading a document, you need to add a PDF Viewer to your WPF application. Refer to How to: Add a PDF Viewer at Design Time and How to: Add a PDF Viewer via Code tutorials for more information.

Note

The PDF Viewer loads a document asynchronously (the default mode). Set the PdfViewerControl.AsyncDocumentLoad property to false to load a document in the main application thread.

Load from a File

This tutorial uses the Demo.pdf file included in the product installation and located at C:\Users\Public\Documents\DevExpress Demos 18.2\Components\Data\Demo.pdf . You can copy the file to your project’s subdirectory.

Use one of the following approaches to load a document from a file.

  1. Call the PdfViewerControl.OpenDocument method with a specified file path.

    pdfViewer.OpenDocument("..\\..\\Demo.pdf");
    

    Tip

    A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=T271077.

  2. Assign a path to the file using the DocumentViewerControl.DocumentSource property.

    pdfViewer.DocumentSource = "..\\..\\Demo.pdf";
    
  3. Use an Open dialog box at runtime.

    • Run the application.

    • Click the Open button or press CTRL+O to show the Open dialog box.

    PDFViewer_ClickOpen

    • Locate the document to be opened and click Open.

    Open

Important

The PDF document is locked for editing until it’s opened in the PDF Viewer. Set the PdfViewerControl.DetachStreamOnLoadComplete property to true to keep files unlocked.

Load from the Stream

The PDF Viewer can load a document from a stream obtained from various sources, for example, from an application resource, as shown below.

To load a document from a stream, Assign the stream object to the DocumentViewerControl.DocumentSource property to load a document from a stream.

Tip

A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=T263193.

using System.IO;
using System.Reflection;
using System.Windows;

namespace LoadPDFDocument
{

    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
            Stream stream = GetResourceStream("LoadPDFDocument.Demo.pdf");
            pdfViewer.DocumentSource = stream;
        }
        static Stream GetResourceStream(string resourceName)
        {
            return Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
        }
    }
}

Important

The input stream cannot not be closed until a document is opened. Set the PdfViewerControl.DetachStreamOnLoadComplete property to true before specifying the document source to allow closing the stream when the document is opened.

Load from a Uri

Create a Uri object with the specified base Uri and file path, and assign this object to the DocumentViewerControl.DocumentSource property.

Tip

A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=T263203.

using System;
using System.Windows;
using System.Reflection;
//...

Uri baseUri = new Uri(Assembly.GetEntryAssembly().Location);
Uri uri = new Uri(baseUri, "..\\..\\Demo.pdf");
pdfViewer.DocumentSource = uri;
See Also