无法打开除MS-Word应用程序之外的生成的docx4j文档

时间:2017-01-11 19:04:29

标签: java docx4j

以下是使用docx4j生成文档的代码。我只能在MS-Word应用程序中打开生成的文档。

当我邮寄生成的文档时,接收者无法查看文档。

 private static void documentGenerator(String html, File file) throws Docx4JException, JAXBException {
//Word Processing Package
WordprocessingMLPackage wordMLPackage = getWordMLPackage();
NumberingDefinitionsPart ndp = new NumberingDefinitionsPart();
wordMLPackage.getMainDocumentPart().addTargetPart(ndp);
ndp.unmarshalDefaultNumbering();
AlternativeFormatInputPart inputPart = new AlternativeFormatInputPart(AltChunkType.Xhtml);
inputPart.setContentType(new ContentType("text/html"));
inputPart.setBinaryData(html.getBytes());
Relationship altChunkRel = wordMLPackage.getMainDocumentPart().addTargetPart(inputPart);
// .. the bit in document body
CTAltChunk ac = Context.getWmlObjectFactory().createCTAltChunk();
ac.setId(altChunkRel.getId());
wordMLPackage.getMainDocumentPart().addObject(ac);
// .. content type
wordMLPackage.getContentTypeManager().addDefaultContentType("html", "text/html");
//Saving the Document
wordMLPackage.save(file);
}

1 个答案:

答案 0 :(得分:0)

我使用XhtmlImporter生成文档,然后解决了兼容性问题。

以下是Code Snippet

{{1}}