Document(String, LoadOptions) Constructor

Opens an existing document from a file. Allows to specify additional options such as an encryption password.

Namespace:  Aspose.Words
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.9.0


public Document(
	string fileName,
	LoadOptions loadOptions


Type: SystemString
File name of the document to open.
Type: Aspose.WordsLoadOptions
Additional options to use when loading a document. Can be null.


UnsupportedFileFormatExceptionThe document format is not recognized or not supported.
FileCorruptedExceptionThe document appears to be corrupted and cannot be loaded.
ExceptionThere is a problem with the document and it should be reported to Aspose.Words developers.
IOExceptionThere is an input/output exception.
IncorrectPasswordExceptionThe document is encrypted and requires a password to open, but you supplied an incorrect password.
ArgumentExceptionThe name of the file cannot be null or empty string.


Shows how to load an encrypted Microsoft Word document.
Document doc;

// Aspose.Words throw an exception if we try to open an encrypted document without its password.
Assert.Throws<IncorrectPasswordException>(() => doc = new Document(MyDir + "Encrypted.docx"));

// When loading such a document, the password is passed to the document's constructor using a LoadOptions object.
LoadOptions options = new LoadOptions("docPassword");

// There are two ways of loading an encrypted document with a LoadOptions object.
// 1 -  Load the document from the local file system by filename.
doc = new Document(MyDir + "Encrypted.docx", options);

// 2 -  Load the document from a stream.
using (Stream stream = File.OpenRead(MyDir + "Encrypted.docx"))
    doc = new Document(stream, options);
Shows how to create and load documents.
// There are two ways of creating a Document object using Aspose.Words.
// 1 -  Create a blank document.
Document doc = new Document();

// New Document objects by default come with a section, body, and paragraph;
// the minimal set of nodes required to begin editing.
doc.FirstSection.Body.FirstParagraph.AppendChild(new Run(doc, "Hello world!"));

// 2 -  Load a document that exists in the local file system.
doc = new Document(MyDir + "Document.docx");

// Loaded documents will have contents that we can access and edit.
Assert.AreEqual("Hello World!", doc.FirstSection.Body.FirstParagraph.GetText().Trim());

// Some operations that need to occur during loading, such as using a password to decrypt a document,
// can be done by passing a LoadOptions object when loading the document.
doc = new Document(MyDir + "Encrypted.docx", new LoadOptions("docPassword"));

Assert.AreEqual("Test encrypted document.", doc.FirstSection.Body.FirstParagraph.GetText().Trim());

See Also