Protect word document with password to open

There is a DocSavingOptions class in Aspose.Words.Saving namespace, but I couldn’t find any info on how to use it.


I’m trying to write a code to open a document with the password to open, process the document, and save it with the same password.


Thanks in advance

Hi Carla,

Thanks
for your inquiry. Encrypted documents can be loaded into Aspose.Words
as long as the password supplied on load for the document is correct. Please check following code snippet.

Document doc = new Document(“Document.LoadEncrypted.doc”, new LoadOptions(“password”));

DocSaveOptions Constructor initializes a new instance of this class that can be used to save a document in the Doc or Dot format.

OoxmlSaveOptions Constructor initializes a new instance of this class that can be used to save a document in the Docx, Docm, Dotx, Dotm or FlatOpc format.

Please check following code examples for your kind reference and let us know if you have any more queries.

LoadOptions options = new LoadOptions();

options.Password = “test”;

Document doc = new Document(MyDir + “in.doc”, options);

DocSaveOptions saveoptions = new DocSaveOptions();

saveoptions.Password = “new password”;

doc.Save(MyDir + “Out.doc”, saveoptions);

Document doc = new Document(MyDir + “in.docx”);

OoxmlSaveOptions options = new OoxmlSaveOptions(SaveFormat.Docx);

options.Password = “test”;

doc.Save(MyDir + “Out.docx”, options);

got that

Thank you Tahir.

Tahir, I got it to work for MS Word


But I couldn’t find anything similar for Excel (we also have Aspose.Cells license).

The same procedure, I have to open a workbook with a password, manipulate the file, and the save the file with the same password.

Thanks in advance

Hi Carla,


Thanks
for your inquiry. I am moving this forum thread to Aspose.Total forum. My colleagues from Aspose.Cells team will reply you shortly about your query.

Hi,


Similarly for Excel spreadsheets, you may use the following sample code to open an encrypted file and re-save it via Aspose.Cells APIs:
e.g
Sample code:

//Instantiate LoadOptions
LoadOptions loadOptions = new LoadOptions();

//Specify the password
loadOptions.Password = “007”;

//Create a Workbook object and opening the file from its path
Workbook wb = new Workbook(“e:\test\EncryptedBook.xls”, loadOptions);

//Set the new password if you want.
wb.Settings.Password = “NewPassword1”;

//Save the Excel file
workbook.Save(“e:\test2\outExcel_OpenPassword1.xls”);



Let us know if you still have any issue.

Thank you.

Thanks Amjad


I plugged the code, everything works as you described