This demo uses the DevExpress Word Processing Document API to password-protect documents from modifications. You can protect the sample file or supply your own document. To do the latter, select Upload a File in the file selection drop-down menu.
Specify protection type and password. You can make a document read-only or select allowed actions (write comments, add revisions, fill in forms). Click Protect and Save as... to update the document and download the result.
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
Stream ProtectDocumentWithPassword(Stream inputStream, DocumentFormat outputFormat, string password, DocumentProtectionType protectionType) {
using var wordProcessor = new RichEditDocumentServer();
wordProcessor.LoadDocument(inputStream);
var document = wordProcessor.Document;
if(document.IsDocumentProtected)
document.Unprotect();
if(protectionType == DocumentProtectionType.FillInForms)
foreach(Section section in document.Sections)
section.ProtectedForForms = true;
document.Protect(password, protectionType);
var outputStream = new MemoryStream();
wordProcessor.SaveDocument(outputStream, outputFormat);
outputStream.Position = 0;
return outputStream;
}