Simply pass a stream object that contains the document into the Document constructor.
The following code example shows how to open a document from a stream:
Stream stream = File.Open(MyDir + "Document.docx", FileMode.Open);
using (stream)
{
Document doc = new Document(stream);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("Append text in body - Open and add to wordprocessing stream");
doc.Save(ArtifactsDir + "Open document from stream - Aspose.Words.docx");
}
You can also do the same using the Open XML SDK. At the same time, note that it looks somewhat more complicated and more cumbersome.
The example of the OpenAndAddToWordprocessingStream method shown here can be used to open a Word document from an already open stream and append some text using the Open XML SDK. You can call it by passing a handle to the open stream as the first parameter and the text to add as the second. For example, the following code example opens the OpenDocumentFromStream.docx file in the Public Documents folder and adds text to it:
public void OpenDocumentFromStreamFeature()
{
using (Stream stream = File.Open(MyDir + "Document.docx", FileMode.Open))
{
using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(stream, true))
{
Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
Paragraph para = body.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(new Text("Append text in body - Open and add to wordprocessing stream"));
}
}
}