title: Set margins to word document - Jasper/Apache POI
body: <p>I've created a document with Jasper. Tried to set margins in the below 3 ways,</p>
  
  <ol>
  <li>In iReport designer:</li>
  </ol>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;jasperReport ... pageWidth="595" pageHeight="842" ... columnWidth="595" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" ...&gt;
  </code></pre>
  
  <ol start="2">
  <li>In Java code</li>
  </ol>
  
  <pre class="lang-java prettyprint-override"><code>JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameterMap, connect);
  jasperPrint.setLeftMargin(20);
  jasperPrint.setRightMargin(20);
  jasperPrint.setTopMargin(20);
  jasperPrint.setBottomMargin(20);
  exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
  exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
  exporter.exportReport();
  </code></pre>
  
  <ol start="3">
  <li>In Java code with </li>
  </ol>
  
  <pre class="lang-java prettyprint-override"><code>JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameterMap,connect);  
  exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
  exporter.setParameter(JRExporterParameter.OFFSET_X, 20);
  exporter.setParameter(JRExporterParameter.OFFSET_Y, 20);                                  
  exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
  exporter.exportReport();
  </code></pre>
  
  <p>With all the above 3 ways I was able to give some spacing between the content and the border but when I do print the below warning appears</p>
  
  <p><img src="https://i.stack.imgur.com/SwFbQ.png" alt="enter image description here"></p>
  
  <p><strong>This is because the space between the margin and content given by Jasper is also considered as a data(Jasper) in Microsoft Word.</strong></p>
  
  <p>As far as I researched on this, its a known issue in Jasper and I doubt there's a fix for this till now. (If at all there's one please do let me know)</p>
  
  <p>But I don't want that warning popup to appear. This could be avoided by setting page margins while printing the document in word. Even then it will disturb the layout of the document by moving bottom part of the page to a new page.</p>
  
  <p>So my requirement is to avoid the warning pop-up and also automate the margin setting that is done in MS Word. For this I use Apache POI.</p>
  
  <p>The flow goes like this, the document is designed in iReport, compiled and generated document say <em>Document1.docx</em> is saved in a directory, which is the input to Apache POI where margins are set and again a new document is generated as <em>Document2.docx</em>.</p>
  
  <pre><code>File file = null;
  file = new File("C:\\temp\\Document1.docx");
  FileInputStream fis = new FileInputStream(file.getAbsolutePath());
  XWPFDocument document = new XWPFDocument(fis);
  CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
  CTPageMar pageMar = sectPr.addNewPgMar();
  pageMar.setLeft(BigInteger.valueOf(720L));
  pageMar.setTop(BigInteger.valueOf(720L));
  pageMar.setRight(BigInteger.valueOf(720L));
  pageMar.setBottom(BigInteger.valueOf(720L));
  FileOutputStream out = new FileOutputStream(new File(
          "C:\\temp\\Document2.docx"));
  document.write(out);
  out.close();
  </code></pre>
  
  <p>So <em>Document1.docx</em> will have the margin warning pop-up issue whereas <em>Document2.docx</em> does not still I have the same problem of the layout getting collapsed(bottom part of page1 moves to page2) which spoils the document.</p>
  
  <p>How do I avoid that? Also if there's any other approach simpler than this one to achieve what I want, please do share.</p>