简体   繁体   English

使用 Docx4J v. 3.3.3 解决 Word 中不可读的内容消息

[英]Resolve unreadable content message in Word with Docx4J v. 3.3.3

We are processing a Word template that was created with Word 365 ( Version 2202 Build 16.0.14931.20648 ) in Docx4J.我们正在处理使用 Docx4J 中的 Word 365(版本 2202 Build 16.0.14931.20648 )创建的 Word 模板。 Once the file was modified by our Java application, Word will show an error message when we open the document.一旦我们的 Java 应用程序修改了文件,Word 将在我们打开文档时显示一条错误消息。 The message will state that the file contains unreadable content and that Word needs to repair it.该消息将 state该文件包含不可读的内容并且 Word 需要修复它。 This works fine and the document will eventually open, however, the message is annoying.这工作正常,文档最终会打开,但是,消息很烦人。

I assume that the error message is related to a namespace issue ( see this question ).我假设错误消息与名称空间问题有关请参阅此问题)。 That issue was resolved with docx4j v. 8.2.9 , which defines the missing namespaces properly.该问题已通过docx4j v. 8.2.9解决,它正确定义了缺失的命名空间。

However, I'm stuck with docx4j 3.3.3 and cannot update.但是,我坚持使用 docx4j 3.3.3,无法更新。 The fix seems rather simple in the GitHub commit , so I wonder, if there is any way to resolve the issue myself.修复在GitHub commit中看起来相当简单,所以我想知道是否有任何方法可以自己解决这个问题。 All I have in my code, is a WordprocessingMLPackage object. Can I add or append namespace definitions to that object or it's sub-properties somehow?我的代码中只有一个WordprocessingMLPackage object。我可以向该 object 或其子属性添加或 append 命名空间定义吗?

You can't add the namespace definitions to the WordprocessingMLPackage object.您不能将命名空间定义添加到 WordprocessingMLPackage object。

You'll need to get the source code for 3.3.3 from https://github.com/plutext/docx4j/tree/docx4j-3.3.3 then copy the new NamespacePrefixMappings content into it, then build it.您需要从https://github.com/plutext/docx4j/tree/docx4j-3.3.3获取 3.3.3 的源代码,然后将新的 NamespacePrefixMappings 内容复制到其中,然后构建它。 You can then deploy this new jar file.然后您可以部署这个新的 jar 文件。

If you wanted to try to avoid compiling the source code, you'd have 2 alternatives to try (since docx4j doesn't implement the stategy pattern there).如果您想尝试避免编译源代码,您可以尝试两种选择(因为 docx4j 没有在那里实现策略模式)。 I don't expect these to work!我不希望这些工作!

One is to replace the relevant classes at runtime.一种是在运行时替换相关的类。 For this, see How to replace classes in a running application in java?为此,请参阅java 中如何替换正在运行的应用程序中的类?

The other is to replace the relevant classes in your jar file, which is just a zip file.另一种是将你的jar文件中的相关类替换掉,也就是一个zip文件。

You'd need to get the new class from docx4j 8.2.9.您需要从 docx4j 8.2.9 获取新的 class。

Please note that sometimes, there are also changes to ContentTypeManager and ContentTypes;请注意,有时 ContentTypeManager 和 ContentTypes 也会发生变化; see for example https://github.com/plutext/docx4j/commit/d4d02d3fa6e7bf98f35d1f0520e62eb8aef06cba参见例如https://github.com/plutext/docx4j/commit/d4d02d3fa6e7bf98f35d1f0520e62eb8aef06cba

That commit introduces new parts, and you'll run into problems if you update ContentTypeManager without those.该提交引入了新部分,如果您在没有这些的情况下更新 ContentTypeManager,您将遇到问题。

So you might be tempted to try to replace NamespacePrefixMappings in your existing jar.因此,您可能想尝试替换现有 jar 中的 NamespacePrefixMappings。

But the interfaces it implements changed at https://github.com/plutext/docx4j/commit/65fb843a26b5893200a1824c04c826db2db7940c#diff-70242e2f5ec56be77fe15322526f4530b02e8eafdcb9ae16b60b2220f62e0632但它实现的接口在https://github.com/plutext/docx4j/commit/65fb843a26b5893200a1824c04c826db2db7940c#diff-70242e2f5ec56be77fe15322526f4530b02e8eafdcb02ae1260b02e8eafdcb6bae12603

See further https://github.com/plutext/docx4j/commits/VERSION_8_3_8/docx4j-core/src/main/java/org/docx4j/jaxb/NamespacePrefixMappings.java so that is going to cause you problems.进一步查看https://github.com/plutext/docx4j/commits/VERSION_8_3_8/docx4j-core/src/main/java/org/docx4j/jaxb/NamespacePrefixMappings.java这样会给您带来麻烦。

The upshot is you'll need to get the source code for 3.3.3 from https://github.com/plutext/docx4j/tree/docx4j-3.3.3 then copy the new NamespacePrefixMappings content into it (ie except the interface changes), then build it.结果是您需要从https://github.com/plutext/docx4j/tree/docx4j-3.3.3获取 3.3.3 的源代码,然后将新的 NamespacePrefixMappings 内容复制到其中(即接口更改除外) ),然后构建它。 If you do this, you may as well just deploy your new jar file.如果这样做,您也可以只部署新的 jar 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM