Aishu
Aishu

Reputation: 413

Remove permStart and permEnd tags from document docx

I have 2 docx document, these document are read-only at document level. But edit option for few paragraphs in these document are enabled by using permStart and permEnd tags.

I have to merge these 2 document and make the new document editable. I'm using PowerTools DocumentBuilder to merge these 2 docx. The final docx is editable, but all the paragraphs are highlighted with gray background due to the presence of permStart and permEnd tags.

I'd like to know how we can remove these permStart and permEnd tags. I tried the following code but not working.

wordD.MainDocumentPart.Document.Body.RemoveAllChildren< PermStart>(); wordD.MainDocumentPart.Document.Body.RemoveAllChildren< PermEnd>();

I'm using OpenXML SDK2.0, VS2010, .NET 4.0 & Powertools Document Builder. Any help would be great.

Thank you!

Upvotes: 1

Views: 775

Answers (2)

Atul Verma
Atul Verma

Reputation: 2132

You need to remove the document protection. Along with this you also need to remove the PermStart and PermEnd since these tags are relevant only if document is protected. The code will be

  1. Remove the document protection.

    DocumentSettingsPart documentSettingsPart = wordprocessingDocument.MainDocumentPart.GetPartsOfType().FirstOrDefault();

        if (documentSettingsPart != null)
        {
            documentSettingsPart.Settings.RemoveAllChildren<DocumentProtection>();
        }
    
  2. Remove the PermStart and PermEnd tags as you are doing already

    wordD.MainDocumentPart.Document.Body.RemoveAllChildren(); wordD.MainDocumentPart.Document.Body.RemoveAllChildren(); wordD.MainDocumentPart.Document.Save();

Upvotes: 2

Aishu
Aishu

Reputation: 413

Here's the way I deleted the permStart and permEnd tags. Any improvement to the code below are welcome.

                foreach (PermStart p1 in wordD.MainDocumentPart.Document.Body.Descendants<PermStart>())
                {
                    p1.Parent.RemoveChild<PermStart>(p1);
                }

                foreach (PermEnd p2 in wordD.MainDocumentPart.Document.Body.Descendants<PermEnd>())
                {
                    p2.Parent.RemoveChild<PermEnd>(p2);
                }
                wordD.MainDocumentPart.Document.Save();

Upvotes: 1

Related Questions