How to protect by paragraph

Feb 26, 2015 at 1:12 AM
Hello All,
I have added the password function in the Docx project and it works well.
but it is to protect the whole document. Now, my client wants to protect by paragraph.
for example, he/she wants to add password only for first page and the second page is free to edit.
Is it possible.

thanks,
Feb 26, 2015 at 9:23 AM
As far as I know, you can only set a password in Word to protect an entire document. I'm not really familiar with document security though, so it could be I'm wrong about that.
There is an option in Word to protect parts of the document from being edited, though. Perhaps you could combine these two options? Use a password to grant access to the document and prevent parts from being edited by setting editing-permissions?

I'm pretty sure there's no way to set permissions using DocX. What needs to be done is this:
  • Add documentProtection to the word\settings.xml :
<w:settings>
...
<w:documentProtection w:edit="readOnly" w:enforcement="1"/>
...
</w:settings>
  • Add a permStart to the start of the first paragraph that should be editable, the id can be set to any number:
<w:p>
<w:permStart w:id="1154620435" w:edGrp="everyone"/>
...
  • Use that same id in the permEnd, which should be added at the end of the last editable paragraph:
...
<w:permEnd w:id="1154620435"/>
</w:p>
Besides the w:edGrp="everyone" you could also use w:ed="DOMAIN\user" or w:ed="userName" or w:ed="user@domain" to allow editing for specific people only. Multiple users would each get their own permStart and id:
<w:permStart w:id="897324928" w:ed="DOMAIN\Usera"/>
<w:permStart w:id="639189250" w:ed="DOMAIN\Userb"/>
I'm not sure if it's what you're looking for, but perhaps this information will be of help.
Feb 27, 2015 at 12:42 AM
Hello Annika89,
Thanks for the information. Based on your information, I use Openxml and I can create two paragraph. One is editable by everyone and the other is protected by the password.

thanks much,


Below is the working code.

using (WordprocessingDocument myDoc =
      WordprocessingDocument.Create(@"c:\\temp\bcd.docx",
                    WordprocessingDocumentType.Document))
        {


            // Add a new main document part. 
            MainDocumentPart mainPart = myDoc.AddMainDocumentPart();
            DocumentSettingsPart settingsPart = mainPart.AddNewPart<DocumentSettingsPart>();
            settingsPart.Settings = new Settings();
            settingsPart.Settings.Save();
            ApplyDocumentProtection(myDoc, "livepool");
            //Create Document tree for simple document. 
            mainPart.Document = new Document();
            //Create Body (this element contains
            //other elements that we want to include 
            Body body = new Body();
            //Create paragraph 
            Paragraph paragraph = new Paragraph();

            Run run_paragraph = new Run();
            // we want to put that text into the output document 

            RunProperties runPro = new RunProperties();
            RunFonts runFont = new RunFonts() { Ascii = "Cambria(Headings)", HighAnsi = "Cambria(Headings)" };
            Text text = new Text("TESTING");
            Bold bold = new Bold();

            Color color = new Color() { Val = "365F91", ThemeColor = ThemeColorValues.Accent1, ThemeShade = "BF" };
            runPro.Append(runFont);
            runPro.Append(bold);
            runPro.Append(text);
            runPro.Append(color);

            //Append elements appropriately. 
            run_paragraph.Append(runPro);
            PermStart permStart = new PermStart();
            permStart.EditorGroup = RangePermissionEditingGroupValues.Everyone;
            permStart.Id = 123;
            PermEnd permEnd = new PermEnd();
            permEnd.Id = 123;


            paragraph.Append(permStart);
            paragraph.Append(run_paragraph);

            paragraph.Append(permEnd);
            body.Append(paragraph);


            Paragraph paragraphd = new Paragraph();
            Run run_paragraphd = new Run();
            // we want to put that text into the output document 
            Text text_paragraph1 = new Text(" Editable!");
            //Append elements appropriately. 
            run_paragraphd.Append(text_paragraph1);
            paragraphd.Append(run_paragraphd);
            body.Append(paragraphd);


            mainPart.Document.Append(body);
            // Save changes to the main document part. 
            mainPart.Document.Save();
)