title: Add an image to an XWPFDocument using Apache POI
body: <p>I have tried adding an image to the header of an XWPFDocument with no success. I am using Apache POI.</p>
  
  <p>Here is what I am currently using as a solution:</p>
  
  <p><strong>Header creation</strong></p>
  
  <pre><code>XWPFDocument document = new XWPFDocument(OPCPackage.open(docxInputStream));
  CTP headerCtp = CTP.Factory.newInstance();
  CTR headerCtr = headerCtp.addNewR();
  
  XWPFParagraph headerParagraph = new XWPFParagraph(headerCtp, document);
  XWPFRun run = headerParagraph.getRun(headerCtr);
  InputStream pictureInputStream = new FileInputStream("D:\\logo.jpg");
  run.addPicture(pictureInputStream, XWPFDocument.PICTURE_TYPE_JPEG, "logo.jpg", 300, 150);
  pictureInputStream.close();
  run.addTab();
  run.setText(contentName);
  
  setTabStop(headerCtp, STTabJc.Enum.forString("right"), BigInteger.valueOf(9000));
  
  XWPFParagraph[] headerParagraphs = {headerParagraph};
  CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
  XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy(document, sectPr);
  headerFooterPolicy.createHeader(STHdrFtr.DEFAULT, headerParagraphs);
  </code></pre>
  
  <p><strong>setTabStop method</strong></p>
  
  <pre><code>private static void setTabStop(CTP oCTP, STTabJc.Enum oSTTabJc, BigInteger oPos) {
      CTPPr oPPr = oCTP.getPPr();
      if (oPPr == null) {
          oPPr = oCTP.addNewPPr();
      }
  
      CTTabs oTabs = oPPr.getTabs();
      if (oTabs == null) {
          oTabs = oPPr.addNewTabs();
      }
  
      CTTabStop oTabStop = oTabs.addNewTab();
      oTabStop.setVal(oSTTabJc);
      oTabStop.setPos(oPos);
  }
  </code></pre>
  
  <p>The trouble I am having is that the image is loaded in the run, meaning the size of the pictures array is 1, however it isn't getting displayed in the document.</p>
  
  <p>I have tried numerous solutions such as the following ones:</p>
  
  <p><a href="https://stackoverflow.com/questions/21235868/add-image-into-a-word-docx-document-header-using-apache-poi-xwpf">Add image into a word .docx document header using Apache POI XWPF</a></p>
  
  <p><a href="https://stackoverflow.com/questions/7674115/how-to-add-a-picture-to-a-docx-document-with-apache-poi-xwpf-in-java">how to add a picture to a .docx document with Apache POI XWPF in java</a></p>
  
  <p><a href="https://stackoverflow.com/questions/30645062/java-poi-add-a-picture-to-the-header">Java - POI - Add a picture to the header</a></p>
  
  <p><a href="https://stackoverflow.com/questions/27583478/how-i-can-add-an-image-as-my-header-in-a-word-document-using-apache-poi">How i can add an Image as my header in a word document using Apache POI</a></p>
  
  <p>Any hints and ideas as to what I may be doing wrong are welcome.</p>
  
  <p>Thanks.</p>