Open a word processing document from a stream

OpenXML Word

The example 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 an open stream as the first parameter and the text to add as the second. For example, the following code example opens the file OpenDocumentFromStream.docx in the Public Documents folder and adds text to it.
Below is the sample code

            string txt = "Append text in body - OpenAndAddToWordprocessingStream";
            Stream stream = File.Open(OpenDocumentFromStream.docx, FileMode.Open);
            OpenAndAddToWordprocessingStream(stream, txt);
            stream.Close();    

Aspose.Words

Simply pass a stream object that contains a document to the Document constructor.
Below is the sample code

            string txt = "Append text in body - OpenAndAddToWordprocessingStream";
            //intialize stream by defining file mode
            Stream stream = File.Open(OpenDocumentFromStream.docx, FileMode.Open);
            OpenAndAddToWordprocessingStream(stream, txt);
            private static void OpenAndAddToWordprocessingStream(Stream stream, string txt)
            {
               // Load the entire document into memory.
               Document doc = new Document(stream);
               // You can close the stream now, it is no longer needed because the document is in memory.
               stream.Close();
               //intialize documentbuilder to insert text from stream into word document
               DocumentBuilder db = new DocumentBuilder(doc);
               db.Writeln(txt);
               doc.Save(OpenDocumentFromStream.docx);
            }

Download

Last edited May 26, 2015 at 5:21 AM by asposemarketplace, version 2