Show default view pane in Word Client

What I want : I am editing a WordprocessingDocument and adding some tracked changes. This part is completed. Now I want the word MS to display all changes by default, that is, it does not require the user to click on the red sidebar to open tracked changes in the document.

red sidebar

What I did: For this, I found the RevisionView class, which adds the xml <w:revisionView /> element to settings.xml under the w:settings element. RevisionView has some properties, such as Comments , DisplayRevision , Formatting , etc. I explicitly set them all to true .

 RevisionView revView = new RevisionView(); revView.DisplayRevision = new OnOffValue(true); revView.Formatting = new OnOffValue(true); revView.InkAnnotations = new OnOffValue(true); revView.Markup = new OnOffValue(true); revView.Comments = new OnOffValue(true); 

and then I added this revView to Settings :

 Settings settings = wordprocessingDocument.MainDocumentPart.DocumentSettingsPart.Settings; settings.RemoveAllChildren<RevisionView>(); settings.AppendChild(revView); settings.Save(); 

And then I looked at the xml document explicitly, it adds the following xml to Settings :

 <w:revisionView w:markup="true" w:comments="true" w:insDel="true" w:formatting="true" w:inkAnnotations="true" /> 

But adding this item to the settings does not affect the view. It does not show changes open by default.

Then, for testing purposes, I manually changed the zoom element in settings.xml from <w:zoom w:percent="100" /> to <w:zoom w:percent="120" /> . I expected the word to scale for this document from 100 to 120 . But this is not so, the increase was 100 even after switching to 120 in settings.xml .

One more thing: I cannot use interop, since I need to deploy this on the server, so I do all this with OpenXmlSdk.

What am I asking:

  • Is it possible to do what I want?

  • If so, what am I doing wrong? Is RevisionView option I should rely on?

  • Is there a way to force the word to apply (override the default application level settings) the parameters specified in settings.xml ?

  • Why doesn't the word scale from 100 to 120?

+5
source share
1 answer

Here are the answers to your questions:

Can I do what I want?

What you are trying to do is: opening the docx file, the browse panel will automatically open. I could not find a way to get the Word client to do this using OpenXml.

If so, what am I doing wrong? Is RevisionView an option I should rely on?

Impossible, so the answer here is no.

Is there a way to force the word to apply (override the default application level settings) the parameters specified in settings.xml?

Yes, using the OpenXML SDK . Settings The part has properties that you can control with code to change the default behavior of the Word client.

Why doesn't the word scale from 100 to 120?

I can not answer this question without seeing your file. You may not have saved the file correctly when editing the file manually.

I managed to create a simple console application with the following code. The application will change the zoom level for any Word file by 120%. You need to add your path to the file.

I generated most of this code using the OpenXml Productivity Tool. .

Note. When creating this in Visual Studio, be sure to add DocumentFormat.OpenXml and WindowsBase to your projects.

 using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; namespace ConsoleApp4 { class Program { private static WordprocessingDocument document; private static System.Collections.Generic.IDictionary<System.String, OpenXmlPart> UriPartDictionary = new System.Collections.Generic.Dictionary<System.String, OpenXmlPart>(); private static System.Collections.Generic.IDictionary<System.String, DataPart> UriNewDataPartDictionary = new System.Collections.Generic.Dictionary<System.String, DataPart>(); static void Main(string[] args) { using (document = WordprocessingDocument.Open("<DOCX FILE PATH HERE>", true)) { BuildUriPartDictionary(); ChangeParts(); } } private static void BuildUriPartDictionary() { System.Collections.Generic.Queue<OpenXmlPartContainer> queue = new System.Collections.Generic.Queue<OpenXmlPartContainer>(); queue.Enqueue(document); while (queue.Count > 0) { foreach (var part in queue.Dequeue().Parts) { if (!UriPartDictionary.Keys.Contains(part.OpenXmlPart.Uri.ToString())) { UriPartDictionary.Add(part.OpenXmlPart.Uri.ToString(), part.OpenXmlPart); queue.Enqueue(part.OpenXmlPart); } } } } private static void ChangeParts() { ChangeDocumentSettingsPart1(((DocumentSettingsPart)UriPartDictionary["/word/settings.xml"])); } private static void ChangeDocumentSettingsPart1(DocumentSettingsPart documentSettingsPart) { Settings settings1 = documentSettingsPart.Settings; Zoom zoom1 = settings1.GetFirstChild<Zoom>(); zoom1.Percent = "120"; } } } 
+1
source

Source: https://habr.com/ru/post/1273016/


All Articles