Aspose.Words has the public class WriteProtection that specifies write protection settings for a document. Make a document read-only to restrict editing by using the ReadOnlyRecommended property and the SetPassword method.
The following code example shows how to make your document read-only:
You can also do the same using the Open XML SDK. At the same time, note that it looks somewhat more complicated and more cumbersome.
The code sample below shows how you can add some text and attempt to save the changes to show that access is read-only. Once you have access to the body of the main document part, you add text by adding instances of the Paragraph, Run, and Text classes. This generates the required WordprocessingML markup.
The following code example shows how to make your document read-only:
public void OpenReadOnlyAccessFeature()
{
// Open a WordprocessingDocument based on a filepath.
using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(MyDir + "Open readonly access.docx", false))
{
// Assign a reference to the existing document body.
Body body = wordDocument.MainDocumentPart.Document.Body;
// Attempt to add some text.
Paragraph para = body.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(new Text("Append text in body, but text is not saved - Open wordprocessing document readonly"));
// Call the "Save" method to generate an exception and show that access is read-only.
using (Stream stream = File.Create(ArtifactsDir + "Open readonly access - OpenXML.docx"))
{
wordDocument.MainDocumentPart.Document.Save(stream);
}
}
}