Adding restrictions and password back into document

We have a program that takes a bunch of documents from the database. We strip some stuff off of them and merge them into one document.

Issue is the stripped stuff is the formatting restrictions and password that is put on buy the user.

Can aspose add the document restrictions which would be-
Formatting Restrictions:
Bullets
Caption Text
Footnote
Hyperlink
In Text Table Text
In Text Table Text Right Align
Mandatory Budget Table

Restrictions"
Block theme or scheme switching
Block Quick Style Set Switching

Then let us put a password back on? Any help would be appreciated.

Thanks,
JT

@jtingres,

Thanks for your inquiry. You can mark whole document as read-only and specify editable regions in Word document. I think, you can meet this requirement after running the following code: (see Protecting Documents)

Document doc = new Document();
doc.RemoveAllChildren();

Section section = new Section(doc);
doc.AppendChild(section);

Body body = new Body(doc);
section.AppendChild(body);

Paragraph para = new Paragraph(doc);
body.AppendChild(para);

Run run = new Run(doc);
run.Text = "Hello ";
run.Font.Color = System.Drawing.Color.Red;
para.AppendChild(run);

Run run2 = new Run(doc);
run2.Text = "World!";
para.AppendChild(run2);

doc.Protect(ProtectionType.ReadOnly);

DocumentBuilder builder = new DocumentBuilder(doc);

EditableRangeStart start = builder.StartEditableRange();
EditableRangeEnd end = builder.EndEditableRange();

run2.ParentNode.InsertBefore(start, run2);
run2.ParentNode.InsertAfter(end, run2);

doc.Save("D:\\temp\\18.7.docx");

thank you for your reply.

I’m trying to restrict them from modifying specific styles.

Also how do you set a password on the code you gave me in case they want to unprotect it?

@jtingres,

To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Your simplified input Word document
  • Your expected document which shows the correct output. Please create this document by using Microsoft Word application.
  • Please also list the complete steps that you performed in MS Word to get the desired output.

As soon as you get these pieces of information ready, we will start further investigation into your scenario and provide you code to achieve the same by using Aspose.Words. Thanks for your cooperation.

You can use the following overload of Protect method:

doc.Protect(ProtectionType.ReadOnly, "password");

Also, in order to save document with encryption please use the following code snippet:

Document doc = new Document("D:\\Temp\\in.docx");

OoxmlSaveOptions saveOptions = new OoxmlSaveOptions();
saveOptions.Password = "myPassword";

doc.Save("D:\\Temp\\out.docx", saveOptions);