How to copy an image from one docx to another with docx4j

Viewed 17

I am trying to copy an image from one docx document to another docx document. I copy the content from the MainDocumentPart and in both documents it looks the same.

      <w:p w:rsidR="00377018" w:rsidRDefault="00377018" w14:paraId="7E1835D5" w14:textId="438C3FF9">
         <w:r>
            <w:rPr>
               <w:noProof/>
            </w:rPr>
            <w:drawing>
               <wp:inline distT="0" distB="0" distL="0" distR="0">
                  <wp:extent cx="5760720" cy="4372610"/>
                  <wp:effectExtent l="0" t="0" r="0" b="8890"/>
                  <wp:docPr id="1" name="Grafik 1" descr="Ein Bild, das Text, Person, Gruppe, Personen enthält.&#xa;&#xa;Automatisch generierte Beschreibung" xmlns:dgm1612="http://schemas.microsoft.com/office/drawing/2016/12/diagram" xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" xmlns:lc="http://schemas.openxmlformats.org/drawingml/2006/lockedCanvas" xmlns:pic14="http://schemas.microsoft.com/office/drawing/2010/picture" xmlns:dgm14="http://schemas.microsoft.com/office/drawing/2010/diagram" xmlns:c16="http://schemas.microsoft.com/office/drawing/2014/chart" xmlns:dgm="http://schemas.openxmlformats.org/drawingml/2006/diagram" xmlns:c15="http://schemas.microsoft.com/office/drawing/2012/chart"/>
                  <wp:cNvGraphicFramePr xmlns:dgm1612="http://schemas.microsoft.com/office/drawing/2016/12/diagram" xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" xmlns:lc="http://schemas.openxmlformats.org/drawingml/2006/lockedCanvas" xmlns:pic14="http://schemas.microsoft.com/office/drawing/2010/picture" xmlns:dgm14="http://schemas.microsoft.com/office/drawing/2010/diagram" xmlns:c16="http://schemas.microsoft.com/office/drawing/2014/chart" xmlns:dgm="http://schemas.openxmlformats.org/drawingml/2006/diagram" xmlns:c15="http://schemas.microsoft.com/office/drawing/2012/chart">
                     <a:graphicFrameLocks noChangeAspect="true"/>
                  </wp:cNvGraphicFramePr>
                  <a:graphic xmlns:dgm1612="http://schemas.microsoft.com/office/drawing/2016/12/diagram" xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" xmlns:lc="http://schemas.openxmlformats.org/drawingml/2006/lockedCanvas" xmlns:pic14="http://schemas.microsoft.com/office/drawing/2010/picture" xmlns:dgm14="http://schemas.microsoft.com/office/drawing/2010/diagram" xmlns:c16="http://schemas.microsoft.com/office/drawing/2014/chart" xmlns:dgm="http://schemas.openxmlformats.org/drawingml/2006/diagram" xmlns:c15="http://schemas.microsoft.com/office/drawing/2012/chart">
                     <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
                        <pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
                           <pic:nvPicPr>
                              <pic:cNvPr id="1" name="Grafik 1" descr="Ein Bild, das Text, Person, Gruppe, Personen enthält.&#xa;&#xa;Automatisch generierte Beschreibung"/>
                              <pic:cNvPicPr/>
                           </pic:nvPicPr>
                           <pic:blipFill>
                              <a:blip cstate="print" r:embed="rId6">
                                 <a:extLst>
                                    <a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}">
                                       <a14:useLocalDpi val="false"/>
                                    </a:ext>
                                 </a:extLst>
                              </a:blip>
                              <a:stretch>
                                 <a:fillRect/>
                              </a:stretch>
                           </pic:blipFill>
                           <pic:spPr>
                              <a:xfrm>
                                 <a:off x="0" y="0"/>
                                 <a:ext cx="5760720" cy="4372610"/>
                              </a:xfrm>
                              <a:prstGeom prst="rect">
                                 <a:avLst/>
                              </a:prstGeom>
                           </pic:spPr>
                        </pic:pic>
                     </a:graphicData>
                  </a:graphic>
               </wp:inline>
            </w:drawing>
         </w:r>
      </w:p>

Then I follow the answer to this question to copy the relationship part: How to copy docx file header image into anoter docx file in java

But the image does not appear in the target document. My code looks like this:

MainDocumentPart sourcePart = sourceWmlPackage.getMainDocumentPart();
MainDocumentPart targetPart = targetWmlPackage.getMainDocumentPart();
if (sourcePart.getRelationshipsPart() != null) {
    RelationshipsPart sourceRelationshipsPart = sourcePart.getRelationshipsPart();
    try {
        for (Relationship sourceRelationship : sourceRelationshipsPart.getContents().getRelationship()) {
            Part part = sourcePart.getRelationshipsPart().getPart(sourceRelationship.getId());
            if (part.getContentType().equals("image/jpeg")) {
                if (part instanceof BinaryPart) {
                    ((BinaryPart) part).getBuffer();
                }
                targetWmlPackage.getParts().getParts().put(part.getPartName(), part);
                part.setPackage(targetWmlPackage);
                part.setOwningRelationshipPart(targetPart.getRelationshipsPart());
            }
        }
    } catch (Docx4JException e) {
        e.printStackTrace();
    }
}

What am I doing wrong?

0 Answers
Related