Back to Devexpress

RichEditDocumentServerExtensions.LoadDocumentAsync(RichEditDocumentServer, String, CancellationToken) Method

officefileapi-devexpress-dot-xtrarichedit-dot-richeditdocumentserverextensions-dot-loaddocumentasync-x28-devexpress-dot-xtrarichedit-dot-richeditdocumentserver-system-dot-string-system-dot-threading-dot-cancellationtoken-x29.md

latest6.6 KB
Original Source

RichEditDocumentServerExtensions.LoadDocumentAsync(RichEditDocumentServer, String, CancellationToken) Method

SECURITY-RELATED CONSIDERATIONS

Using file paths sourced from untrusted input may expose unauthorized files or allow unintended file access. Always validate and normalize all external paths to prevent path manipulation.

Asynchronously loads a document from a file. The file format is determined by its content.

Namespace : DevExpress.XtraRichEdit

Assembly : DevExpress.Docs.v25.2.dll

NuGet Package : DevExpress.Document.Processor

Declaration

csharp
public static Task<bool> LoadDocumentAsync(
    this RichEditDocumentServer self,
    string fileName,
    CancellationToken cancellationToken
)
vb
<ExtensionAttribute>
Public Shared Function LoadDocumentAsync(
    self As RichEditDocumentServer,
    fileName As String,
    cancellationToken As CancellationToken
) As Task(Of Boolean)

Parameters

NameTypeDescription
selfRichEditDocumentServer

Current RichEditDocumentServer instance.

| | fileName | String |

A string that specifies the file to load (including the full path).

| | cancellationToken | CancellationToken |

A CancellationToken object used to trace requests to cancel an operation.

|

Returns

TypeDescription
Task<Boolean>

A Task<TResult> object that returns true if the document is successfully loaded; otherwise, false.

|

Remarks

Important

The RichEditDocumentServerExtensions class is defined in the DevExpress.Docs.v25.2.dll assembly and DevExpress.Document.Processor NuGet package. Add this assembly or package to your project to use the RichEditDocumentServerExtensions members. You need a license for the DevExpress Office File API Subscription or DevExpress Universal Subscription to use this library in production code.

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;

To improve performance, define the format explicitly in the LoadDocument method overloads.

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 string value is null , the RichEditDocumentServer.InvalidFormatException event fires.

After loading a document, the DocumentSaveOptions.CurrentFileName property is set to the file name and the DocumentSaveOptions.CurrentFormat property is set to the detected format.

Use the RichEditDocumentServer.DocumentLoaded event to determine the moment when the document model can be safely modified. Handle the DocumentLayout.DocumentFormatted event to check the loaded document layout.

Important

Take into account the following when you call this method:

  • The events fired by this method call may occur in a different thread than the target operation.

  • The operation is not thread safe (the document should not be accessed simultaneously by different threads). Wait until the operation is completed before you continue to work with the document (for example, use the await operator).

The code sample below asynchronously loads a document and exports it to the PDF format using the CancellationToken object:

csharp
private async void Convert2PdfWithCancellation()
{
    //10 second limit
    using (CancellationTokenSource source = new CancellationTokenSource(10000))
    {
        using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
        {
            try
            {
                await wordProcessor.LoadDocumentAsync("Document.docx", source.Token);
                await wordProcessor.ExportToPdfAsync("result.pdf");
            }
            catch (OperationCanceledException)
            {
                // Your code to handle cancellation
            }
        }
    }
}
vb
Private Async Sub Convert2PdfWithCancellation()
  '10 second limit
  Using source As New CancellationTokenSource(10000)
    Using wordProcessor As New RichEditDocumentServer()
      Try
        Await wordProcessor.LoadDocumentAsync("Document.docx", source.Token)
        Await wordProcessor.ExportToPdfAsync("result.pdf")
      Catch e1 As OperationCanceledException
        ' Your code to handle cancellation
      End Try
    End Using
  End Using
End Sub

See Also

RichEditDocumentServerExtensions Class

RichEditDocumentServerExtensions Members

DevExpress.XtraRichEdit Namespace