title: itext : How to detect PDF orientation
body: <p>I'm using itext5 to scale down pdf document, however i notice that the rotation information is rarely used in pdf documents metadata, it's still possible to find out the orientation by checking the width and the height of the page but with the following pdf document i end up with a pdf that has both pages in landscape orientation and in portrait orientation however all the pages are rendered in the same orientation (in the common pdf viewers). 
  My question is where is that information stored ? How the pdf viewer is able to render the document as it should ?</p>
  
  <p><a href="https://github.com/laosmd/iText/blob/master/pdf_tmpl.pdf" rel="nofollow noreferrer">This the orignal pdf file</a></p>
  
  <p><a href="https://github.com/laosmd/iText/blob/master/pdfinfo" rel="nofollow noreferrer">This is the metadata i'm able to retrieve</a></p>
  
  <p>This is the method used :</p>
  
  <pre><code>Document document = new Document(MarginsPDFHelper.DIM_PAGE, MarginsPDFHelper.MARGIN_GEN, MarginsPDFHelper.MARGIN_GEN, MarginsPDFHelper.MARGIN_TOP, MarginsPDFHelper.MARGIN_BOT);
  try {
      PdfReader reader = new PdfReader(pdfByte);
      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
      PdfWriter writer = PdfWriter.getInstance(document, outputStream);
      document.open();
      PdfContentByte content = writer.getDirectContent();
      for (int i = 1; i &lt;= reader.getNumberOfPages(); i++) {
          document.newPage();
          PdfImportedPage page = writer.getImportedPage(reader, i);
          Rectangle pageSize = reader.getPageSizeWithRotation(i);
          float scaleX = MarginsPDFHelper.DIM_PAGE.getWidth() / pageSize.getWidth();
          float scaleY = MarginsPDFHelper.DIM_PAGE.getHeight() / pageSize.getHeight();
          float scale = Math.min(scaleX, scaleY);
          content.addTemplate(page, scale, 0, 0, scale, 0, 0);
      }
      return outputStream.toByteArray();
  } catch (Exception e) {
      LOGGER.error("Can not scale pdf", e);
  } finally {
      if (document.isOpen()) {
          document.close();
      }                                                                                                                        }
  </code></pre>
  
  <p><a href="https://github.com/laosmd/iText/blob/master/mergedResult.pdf" rel="nofollow noreferrer">This the result i get after scaling down with itext5</a></p>