Check if Word doc is password protected

Is there a way to check if a word docuemnt is password protected, we can then get the user to enter the password so it can be used.

Thanks, Martin


1 Reply 1 reply marked as answer

MR Manikandan Ravichandran Syncfusion Team August 12, 2020 03:44 PM UTC

Hi Martin,

Thank you for contacting Syncfusion support.

From the given details, we have found that your requirement is to find whether the given document is password protected or not. If the password protected document is trying to open using DocIO without password, the exception will throw from our source like below.
 
Document is encrypted, password is needed to open the document 

Based on this exception we can find the given document is password protected or not and we can get the password and again reopen the document with password as like below. 
WordDocument document = null;
try
{
     document =
new WordDocument(@"input.docx", FormatType.Docx);
}
catch (Exception ex)
{
   
if (ex.Message == "Document is encrypted, password is needed to open the document")
    {
       
string password = "password";
        document =
new WordDocument(@"input.docx", FormatType.Docx, password);
        document.Save(
"output.docx");
        document.Close();
    }
}
 

Please let us know if you have any other questions.

Regards,
Manikandan Ravichandran
 


Marked as answer
Loader.
Up arrow icon