Load document

The text provides a comprehensive guide on how to load documents using the GroupDocs.Editor for .NET API. Below is a revised version that improves readability, consistency, and clarity.


Load Document

This guide explains how to load a document from a local disk or file stream for editing using the GroupDocs.Editor for .NET API.

Introduction

In this article, you will learn how to load an input document into GroupDocs.Editor and apply load options.

Loading Documents

To load an input document, which should be accessible either as a byte stream or through a valid file path, you can create an instance of the Editor class using one of its constructor overloads. Below are examples of loading documents from a file path and from a stream.

// Load document from file path
string inputFilePath = "C:\\input_path\\document.docx"; // Path to the document
Editor editor = new Editor(inputFilePath);

// Load document from stream
FileStream inputStream = System.IO.File.OpenRead(inputFilePath);
Editor editor = new Editor(inputStream);

When using the constructor overloads shown above, GroupDocs.Editor automatically detects the format of the input document and applies the most suitable default loading options. However, it is recommended to specify the correct loading options explicitly by using constructor overloads that accept two parameters. Here is how you can do this:

// Load document from file path with load options
string inputFilePath = "C:\\input_path\\document.docx"; // Path to the document
WordProcessingLoadOptions wordLoadOptions = new WordProcessingLoadOptions();
Editor editor = new Editor(inputFilePath, wordLoadOptions); // Passing path and load options to the constructor

// Load document from stream with load options
MemoryStream inputStream = new MemoryStream(); // Stream obtained from somewhere
SpreadsheetLoadOptions spreadsheetLoadOptions = new SpreadsheetLoadOptions();
Editor editor = new Editor(inputStream, spreadsheetLoadOptions);

Load Options

Please note that not all document formats have associated classes for load options. As of version 22.7, only WordProcessing, Spreadsheet, Presentation formats, and a distinct PDF format have specific load options classes. Other formats, such as DSV, TXT, or XML, do not have load options.

Format FamilyExample FormatsLoad Options Class
WordProcessingDOC, DOCX, DOCM, DOT, ODTWordProcessingLoadOptions
SpreadsheetXLS, XLSX, XLSM, XLSBSpreadsheetLoadOptions
PresentationPPT, PPTX, PPS, POTPresentationLoadOptions
Fixed-layout formatPDFPdfLoadOptions

Handling Password-Protected Documents

Using load options is essential when working with password-protected documents. Any document can be loaded into the Editor instance, even if it is password-protected. However, if the password is not handled correctly, an exception will be thrown during the editing process. Here’s how GroupDocs.Editor handles passwords:

  1. If the document is not password-protected, any specified password will be ignored.
  2. If the document is password-protected but no password is specified, a PasswordRequiredException will be thrown during editing.
  3. If the document is password-protected and an incorrect password is provided, an IncorrectPasswordException will be thrown during editing.

The example below demonstrates how to specify a password for opening a password-protected WordProcessing document:

Stream inputStream = GetDocumentStreamFromSomewhere();
WordProcessingLoadOptions wordLoadOptions = new WordProcessingLoadOptions();
wordLoadOptions.Password = "correct_password";
Editor editor = new Editor(inputFilePath, wordLoadOptions);
Note
The same approach applies to Spreadsheet, Presentation, and PDF documents as well.

This revision enhances clarity, improves structure, and ensures that the instructions are easy to follow.