title: Merge word(docx) documents with DOCX4J: how to copy images?
body: <p>I need to merge two (or more, but let's stick to two) word documents (docx) with docx4j. My approach to merge is copy all body children from one document and append to another. Then, I just rearrange some stuff. I have been using it for two years, and it is fine for my purpose.</p>
  
  <p>Here is a simple example:</p>
  
  <p>first.docx = simple text<br />
  second.docx = simple text + image</p>
  
  <pre><code>    File first = new File("first.docx");
      File second = new File("second.docx");
  
      WordprocessingMLPackage f = WordprocessingMLPackage.load(first);
      WordprocessingMLPackage s = WordprocessingMLPackage.load(second);
  
      List body = s.getMainDocumentPart().getJAXBNodesViaXPath("//w:body", false);
      for(Object b : body){
          List filhos = ((org.docx4j.wml.Body)b).getContent();
          for(Object k : filhos)
              f.getMainDocumentPart().addObject(k);
      }
  
      List blips = s.getMainDocumentPart().getJAXBNodesViaXPath("//a:blip", false);
      for(Object el : blips){
          try {
              CTBlip blip = (CTBlip) el;
  
              RelationshipsPart parts = s.getMainDocumentPart().getRelationshipsPart();
              Relationship rel = parts.getRelationshipByID(blip.getEmbed());
  
              RelationshipsPart docRels = f.getMainDocumentPart().getRelationshipsPart();
  
              rel.setId(null);
              docRels.addRelationship(rel);
              blip.setEmbed(rel.getId());
  
              f.getMainDocumentPart().addTargetPart(s.getParts().getParts().get(new PartName("/word/"+rel.getTarget())));
  
          } catch (Exception ex){}
      }
  
      File saved = new File("saved.docx");
      f.save(saved);
  
      Desktop.getDesktop().open(saved);
  </code></pre>
  
  <p>The problem is when I save. This errors come out:</p>
  
  <pre><code>    org.docx4j.openpackaging.exceptions.Docx4JException: Failed to add parts from relationships of /
  at org.docx4j.openpackaging.io3.Save.addPartsFromRelationships(Save.java:390)
  at org.docx4j.openpackaging.io3.Save.save(Save.java:192)
  at org.docx4j.openpackaging.packages.OpcPackage.save(OpcPackage.java:441)
  at org.docx4j.openpackaging.packages.OpcPackage.save(OpcPackage.java:406)
  
  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:390)
  at org.docx4j.openpackaging.io3.Save.savePart(Save.java:442)
  at org.docx4j.openpackaging.io3.Save.addPartsFromRelationships(Save.java:385)
  ... 4 more
  
  Caused by: org.docx4j.openpackaging.exceptions.Docx4JException: Failed to put binary part
  at
  org.docx4j.openpackaging.io3.stores.ZipPartStore.saveBinaryPart(ZipPartStore.java:398)
  at org.docx4j.openpackaging.io3.Save.savePart(Save.java:418)
  at org.docx4j.openpackaging.io3.Save.addPartsFromRelationships(Save.java:385)
  ... 6 more
  
  Caused by: java.io.IOException: part '/word/media/image1.jpg' not found
  at
  org.docx4j.openpackaging.io3.stores.ZipPartStore.saveBinaryPart(ZipPartStore.java:361)
  ... 8 more
  
  Exception in thread "main" org.docx4j.openpackaging.exceptions.Docx4JException: Failed to add parts from relationships of /
  at org.docx4j.openpackaging.io3.Save.addPartsFromRelationships(Save.java:390)
  at org.docx4j.openpackaging.io3.Save.save(Save.java:192)
  at org.docx4j.openpackaging.packages.OpcPackage.save(OpcPackage.java:441)
  at org.docx4j.openpackaging.packages.OpcPackage.save(OpcPackage.java:406)
  
  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:390)
  at org.docx4j.openpackaging.io3.Save.savePart(Save.java:442)
  at org.docx4j.openpackaging.io3.Save.addPartsFromRelationships(Save.java:385)
  ... 4 more
  
  Caused by: org.docx4j.openpackaging.exceptions.Docx4JException: Failed to put binary part
  at
  org.docx4j.openpackaging.io3.stores.ZipPartStore.saveBinaryPart(ZipPartStore.java:398)
  at org.docx4j.openpackaging.io3.Save.savePart(Save.java:418)
  at org.docx4j.openpackaging.io3.Save.addPartsFromRelationships(Save.java:385)
  ... 6 more
  
  Caused by: java.io.IOException: part '/word/media/image1.jpg' not found
  at
  org.docx4j.openpackaging.io3.stores.ZipPartStore.saveBinaryPart(ZipPartStore.java:361)
  ... 8 more
  </code></pre>
  
  <p>Any light here to solve it?</p>
  
  <p>1) I dont want altchunk, it sucks.
  2) The commercial (Enterprise) version of docx4j can do it, but I'm looking for FOSS.</p>
  
  <p>Thanks</p>