title: Change image layout or wrap in DOCX with Apache POI
body: <p>I paste image into docx programmatically. But in result the layout does not suit me. Faced a lack of documentation.
  I need to change image wrap (layout). For example now I have this:</p>
  
  <p><a href="https://i.stack.imgur.com/weZ9A.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/weZ9A.png" alt="enter image description here"></a></p>
  
  <p>But want this:</p>
  
  <p><a href="https://i.stack.imgur.com/DipHD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DipHD.png" alt="enter image description here"></a></p>
  
  <p><strong>UPD1</strong>: What I do: iterate through the paragraphs, then through the runs and find certain run with special bookmark. In this run I add picture:</p>
  
  <pre><code>XWPFPicture pic =  run.addPicture(
          new ByteArrayInputStream(picSource),
          Document.PICTURE_TYPE_PNG,
          "pic",
          Units.toEMU(100),
          Units.toEMU(30));
  </code></pre>
  
  <p><strong>UPD2</strong>: Investigated something interesting inside this class:</p>
  
  <pre><code>org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTAnchor
  </code></pre>
  
  <p>method <code>setWrapTight(CTWrapTight var1)</code>. May be is it. By still don't know how to apply it to my code.</p>
  
  <p><strong>UPD3</strong>: Finally I came to this (currentRun - run with our picture):</p>
  
  <pre><code>    CTWrapTight ctWrapTight = currentRun.getCTR().getDrawingList().get(0).addNewAnchor().addNewWrapTight();
   CTWrapPath ctWrapPath = ctWrapTight.addNewWrapPolygon();
  
   CTPoint2D ctStart = ctWrapPath.addNewStart();
   ctStart.setX(0L);
   ctStart.setY(0L);
  
   CTPoint2D ctLineTo1 = ctWrapPath.addNewLineTo();
   CTPoint2D ctLineTo2 = ctWrapPath.addNewLineTo();
   CTPoint2D ctLineTo3 = ctWrapPath.addNewLineTo();
  
   ctLineTo1.setX(21384L);
   ctLineTo1.setY(20520L);
  
   ctLineTo2.setX(21384L);
   ctLineTo2.setY(0L);
  
   ctLineTo3.setX(0L);
   ctLineTo3.setY(0L);
  
  ctWrapTight.setWrapText(STWrapText.BOTH_SIDES);
  </code></pre>
  
  <p>But it's break down document when I try to open it: </p>
  
  <blockquote>
    <p>We're sorry. We can't open document because we found a problem with
    its contents.</p>
  </blockquote>
  
  <p>Dependency are:</p>
  
  <pre><code>&lt;dependency&gt;
          &lt;groupId&gt;org.apache.poi&lt;/groupId&gt;
          &lt;artifactId&gt;poi-ooxml&lt;/artifactId&gt;
          &lt;version&gt;3.17&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.apache.poi&lt;/groupId&gt;
          &lt;artifactId&gt;poi-ooxml-schemas&lt;/artifactId&gt;
          &lt;version&gt;3.17&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.apache.poi&lt;/groupId&gt;
          &lt;artifactId&gt;ooxml-schemas&lt;/artifactId&gt;
          &lt;version&gt;1.3&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>