Back to Devexpress

Document.LoadDocument(Stream) Method

officefileapi-devexpress-dot-xtrarichedit-dot-api-dot-native-dot-document-dot-loaddocument-x28-system-dot-io-dot-stream-x29.md

latest4.5 KB
Original Source

Document.LoadDocument(Stream) Method

Loads a document from the stream.

Namespace : DevExpress.XtraRichEdit.API.Native

Assembly : DevExpress.RichEdit.v25.2.Core.dll

NuGet Package : DevExpress.RichEdit.Core

Declaration

csharp
bool LoadDocument(
    Stream stream
)
vb
Function LoadDocument(
    stream As Stream
) As Boolean

Parameters

NameTypeDescription
streamStream

A Stream object that is the stream used to load a document.

|

Returns

TypeDescription
Boolean

true , if the document is successfully loaded; otherwise, false.

|

Remarks

The source stream can be non-seekable. In this case, the stream is buffered automatically. After loading the document, the stream remains open.

The format of the document loaded from a stream is detected automatically by the built-in IFormatDetectorService service implementation. The following formats are detected:

  • DOC, DOCM, DOTX, DOT, DOTM, DOCX, RTF, HTM, HTML, MHT, XML, FlatOpc, EPUB;
  • ODT - non-encrypted files only.

Plain text cannot be detected automatically. To load a plain text document, use another method override with DocumentFormat.PlainText as a parameter.

If the format detection fails or the passed stream is null , the RichEditControl.InvalidFormatException event fires.

Use the RichEditControl.DocumentLoaded (WinForms), RichEditDocumentServer.DocumentLoaded or RichEditControl.DocumentLoaded (WPF) event to determine a moment when the document model can be safely modified. Handle the DocumentLayout.DocumentFormatted event to check the loaded document’s layout.

Tip

Define the format explicitly in the LoadDocument method overloads to improve performance.

csharp
using System.IO;
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using System.Reflection;
// ...
//
// Load document from stream
using(Stream stream = GetResourceStream("MyApplication.Document1.rtf"))
{
    stream.Seek(0, SeekOrigin.Begin);
    wordProcessor.LoadDocument(stream);
    stream.Close();
}
static Stream GetResourceStream(string resourceName)
{
    return Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
}
vb
Imports System.IO
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native
Imports System.Reflection
' ...
' Load document from stream
Using stream As Stream = GetResourceStream("MyApplication.Document1.rtf")
    stream.Seek(0, SeekOrigin.Begin)
    richEditDocumentServer1.LoadDocument(stream)
    stream.Close()
End Using

Private Shared Function GetResourceStream(ByVal resourceName As String) As Stream
    Return Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)
End Function
' Load document from file
Private Sub btnLoadFile_Click(ByVal sender As Object, ByVal e As EventArgs) _
        Handles btnLoadFile.Click
    richEditControl1.LoadDocument("Document1.rtf",DocumentFormat.Rtf)
End Sub

See Also

Document Interface

Document Members

DevExpress.XtraRichEdit.API.Native Namespace