Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法打开生成的docx4j文档,MS Word应用程序除外_Java_Docx4j - Fatal编程技术网

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

Java 无法打开生成的docx4j文档,MS Word应用程序除外,java,docx4j,Java,Docx4j,下面是使用docx4j生成文档的代码。我只能在MS Word应用程序中打开生成的文档 当我邮寄生成的文档时,收件人无法查看该文档 private static void documentGenerator(String html, File file) throws Docx4JException, JAXBException { //Word Processing Package WordprocessingMLPackage wordMLPackage = getWordMLPackage

下面是使用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);
}

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

下面是代码片段

WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
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);

到底发生了什么,使用哪个应用程序在接收方打开文件?接收方使用的是什么应用程序?听起来它无法处理AlternativeFormatInputPart(AltChunkType.Xhtml)。您可以使用XhtmlImporter进行更多的控制;如果您使用它,它就不会发生。@JasonPlutext我也用XhtmlImporter试过。同一问题Exist@Marged接收者试图在谷歌文档中打开文档。当下载文档时,它显示一个空白文档。接收者也可以在MS Word中看到文档