Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a Password-Protected Word File in Java?

How can I open a password-protected Microsoft word(.doc, .docx) file in Java, assuming that the password is known?

like image 798
brainless Avatar asked Sep 09 '10 10:09

brainless


1 Answers

You can try it with com4j.

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.documents.open2000.aspx

Since there is a parameter called "PasswordDocument" in the "open"-method, I think it is possible to open a password protected file.

Hope this is what you were searching for ;)

Edit: I recorded this Macro in Word.

Documents.Open FileName:="test.doc", ConfirmConversions:= _
    False, ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:= _
    "hallo", PasswordTemplate:="", Revert:=False, WritePasswordDocument:= _
    "hallo", WritePasswordTemplate:="", Format:=wdOpenFormatAuto

So the open method in com4j should look somethin like this (password is "Hallo"):

     _Document document = app.documents().open2000(doc, false, false, false, "hallo", "", false, "hallo", "", WdOpenFormat.wdOpenFormatAuto, false, true);
like image 186
Tronje182 Avatar answered Sep 23 '22 22:09

Tronje182