We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How set password for Word document using .NET

how to add password in doc while creating it using aspose. Such that if user opens the documents, document should ask for the password to open it.

@sunnydeol1712

Following code example shows how to create a password protected Office Open XML document.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("Hello world!");

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

doc.Save(ArtifactsDir + "OoxmlSaveOptions.Password.docx", saveOptions);

If you want to save document to DOC file format, please use the following code example.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("Hello world!");

// DocSaveOptions only applies to Doc and Dot save formats
DocSaveOptions options = new DocSaveOptions(SaveFormat.Doc);

// Set a password with which the document will be encrypted, and which will be required to open it
options.Password = "MyPassword";

// If the document contains a routing slip, we can preserve it while saving by setting this flag to true
options.SaveRoutingSlip = true;

doc.Save(ArtifactsDir + "DocSaveOptions.SaveAsDoc.doc", options);