当我在使用 POI 使用 XPath 处理一些文本后保存 docx 文件时,然后我将 ByteArrayOutputStream 传递给新的 ByteArrayInputStream 并将其提供给 dox4j
wordMLPackage = WordprocessingMLPackage.load(
bis
);
对于我的 4 个模板中的 3 个,这会引发异常:
org.docx4j.openpackaging.exceptions.InvalidFormatException: Unexpected package (docx4j supports docx/docxm and pptx only
at org.docx4j.openpackaging.contenttype.ContentTypeManager.createPackage(ContentTypeManager.java:834)
代码看起来像这样:
/* Return a package of the appropriate type. Used when loading an existing
* Package, with an already populated [Content_Types].xml. When
* creating a new Package, start with the new WordprocessingMLPackage constructor. */
public OpcPackage createPackage() throws InvalidFormatException {
/*
* How do we know what type of Package this is?
*
* In principle, either:
*
* 1. We were told its file extension or mime type in the
* constructor/method parameters, or
*
* 2. Because [Content_Types].xml contains an override for PartName
* /document.xml of content type
* application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml
*
* The latter approach is more reliable, so ..
*
*/
OpcPackage p;
if (getPartNameOverridenByContentType(ContentTypes.WORDPROCESSINGML_DOCUMENT) != null
|| getPartNameOverridenByContentType(ContentTypes.WORDPROCESSINGML_DOCUMENT_MACROENABLED) != null
|| getPartNameOverridenByContentType(ContentTypes.WORDPROCESSINGML_TEMPLATE ) != null
|| getPartNameOverridenByContentType(ContentTypes.WORDPROCESSINGML_TEMPLATE_MACROENABLED) != null ) {
log.info("Detected WordProcessingML package ");
p = new WordprocessingMLPackage(this);
return p;
} else if (getPartNameOverridenByContentType(ContentTypes.PRESENTATIONML_MAIN) != null
|| getPartNameOverridenByContentType(ContentTypes.PRESENTATIONML_TEMPLATE) != null
|| getPartNameOverridenByContentType(ContentTypes.PRESENTATIONML_SLIDESHOW) != null) {
log.info("Detected PresentationMLPackage package ");
p = new PresentationMLPackage(this);
return p;
} else if (getPartNameOverridenByContentType(ContentTypes.SPREADSHEETML_WORKBOOK) != null
|| getPartNameOverridenByContentType(ContentTypes.SPREADSHEETML_WORKBOOK_MACROENABLED) != null
|| getPartNameOverridenByContentType(ContentTypes.SPREADSHEETML_TEMPLATE) != null
|| getPartNameOverridenByContentType(ContentTypes.SPREADSHEETML_TEMPLATE_MACROENABLED) != null) {
// "xlam", "xlsb" ?
log.info("Detected SpreadhseetMLPackage package ");
p = new SpreadsheetMLPackage(this);
return p;
} else if (getPartNameOverridenByContentType(ContentTypes.DRAWINGML_DIAGRAM_LAYOUT) != null) {
log.info("Detected Glox file ");
p = new GloxPackage(this);
return p;
} else {
throw new InvalidFormatException("Unexpected package (docx4j supports docx/docxm and pptx only");
//return new Package(this);
}
}
它似乎无法匹配某些特定的内容类型覆盖。在我的起始 docx 模板中有一个 [Content_Types].xml 文件,其中包含:
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
<Override PartName="/_rels/.rels" ContentType="application/vnd.openxmlformats-package.relationships+xml" />
<Override PartName="/word/fontTable.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml" />
<Override PartName="/word/_rels/document.xml.rels" ContentType="application/vnd.openxmlformats-package.relationships+xml" />
<Override PartName="/word/media/image1.wmf" ContentType="image/x-wmf" />
<Override PartName="/word/comments.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.comments+xml" />
<Override PartName="/word/numbering.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml" />
<Override PartName="/word/footer1.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml" />
<Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml" />
<Override PartName="/word/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml" />
<Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml" />
<Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml" />
</Types>
使用 POI 处理后,[Content_Types].xml 如下所示:
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
<Default Extension="xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/>
<Override PartName="/_rels/.rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
<Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/>
<Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>
<Override PartName="/word/_rels/document.xml.rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
<Override PartName="/word/comments.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.comments+xml"/>
<Override PartName="/word/fontTable.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml"/>
<Override PartName="/word/footer1.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml"/>
<Override PartName="/word/media/image1.wmf" ContentType="image/x-wmf"/>
<Override PartName="/word/numbering.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml"/>
<Override PartName="/word/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml"/>
</Types>
请注意,缺少 PartName="/word/document.xml" 的覆盖!
这是没有 word/document.xml 覆盖的可接受文件内容类型文件吗?它在 LibreOffice 中打开,没有任何投诉。docx4j 是否依赖于内容类型中可能不存在的覆盖标签,或者 POI 是否没有为我的某些文件正确编写内容类型(4 个中的 3 个)。