用 Java 打开受密码保护的 Word 文件?

发布于 2024-09-18 00:12:46 字数 62 浏览 5 评论 0原文

假设密码已知,如何在 Java 中打开受密码保护的 Microsoft Word(.doc、.docx)文件?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心作怪 2024-09-25 00:12:46

你可以用com4j试试。

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

由于“open”方法中有一个名为“PasswordDocument”的参数,我认为可以打开一个受密码保护的文件。

希望这就是您正在寻找的内容;)

编辑:我在 Word 中录制了这个宏。

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

所以 com4j 中的 open 方法应该看起来像这样(密码是“Hallo”):

     _Document document = app.documents().open2000(doc, false, false, false, "hallo", "", false, "hallo", "", WdOpenFormat.wdOpenFormatAuto, false, true);

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);
南巷近海 2024-09-25 00:12:46

Apache POI 项目是一个很好的起点,它支持 Office 97-2003 和 OOXML (2007-2010) 格式。如果您主要对从这些文件中提取文本感兴趣,您还应该查看 Tika 项目,它有一些很好的代码,例如 OfficeParser.java

您需要在第 220 行左右替换您已知的密码在 parse() 方法中:

if (!d.verifyPassword(Decryptor.DEFAULT_PASSWORD)) {
throw new TikaException("Unable to process: document is encrypted");
}

-- 默认密码设置为最无用的密码“VelvetSweatshop”(!)

A good starting point would be the Apache POI project which supports Office 97-2003 and OOXML (2007-2010) formats. If you are mainly interested in extracting text from those files, you should also look at the Tika project that has some good code, such as OfficeParser.java

You will want to substitute in your known password(s) around line 220 in the parse() method:

if (!d.verifyPassword(Decryptor.DEFAULT_PASSWORD)) {
throw new TikaException("Unable to process: document is encrypted");
}

-- the default password is set to the mostly useless password "VelvetSweatshop" (!)

无边思念无边月 2024-09-25 00:12:46

在我们的项目中,我们使用 Aspose 来管理 Office 文档,但我们不处理受密码保护的文档,但我想这个库可以处理这种情况......

In our projects, we use Aspose to manage Office documents, but we do not deal with password-protected documents, but I imagine that this library handles such cases...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文