300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > docx 图片预处理 Java_如何在java中将docx文件头图像复制到另一个docx文件中

docx 图片预处理 Java_如何在java中将docx文件头图像复制到另一个docx文件中

时间:2020-11-05 16:02:06

相关推荐

docx 图片预处理 Java_如何在java中将docx文件头图像复制到另一个docx文件中

尝试(未经测试的):

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);

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。