title: Ghostscript rotate pages
body: <p>I use Ghostscript to convert PDF documents to PCL for printing. Recently I have the additional requirement that all pages must be rotated to Portrait before printing. I have found a way to do so using Ghostscript with following command and postscript function.</p>
  <pre><code>&quot;C:\Program Files (x86)\gs\bin\gswin32c.exe&quot; &quot;-dNOPAUSE&quot; &quot;-dNOPROMPT&quot; &quot;-dBATCH&quot; &quot;-sDEVICE=pxlmono&quot; &quot;-Ic:\Program Files (x86)\gs\fonts\;c:\Program Files (x86)\gs\lib\;c:\Program Files (x86)\gs\lib\;&quot; &quot;-r300&quot; &quot;-sOutputFile=C:\EXPORTFILE_e542e04f-5e84-4c8e-9b41-55480cd5ec52.cache&quot; &quot;rotate612x792.ps&quot; &quot;C:\EXPORTFILE_3a5de9da-d9ca-4562-8cb6-10fb8715385a.cache&quot;
  </code></pre>
  <p>Contents of rotate612x792.ps</p>
  <pre><code>%! Rotate Pages
  &lt;&lt; /Policies &lt;&lt; /PageSize 5 &gt;&gt; 
     /PageSize [612 792] 
     /InputAttributes currentpagedevice 
     /InputAttributes get mark exch {1 index /Priority eq not {pop &lt;&lt; /PageSize [612 792] &gt;&gt;} if }  forall &gt;&gt;
     &gt;&gt; setpagedevice
  </code></pre>
  <p>The problem is that this function replaces all page sizes with letter size. My documents are sometimes legal or A4. I have tried to modify this function to replace landscape sizes with their portrait counterpart, but have not been able to produce functioning postscript. I need to be pointed in the right direction to produce the postscript equivalent of the following pseudo code.</p>
  <pre><code>for(each page)
  {
     if(PageSize == [792 612])
         PageSize = [612 792];
  }
  </code></pre>
  <p>I am aware that there are non-Ghostscript ways of rotating pages, but if I can get this to work it would fit nicely into my process and would not reduce performance.</p>
  <p>Here is a sample of one of my pdf files:
  <a href="https://docs.google.com/open?id=0B2LzHaZ9pUynWXc2X0J3M0RvN28" rel="nofollow noreferrer">Sample1.pdf</a></p>