title: How to force Apache POI to create a relations file for my header?
body: <p>I've got a very simple file generated by Apache POI, that contains an image and a sentence in the page header, nothing else. The image, while embedded, doesn't display under Word.</p>
  
  <p>I have taken a lot of time to compare the same file generated by Word, removing the differences one by one in order to find the root cause.</p>
  
  <p>Here's the Apache POI-generated file structure:</p>
  
  <pre><code>.
  ├── [Content_Types].xml
  ├── _rels
  ├── docProps
  │   ├── app.xml
  │   └── core.xml
  └── word
      ├── _rels
      │   └── document.xml.rels
      ├── document.xml
      ├── footer1.xml
      ├── header1.xml
      ├── media
      │   └── image1.png
      └── settings.xml
  </code></pre>
  
  <p>Here's the <code>header1.xml</code> file (stripped a <em>little</em> bit):</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  &lt;w:hdr xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mo="http://schemas.microsoft.com/office/mac/office/2008/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 wp14"&gt;
      &lt;w:p&gt;
          &lt;w:pPr&gt;
              &lt;w:jc w:val="left" /&gt;&lt;/w:pPr&gt;
          &lt;w:r&gt;
              &lt;w:drawing&gt;
                  &lt;wp:inline distT="0" distB="0" distL="0" distR="0"&gt;
                      &lt;wp:extent cx="1193800" cy="635000" /&gt;
                      &lt;wp:docPr id="2" name="Picture 2" descr="Generated" /&gt;
                      &lt;a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"&gt;
                          &lt;a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture"&gt;
                              &lt;pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"&gt;
                                  &lt;pic:nvPicPr&gt;
                                      &lt;pic:cNvPr id="2" name="Generated" /&gt;
                                      &lt;pic:cNvPicPr/&gt;&lt;/pic:nvPicPr&gt;
                                  &lt;pic:blipFill&gt;&lt;a:blip r:embed="rId2" /&gt;
                                      &lt;a:stretch&gt;&lt;a:fillRect/&gt;&lt;/a:stretch&gt;
                                  &lt;/pic:blipFill&gt;
                                  &lt;pic:spPr&gt;
                                      &lt;a:xfrm&gt;&lt;a:off x="0" y="0" /&gt;&lt;a:ext cx="1193800" cy="635000" /&gt;&lt;/a:xfrm&gt;
                                      &lt;a:prstGeom prst="rect"&gt;&lt;a:avLst/&gt;&lt;/a:prstGeom&gt;
                                  &lt;/pic:spPr&gt;
                              &lt;/pic:pic&gt;
                          &lt;/a:graphicData&gt;
                      &lt;/a:graphic&gt;
                  &lt;/wp:inline&gt;
              &lt;/w:drawing&gt;
          &lt;/w:r&gt;
      &lt;/w:p&gt;
  &lt;/w:hdr&gt;
  </code></pre>
  
  <p>The image XML code is generated by hand using <a href="https://stackoverflow.com/a/17849834/334493">the solution found here</a>.</p>
  
  <p>The reference ID is relative to what's inside <code>_rels/document.xml.rels</code>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"&gt;
    &lt;Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/&gt;
    &lt;Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image1.png"/&gt;
    &lt;Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/header" Target="header1.xml"/&gt;
    &lt;Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer" Target="footer1.xml"/&gt;
  &lt;/Relationships&gt;
  </code></pre>
  
  <p>So basically insert image <code>rId2</code> => <code>media/image1.png</code>.</p>
  
  <p>Now with the document generated by Word, there are a lot more files generated, but once striped, this is what it looks like:</p>
  
  <pre><code>.
  ├── [Content_Types].xml
  ├── _rels
  ├── docProps
  │   ├── app.xml
  │   └── core.xml
  └── word
      ├── _rels
      │   ├── document.xml.rels
      │   └── header1.xml.rels
      ├── document.xml
      ├── header1.xml
      ├── media
      │   └── image1.png
      └── settings.xml
  </code></pre>
  
  <p>Same files, except for the <code>_rels/header1.xml.rels</code>, which contains:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships&gt;
      &lt;Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image1.png"/&gt;
  &lt;/Relationships&gt;
  </code></pre>
  
  <p>And the only difference is the relation ID, which is <code>rId1</code>, which is taken from <code>header1.xml.rels</code>, which points to the image.</p>
  
  <p>Now I'm no expert in OOXML nor Apache POI, but I would like POI to put the header's relations in a separate file. Is it possible?</p>
  
  <p>I also noted that these XML namespaces <em>need</em> to be added for the image to be decoded:</p>
  
  <pre><code>xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
  xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
  xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
  </code></pre>
  
  <p>Update: code uploaded to <a href="https://gist.github.com/BenoitDuffez/b132d45747ef8c2e9e7c" rel="nofollow noreferrer">https://gist.github.com/BenoitDuffez/b132d45747ef8c2e9e7c</a></p>
  
  <p>Processing:</p>
  
  <ul>
  <li>the code calls <code>Exporter#export</code></li>
  <li>the <code>Exporter</code> class calls through <code>POIExporter</code> methods such as <code>#openDocument</code>, <code>#addImage</code>, etc</li>
  <li>the order of calls is: <code>createDocument</code>, <code>startHeader</code>, <code>createParagraph</code>, <code>addImage</code>, <code>setParagraphAlignment</code>, <code>endParagraph</code></li>
  </ul>
  
  <p>I attached a full log to the gist.</p>