【发布时间】:2019-04-04 05:12:47
【问题描述】:
我正在使用docx4j api 创建docx 文件。我已成功将一个docx 内容复制到另一个。
对于复制标题内容,我得到标题文本。
但我的要求也是复制标题图像。我怎样才能做到这一点?
我正在使用下面的代码来复制标题-
WordprocessingMLPackage source = WordprocessingMLPackage.load(new File(
"D://PoC//Agenda Formats//test.docx"));
RelationshipsPart rp = source.getMainDocumentPart()
.getRelationshipsPart();
Relationship rel = rp.getRelationshipByType(Namespaces.HEADER);
HeaderPart headerPart = (HeaderPart)rp.getPart(rel);
HeaderPart newHeaderPart = new HeaderPart();
newHeaderPart.setContents(XmlUtils.deepCopy(headerPart.getContents()));
return wordprocessingMLPackage.getMainDocumentPart().addTargetPart(
newHeaderPart, AddPartBehaviour.RENAME_IF_NAME_EXISTS);
但此代码不复制图像。任何帮助表示赞赏。
【问题讨论】:
-
您需要复制标题的每个 rel。所以遍历它的关系,并复制每个部分。在这种情况下,您可以使用 addTargetPart 以相同的 relId 添加它。这样,您就不必更改标头内容中的 relId。您应该可以使用 Google 找到一些示例代码,但如果您仍然遇到问题,请直说 :-)
-
我从昨天开始一直在苦苦挣扎,但没有收到任何代码。请如果你有相同的代码。