Hi to all,
I want to merge some file docx into a new one. These files may contains images.
This is my code.
File first = new File("C:\\...\\...\\main.docx");
File second = new File("C:\\...\\...\\attach_1.docx");
File third = new File("C:\\...\\...\\attach_2.docx");
File fourth = new File("C:\\...\\...\\attach_3.docx");
try
{
WordprocessingMLPackage cdu = WordprocessingMLPackage.load(new FileInputStream(first));
org.docx4j.wml.P p = new org.docx4j.wml.P();
org.docx4j.wml.R r = new org.docx4j.wml.R();
org.docx4j.wml.Br br = new org.docx4j.wml.Br();
br.setType(STBrType.PAGE);
r.getContent().add(br);
p.getContent().add(r);
cdu.getMainDocumentPart().addObject(p);
List<WordprocessingMLPackage> attachmentList = new ArrayList<WordprocessingMLPackage>();
attachmentList.add(WordprocessingMLPackage.load(new FileInputStream(second)));
attachmentList.add(WordprocessingMLPackage.load(new FileInputStream(second)));
attachmentList.add(WordprocessingMLPackage.load(new FileInputStream(third)));
attachmentList.add(WordprocessingMLPackage.load(new FileInputStream(fourth)));
int numAttach = attachmentList.size();
// cycle on attachment
for (int idx=0; idx<numAttach; idx++)
{
WordprocessingMLPackage attach = attachmentList.get(idx);
List<Object> blips = attach.getMainDocumentPart().getJAXBNodesViaXPath("//a:blip", false);
for(Object el : blips)
{
try
{
CTBlip blip = (CTBlip) el;
RelationshipsPart parts = attach.getMainDocumentPart().getRelationshipsPart();
Relationship rel = parts.getRelationshipByID(blip.getEmbed());
RelationshipsPart cduRels = cdu.getMainDocumentPart().getRelationshipsPart();
rel.setId(null);
cduRels.addRelationship(rel);
blip.setEmbed(rel.getId());
PartName pn = new PartName("/word/"+rel.getTarget());
Part part = attach.getParts().getParts().get(pn);
Relationship newRel = cdu.getMainDocumentPart().addTargetPart(part, AddPartBehaviour.RENAME_IF_NAME_EXISTS);
}
catch (Exception ex) {}
}
List<Object> body = attach.getMainDocumentPart().getJAXBNodesViaXPath("//w:body", false);
for(Object b : body)
{
List filhos = ((org.docx4j.wml.Body)b).getContent();
for(Object k : filhos)
cdu.getMainDocumentPart().addObject(k);
}
}
File saved = new File("C:\\...\\saved.docx");
cdu.save(saved);
}
catch (Docx4JException e) {
e.printStackTrace();
}
catch (JAXBException e1) {
e1.printStackTrace();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
I have an exception when I save merged doc:
org.docx4j.openpackaging.exceptions.Docx4JException: Failed to add parts from relationships of /
at org.docx4j.openpackaging.io3.Save.addPartsFromRelationships(Save.java:392)
at org.docx4j.openpackaging.io3.Save.save(Save.java:192)
...
Caused by: org.docx4j.openpackaging.exceptions.Docx4JException: Failed to add parts from relationships of /word/document.xml
at org.docx4j.openpackaging.io3.Save.addPartsFromRelationships(Save.java:392)
at org.docx4j.openpackaging.io3.Save.savePart(Save.java:444)
...
Caused by: java.io.IOException: part '/word/media/image12.png' not found
at org.docx4j.openpackaging.io3.stores.ZipPartStore.saveBinaryPart(ZipPartStore.java:394)
...
image12.png is generated by cdu.getMainDocumentPart().addTargetPart(part, AddPartBehaviour.RENAME_IF_NAME_EXISTS)
this method return a Relationship (newRel), but I can't find the way to use it.
Anyone has some ideas? I have some difficulty to find documentation about this.
Thanks.
Giuseppe