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

Lesson 2 - Loading a Document

  • 3 minutes to read

This tutorial in the Getting Started series for the PDF Viewer control. It describes several ways of loading a document in the PDF Viewer and consists of the following sections:

Before loading a document, you need to add a PDF Viewer to your WinForms application. Refer to How to: Add a PDF Viewer to the WinForms Application at Design Time and How to: Add a PDF Viewer to the WinForms Application via Code tutorials on how to do this.

Note

This tutorial uses the Demo.pdf that comes shipped with the WinForms PDF Viewer demo. Copy the file to the subdirectory of your project. This file is located in the following folder by default:

C:\Users\Public\Documents\DevExpress Demos 17.2\Components\Data\Demo.pdf

Load from a File

Use one of the following approaches:

  • Call the PdfViewer.LoadDocument method with a specified file path.

    
    Viewer.LoadDocument("..\\..\\Demo.pdf"); 
    

    Tip

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

  • At design time, click the PdfViewer’s smart tag to invoke its actions list. In the Actions list, click the Load PDF file… link.

    smart-tag-ribbon-load-pdf-file

    The Open dialog box appears. In this dialog, locate the document to be opened and click Open.

    LoadPDF_OpenDialog

    or…

  • Use an Open dialog box at runtime.

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

      Viewer_OpenRibbonButton

    • In the dialog, click Open to load a desired document to the PDF Viewer.

      Viewer_InvokeRuntimeOpenDialiog

Important

If the PdfViewer.DetachStreamAfterLoadComplete property is set to false (default mode) the PDF Viewer locks a file until it is opened in the PDF Viewer. If you want the PDF Viewer does not lock the file, set the PdfViewer.DetachStreamAfterLoadComplete property to true.

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, pass the stream object as a parameter to one of the PdfViewer.LoadDocument overloaded methods.


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

namespace LoadDocumentFromStream {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();

            Stream stream = GetResourceStream("LoadDocumentFromStream.Demo.pdf");
            pdfViewer1.LoadDocument(stream);
        }

        static Stream GetResourceStream(string resourceName) {
            return Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
        }
    }
}

Tip

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

Important

If the PdfViewer.DetachStreamAfterLoadComplete property is set to false (default mode), the input stream should not be closed until a document is opened.

If you want to close the stream when a document is opened, set the PdfViewer.DetachStreamAfterLoadComplete property to true before specifying the document source.

See Also