docx4j无法读取POI保存的文件,谁有错?

时间:2013-02-21 16:37:32

标签: ms-office apache-poi docx4j

当我使用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的覆盖=&#34; /word/document.xml"不见了!

这是否是一个可接受的文件内容类型文件,没有/ document.xml覆盖?它在LibreOffice中打开,没有任何投诉。 docx4j是否依赖于内容类型中可能不存在的覆盖标记,或者POI没有正确地为我的某些文件编写内容类型(4个中的3个)。

1 个答案:

答案 0 :(得分:2)

披露:我是docx4j项目负责人

根据规范,POI正在做什么似乎是合法的,但并不理想。

根据ECMA-376第2部分“获取部件的内容类型”,docx4j应在POI指定时找到docx的内容类型。

第1部分中的WordprocessingML章节在“包结构”部分中说:

  

首先,关系部分的内容类型和主文档   必须定义部分(唯一必需的部分)(物理上位于   包中的/[Content_Types].xml):

<Types
 xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
 &lt;Default Extension="rels"
 ContentType="application/vnd.openxmlformatspackage.
 relationships+xml"/> 
<Override PartName="/document.xml"
 ContentType="application/vnd.openxmlformatsofficedocument.
 wordprocessingml.document.main+xml"/> </Types>

我的阅读是你必须定义主文档部分的内容类型(POI所做的),并且提示只是使用覆盖来执行此操作。

当大多数部分都是.xml并且需要覆盖来指定不同的东西时,将你的.xml默认值用于匹配一个(或者可能是2或3个部分)的东西并没有多大意义。 。我想知道为什么POI这样做了 - 与规范中的建议不同,与Word发出的不同。

那就是说,https://github.com/plutext/docx4j/commit/1c1190fc3a2fc6e191c825a0e30fde2654cc997c应该解决这个问题。