Open a word processing document for read-only access

Sometimes you want to open a document to inspect or retrieve some information, and you want to do so in a way that ensures the document remains unchanged. In these instances, you want to open the document for read-only access.

OpenXML Words

The below sample code shows how you can add some text and attempt to save the changes to show that access is read-only. Once you have access to the body of the main document part, you add text by adding instances of the Paragraph, Run, and Text classes. This generates the required WordprocessingML markup.

Below is the code sample

        public static void OpenWordprocessingDocumentReadonly(string filepath)
            // Open a WordprocessingDocument based on a filepath.
            using (WordprocessingDocument wordDocument =
                WordprocessingDocument.Open(filepath, false))
                // Assign a reference to the existing document body.  
                Body body = wordDocument.MainDocumentPart.Document.Body;

                // Attempt to add some text.
                Paragraph para = body.AppendChild(new Paragraph());
                Run run = para.AppendChild(new Run());
                run.AppendChild(new Text("Append text in body, but text is not saved       



Make the Document object having parameter filename and LoadOptions without setting password we cant protect or open document for read-only access . LoadOptions is a shortcut to initialize a new instance of this class with the specified password to load an encrypted document. Then initialize the DocumentBuilder object which provides methods to insert text, images and other content, specify font, paragraph and section formatting of word document.

Below is the sample code

        private static void OpenWordprocessingDocumentReadonly(string fileName)
            //make object of document class to get the filename and set the password
            Document doc = new Document(fileName, new LoadOptions("1234"));
            //intialize document builder to insert text in word document
            DocumentBuilder db = new DocumentBuilder(doc);
            string txt = "Append text in body - OpenAndAddToWordprocessingStream";
           //save the document file


Last edited May 26, 2015 at 4:20 AM by asposemarketplace, version 2