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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
|
|
using (Stream stream = File.Open(MyDir + "Document.docx", FileMode.Open))
|
|
{
|
|
Document doc = new Document(stream);
|
|
DocumentBuilder builder = new DocumentBuilder(doc);
|
|
|
|
builder.Writeln();
|
|
builder.Write("This is the text added to the end of the document.");
|
|
|
|
doc.Save(ArtifactsDir + "Add text 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 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 file in the Public Documents folder and adds text to it:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
|
|
using (Stream inStream = File.Open(MyDir + "Document.docx", FileMode.Open))
|
|
{
|
|
using (WordprocessingDocument originalDocument = WordprocessingDocument.Open(inStream, false))
|
|
{
|
|
using (Stream outStream = File.Create(ArtifactsDir + "Add text stream - OpenXML.docx"))
|
|
{
|
|
// Create a new Wordprocessing document.
|
|
using (WordprocessingDocument newDocument = WordprocessingDocument.Create(outStream, WordprocessingDocumentType.Document))
|
|
{
|
|
// Add a main document part to the new document.
|
|
MainDocumentPart newMainPart = newDocument.AddMainDocumentPart();
|
|
newMainPart.Document = new Document(new Body());
|
|
|
|
// Copy content from the original document to the new document.
|
|
MainDocumentPart originalMainPart = originalDocument.MainDocumentPart;
|
|
newMainPart.Document.Body = (Body)originalMainPart.Document.Body.Clone();
|
|
|
|
Body body = newMainPart.Document.Body;
|
|
|
|
// Create a new paragraph with the text you want to add
|
|
Paragraph newParagraph = new Paragraph();
|
|
Run newRun = new Run();
|
|
Text newText = new Text("This is the text added to the end of the document.");
|
|
newRun.Append(newText);
|
|
newParagraph.Append(newRun);
|
|
|
|
// Append the new paragraph to the body
|
|
body.Append(newParagraph);
|
|
|
|
// Save the changes
|
|
newMainPart.Document.Save();
|
|
}
|
|
}
|
|
}
|
|
} |