Back to Devexpress

How to: Load a PDF Document from a Stream

windowsforms-114436-controls-and-libraries-pdf-viewer-examples-file-operations-how-to-load-a-pdf-document-from-a-stream.md

latest2.0 KB
Original Source

How to: Load a PDF Document from a Stream

  • May 16, 2019

The following example illustrates how to load a document into the PDF Viewer from a stream at runtime.

Create a FileStream object with the specified file path to open the existing file, and call one of the PdfViewer.LoadDocument overloaded method with this stream object passed as a parameter.

Note

The PDF Viewer expects that the input stream is not modified or closed before the component finishes using a document. Set true as the PdfViewer.DetachStreamAfterLoadComplete property value to force the PDF Viewer to complete all input operations after loading a document. Set this property to false to lock files by the input stream (i.e., make it impossible to edit or delete the document until it is opened in the PDF Viewer) and render large PDF files faster.

View Example

csharp
using System.IO;
using System.Windows.Forms;

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

            // Load a document from the stream.
            FileStream stream = new FileStream("..\\..\\Demo.pdf", FileMode.Open);
            pdfViewer1.LoadDocument(stream);
        }
    }
}
vb
Imports System.IO
Imports System.Windows.Forms

Namespace LoadDocumentFromStream
    Partial Public Class Form1
        Inherits Form

        Public Sub New()
            InitializeComponent()

            ' Load a document from the stream.
            Dim stream As New FileStream("..\..\Demo.pdf", FileMode.Open)
            pdfViewer1.LoadDocument(stream)
        End Sub
    End Class
End Namespace