【问题标题】:How to copy docx file header image into anoter docx file in java如何将docx文件头图像复制到java中的另一个docx文件中
【发布时间】: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 找到一些示例代码,但如果您仍然遇到问题,请直说 :-)
  • 我从昨天开始一直在苦苦挣扎,但没有收到任何代码。请如果你有相同的代码。

标签: java docx4j


【解决方案1】:

尝试类似(未经测试):

void attachHeader(HeaderPart sourcePart, WordprocessingMLPackage targetPkg) throws Docx4JException {

    HeaderPart newHeaderPart = new HeaderPart();
    newHeaderPart.setContents(XmlUtils.deepCopy(sourcePart.getContents()));

    if (sourcePart.getRelationshipsPart()!=null) {

        // clone the rels part
        RelationshipsPart rp = sourcePart.getRelationshipsPart();
        newHeaderPart.getRelationshipsPart(true).setContents(XmlUtils.deepCopy(rp.getContents()));

        // copy/add each part
        for (Relationship r : newHeaderPart.getRelationshipsPart().getContents().getRelationship()) {

            // get the source part
            Part part = sourcePart.getRelationshipsPart().getPart(r.getId());

            // ensure it is loaded
            if (part instanceof BinaryPart) {
                ((BinaryPart)part).getBuffer();
            }

            // You might need to clone this part depending on your use case, but here I'll just attach it to targetPkg              
            targetPkg.getParts().getParts().put(part.getPartName(), part);
                // This simple approach won't work if the target package already contains a part with the same name 
                // To fix that, you'd need to rename the part (also in the rel)

            part.setPackage(targetPkg);
            part.setOwningRelationshipPart(newHeaderPart.getRelationshipsPart());

        }

    }
    targetPkg.getMainDocumentPart().addTargetPart(newHeaderPart,
            AddPartBehaviour.RENAME_IF_NAME_EXISTS);        
}

【讨论】:

  • 我使用的是企业版,但我可以使用上述代码读取图像。请帮忙。
  • 对于企业版,只需添加一个包含 sectPr 的 BlockRange 和感兴趣的标头。它会完成剩下的工作。
  • 请提供一些例子
  • 请提供非常紧急的例子。
  • 您可以发送电子邮件至 support@plutext.com,但请说明您的总体目标并附上相关的 docx 文件
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-24
  • 1970-01-01
  • 2014-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-13
相关资源
最近更新 更多