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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("Open document as read-only");
// Enter a password that's up to 15 characters long.
doc.WriteProtection.SetPassword("MyPassword");
// Make the document as read-only.
doc.WriteProtection.ReadOnlyRecommended = true;
// Apply write protection as read-only.
doc.Protect(ProtectionType.ReadOnly);
doc.Save(ArtifactsDir + "ReadOnly protection - Aspose.Words.docx");
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
using WordprocessingDocument doc = WordprocessingDocument.Create(ArtifactsDir + "ReadOnly protection - OpenXml.docx", WordprocessingDocumentType.Document);
// Add a main document part.
MainDocumentPart mainPart = doc.AddMainDocumentPart();
mainPart.Document = new Document();
Body body = new Body();
mainPart.Document.Append(body);
// Add a paragraph with some text.
Paragraph paragraph = new Paragraph();
Run run = new Run();
Text text = new Text("Open document as read-only");
run.Append(text);
paragraph.Append(run);
body.Append(paragraph);
// Add write protection settings.
DocumentProtection documentProtection = new DocumentProtection
{
Edit = DocumentProtectionValues.ReadOnly,
Enforcement = OnOffValue.FromBoolean(true),
CryptographicProviderType = CryptProviderValues.RsaFull,
CryptographicAlgorithmClass = CryptAlgorithmClassValues.Hash,
CryptographicAlgorithmType = CryptAlgorithmValues.TypeAny,
CryptographicAlgorithmSid = 4, // SHA-1
Hash = "MyPassword", // Password hash (in real scenarios, you should hash the password).
SpinCount = 100000, // Number of iterations for hashing.
Salt = Convert.ToBase64String(Guid.NewGuid().ToByteArray()) // Random salt.
};
// Add the document protection settings to the document settings part.
DocumentSettingsPart settingsPart = mainPart.AddNewPart<DocumentSettingsPart>();
settingsPart.Settings = new Settings();
settingsPart.Settings.Append(documentProtection);
mainPart.Document.Save();