Esempio n. 1
0
        private static void RemoveRsidInfoInSettings(WordprocessingDocument doc)
        {
            DocumentSettingsPart part = doc.MainDocumentPart.DocumentSettingsPart;

            if (part == null)
            {
                return;
            }

            XDocument settingsXDoc = part.GetXDocument();

            settingsXDoc.Descendants(W.rsids).Remove();
            part.PutXDocument();
        }
Esempio n. 2
0
        public static OpenXmlPowerToolsDocument Lock(WmlDocument doc)
        {
            using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(doc))
            {
                using (WordprocessingDocument document = streamDoc.GetWordprocessingDocument())
                {
                    //Finds the settings part
                    XDocument settingsDocument;
                    XElement  documentProtectionElement = null;
                    if (document.MainDocumentPart.DocumentSettingsPart == null)
                    {
                        //If settings part does not exist creates a new one
                        DocumentSettingsPart settingsPart = document.MainDocumentPart.AddNewPart <DocumentSettingsPart>();
                        settingsDocument = settingsPart.GetXDocument();
                        settingsDocument.Add(new XElement(W.settings,
                                                          new XAttribute(XNamespace.Xmlns + "w", W.w),
                                                          new XAttribute(XNamespace.Xmlns + "r", R.r)));
                    }
                    else
                    {
                        //If the settings part does exist looks if documentProtection has been included
                        settingsDocument          = document.MainDocumentPart.DocumentSettingsPart.GetXDocument();
                        documentProtectionElement = settingsDocument.Element(W.settings).Element(W.documentProtection);
                    }

                    //Creates the documentProtection element, or edits it if it exists
                    if (documentProtectionElement == null)
                    {
                        settingsDocument
                        .Element(W.settings)
                        .Add(
                            new XElement(W.documentProtection,
                                         new XAttribute(W.edit, "readOnly")
                                         )
                            );
                    }
                    else
                    {
                        documentProtectionElement.SetAttributeValue(W.edit, "readOnly");
                    }
                    document.MainDocumentPart.DocumentSettingsPart.PutXDocument();
                }
                return(streamDoc.GetModifiedDocument());
            }
        }