title: Determine page orientation in Postscript
body: <p>I need to add a white rectangle and some text to the bottom left corner of each page of the PDF document using Ghostscript. To achieve this, I have created the following Postscript script:</p>
  
  <pre><code>&lt;&lt;
     /EndPage
     {
       2 eq { pop false }
       {
          newpath
          0 0 moveto
          0 20 lineto
          200 20 lineto
          200 0 lineto
          closepath
          %%gsave
          1 setgray
          fill
          %%grestore
          1 setlinewidth
          0 setgray
          stroke
  
          gsave
          /Times-Roman 9 selectfont              
          30 5 moveto                            
          (My text) show
  
          grestore
          true
       } ifelse
     } bind
  &gt;&gt; setpagedevice
  </code></pre>
  
  <p>This works well when combined with a Ghostscript command:</p>
  
  <pre><code>gs -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=output.pdf my_script.ps input.pdf
  </code></pre>
  
  <p>However, if input.pdf is in landscape mode, then the white box and text are printed in the upper left corner and not the lower left. I can get it to work by adding:</p>
  
  <pre><code>90 rotate 0 -595 translate
  </code></pre>
  
  <p>but I can't determine when the pages are in landscape mode vs. portrait mode.  I can get the page width and height, but even for landscape mode pages the width is smaller than the height. I tried the following but it fails:</p>
  
  <pre><code>/orient currentpagedevice /Orientation get def
  </code></pre>
  
  <p>I have been stuck with this for a while. Any help is greatly appreciated!</p>
  
  <p>(Ghostscript version is 9.25)</p>
  
  <p>[UPDATE]</p>
  
  <p>To illustrate how the width is smaller than height for a page in landscape mode, here's the script.ps I am using: <a href="https://gist.github.com/irinkaa/9faadf30b3a5a381a0b621d72b712020" rel="nofollow noreferrer">https://gist.github.com/irinkaa/9faadf30b3a5a381a0b621d72b712020</a></p>
  
  <p>And here are the <a href="https://drive.google.com/open?id=16u3W0rtNgB3LAtc2FbbShGxEyhjcvMMC" rel="nofollow noreferrer">input.pdf</a> and the <a href="https://drive.google.com/open?id=1KFmTuUOb7uSt7LBWOSlL8BCtPAU665eU" rel="nofollow noreferrer">output.pdf</a>. As you can see, <code>612.0 - 792.0</code> is printed inside the output file, showing that width (612) &lt; height (792).</p>
  
  <p>When I re-run the same command on the output file, it prints the same width and height values, but the box is then placed properly in the lower left corner.</p>
  
  <p>When I add the following to the script:</p>
  
  <pre><code>/orient currentpagedevice /Orientation get def
  </code></pre>
  
  <p>I get an error suggesting orientation isn't set (if I understand correctly):</p>
  
  <pre><code>Error: /undefined in --get--
  Operand stack:
     orient   --dict:212/312(ro)(L)--   Orientation
  Execution stack:
     %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   false   1   %stopped_push   1999   1   3   %oparray_pop   1998   1   3   %oparray_pop   1982   1   3   %oparray_pop   1868   1   3   %oparray_pop   --nostringval--   %errorexec_pop   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--
  Dictionary stack:
     --dict:977/1684(ro)(G)--   --dict:0/20(G)--   --dict:80/200(L)--
  Current allocation mode is local
  Current file position is 151
  GPL Ghostscript 9.25: Unrecoverable error, exit code 1
  </code></pre>