Page 1 of 1

docx with images merge

PostPosted: Wed Jun 21, 2017 1:44 am
by giuseppe
Hi to all,
I have many docx file that I have merge into an unique docx (I use doc4j open).
The problem is with embed images in the docs.
These are all called image1.png (in /word/media/) and when I merge docx, at the end I have only the image of the first document that replace all other images.
There is a way to rename images (I aspect that at the end, in the word/media/ dir, I have image1.png, image2.png and so on).
Could someone help me?
Thanks in advance.

Giuseppe

Re: docx with images merge

PostPosted: Thu Jun 22, 2017 8:59 pm
by jason
You can use addTargetPart with AddPartBehaviour.RENAME_IF_NAME_EXISTS; see https://github.com/plutext/docx4j/blob/ ... s.java#L87

You'll need to update the document with the new relId

Please be aware of the other structures you are likely to run into when merging documents: https://www.docx4java.org/blog/2010/11/ ... documents/

Re: docx with images merge

PostPosted: Fri Jun 23, 2017 12:29 am
by giuseppe
Thanks for the reply Jason,

I have added the flag AddPartBehaviour.RENAME_IF_NAME_EXISTS in the addTargetPart method, but now I have an exception.
What is image12.png?
This is my code and the exception received.
Thanks.

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

cdu.getMainDocumentPart().addTargetPart(part, AddPartBehaviour.RENAME_IF_NAME_EXISTS);
}
catch (Exception ex){}
}


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)
at org.docx4j.openpackaging.packages.OpcPackage.save(OpcPackage.java:693)
at org.docx4j.openpackaging.packages.OpcPackage.save(OpcPackage.java:582)
at org.docx4j.openpackaging.packages.OpcPackage.save(OpcPackage.java:565)
at org.docx4j.openpackaging.packages.OpcPackage.save(OpcPackage.java:554)
at provaDocx4j.merge.main(merge.java:172)
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)
at org.docx4j.openpackaging.io3.Save.addPartsFromRelationships(Save.java:387)
... 6 more
Caused by: org.docx4j.openpackaging.exceptions.Docx4JException: Failed to put binary part
at org.docx4j.openpackaging.io3.stores.ZipPartStore.saveBinaryPart(ZipPartStore.java:431)
at org.docx4j.openpackaging.io3.Save.savePart(Save.java:420)
at org.docx4j.openpackaging.io3.Save.addPartsFromRelationships(Save.java:387)
... 8 more
Caused by: java.io.IOException: part '/word/media/image12.png' not found
at org.docx4j.openpackaging.io3.stores.ZipPartStore.saveBinaryPart(ZipPartStore.java:394)
... 10 more
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)
at org.docx4j.openpackaging.packages.OpcPackage.save(OpcPackage.java:693)
at org.docx4j.openpackaging.packages.OpcPackage.save(OpcPackage.java:582)
at org.docx4j.openpackaging.packages.OpcPackage.save(OpcPackage.java:565)
at org.docx4j.openpackaging.packages.OpcPackage.save(OpcPackage.java:554)
at provaDocx4j.merge.main(merge.java:172)
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)
at org.docx4j.openpackaging.io3.Save.addPartsFromRelationships(Save.java:387)
... 6 more
Caused by: org.docx4j.openpackaging.exceptions.Docx4JException: Failed to put binary part
at org.docx4j.openpackaging.io3.stores.ZipPartStore.saveBinaryPart(ZipPartStore.java:431)
at org.docx4j.openpackaging.io3.Save.savePart(Save.java:420)
at org.docx4j.openpackaging.io3.Save.addPartsFromRelationships(Save.java:387)
... 8 more
Caused by: java.io.IOException: part '/word/media/image12.png' not found
at org.docx4j.openpackaging.io3.stores.ZipPartStore.saveBinaryPart(ZipPartStore.java:394)
... 10 more

Re: docx with images merge

PostPosted: Sat Jun 24, 2017 2:06 am
by giuseppe
Hi to all,

un update:

I see that addTargetPart method return the RelationShip object and that this has, after rename, a new Id.
I suppose that I have to use this Id on cdu object (in my case) but I don't know how ...
Is that right?
Thanks.

Giuseppe