Document(Stream) Constructor

Opens an existing document from a stream. Automatically detects the file format.

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

Syntax

public Document(
	Stream stream
)

Parameters

stream
Type: System.IOStream
Stream where to load the document from.

Exceptions

ExceptionCondition
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.
ArgumentNullExceptionThe stream cannot be null.
NotSupportedExceptionThe stream does not support reading or seeking.
ObjectDisposedExceptionThe stream is a disposed object.

Remarks

The document must be stored at the beginning of the stream. The stream must support random positioning.

Examples

Shows how to open a document from a stream.
// Open the stream. Read only access is enough for Aspose.Words to load a document.
using (Stream stream = File.OpenRead(MyDir + "Document.docx"))
{
    // Load the entire document into memory and read its contents
    Document doc = new Document(stream);

    Assert.AreEqual("Hello World!", doc.GetText().Trim());
}
Shows how to retrieve a document from a URL and saves it to disk in a different format.
// This is the URL address pointing to where to find the document
const string url = "https://omextemplates.content.office.net/support/templates/en-us/tf16402488.dotx";

// The easiest way to load our document from the internet is make use of the 
// System.Net.WebClient class. Create an instance of it and pass the URL
// to download from.
using (WebClient webClient = new WebClient())
{
    // Download the bytes from the location referenced by the URL
    byte[] dataBytes = webClient.DownloadData(url);

    // Wrap the bytes representing the document in memory into a MemoryStream object
    using (MemoryStream byteStream = new MemoryStream(dataBytes))
    {
        // Load this memory stream into a new Aspose.Words Document
        // The file format of the passed data is inferred from the content of the bytes itself
        // You can load any document format supported by Aspose.Words in the same way
        Document doc = new Document(byteStream);

        // Convert the document to any format supported by Aspose.Words and save
        doc.Save(ArtifactsDir + "Document.OpenDocumentFromWeb.docx");
    }
}

See Also