title: Landscape and portrait pages in the same word document using Apache POI XWPF in Java
body: <p>I am trying to use Java and the Apache POI library to create a word document that contained some landscape and some portrait pages. I can change the orientation of all the pages, but is there a way to change just the orientation of some of them? I have tried using different sections and bodies, but to no avail.</p>
  
  <p>At the moment I have written a function that takes a XWPFDocument as input and changes the orientation of a new body. I had hoped changing the orientation for new body would not affect the previous pages, but it affects the entire document.</p>
  
  <pre><code>private void changeOrientation(XWPFDocument document, String orientation){
      CTDocument1 doc = document.getDocument();
      CTBody body = doc.addNewBody();
      body.addNewSectPr();
      CTSectPr section = body.getSectPr();
      if(!section.isSetPgSz()) {
          section.addNewPgSz();
      }
      CTPageSz pageSize = section.getPgSz();
      if(orientation.equals("landscape")){
          pageSize.setOrient(STPageOrientation.LANDSCAPE);
          pageSize.setW(BigInteger.valueOf(842 * 20));
          pageSize.setH(BigInteger.valueOf(595 * 20));
      }
      else{
          pageSize.setOrient(STPageOrientation.PORTRAIT);
          pageSize.setH(BigInteger.valueOf(842 * 20));
          pageSize.setW(BigInteger.valueOf(595 * 20));
      }
  }
  </code></pre>
  
  <p>Thanks!</p>