Page 1 of 1

merge document error

PostPosted: Wed Jul 05, 2017 6:31 pm
by giuseppe
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

Re: merge document error

PostPosted: Wed Jul 05, 2017 8:12 pm
by jason
You need to move or copy the relevant part to the target package, so it can be found by saveBinaryPart

Re: merge document error

PostPosted: Thu Jul 06, 2017 8:59 pm
by giuseppe
Hi Jason,

thanks for the response, but I'm afraid I don't understand your suggestion.
Could you indicate me some examples or some documentation/guide about this?
Thanks a lot.
Giuseppe

Re: merge document error

PostPosted: Thu Jul 06, 2017 10:16 pm
by jason
You can examine the saving code to see what it is looking for, but basically:

- a package knows which parts it contains https://github.com/plutext/docx4j/blob/ ... .java#L114 so you need to add the part here

- a part can also know which package it is in https://github.com/plutext/docx4j/blob/ ... .java#L217