title: Setting new page size to existing PDF document using Java IText
body: <p>I want to set a new page size to my existing PDF document without cropping the contents. I am writing the following code but it just crops my PDF file from the bottom resulting in the loss of content.</p>
  
  <p>The current size is 8.26" X 11.69 " and I need to make it 8.5" X 11". </p>
  
  <p>My code only converts 11.59 to 11. Tried to change 8.26 but its not woking.</p>
  
  <p>Can any one please help? I am using itextpdf-5.5.8 with Java.</p>
  
  <pre><code>import com.itextpdf.text.DocumentException;
  import com.itextpdf.text.pdf.GrayColor;
  import com.itextpdf.text.pdf.PdfArray;
  import com.itextpdf.text.pdf.PdfContentByte;
  import com.itextpdf.text.pdf.PdfDictionary;
  import com.itextpdf.text.pdf.PdfName;
  import com.itextpdf.text.pdf.PdfNumber;
  import com.itextpdf.text.pdf.PdfReader;
  import com.itextpdf.text.pdf.PdfRectangle;
  import com.itextpdf.text.pdf.PdfStamper;
  
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.IOException;
  
  
  
  public class PageSize {
  
      public static final String SRC = "C:/Temp/BC.pdf";
      public static final String DEST = "C:/Temp/BC_New.pdf";
  
      public static void main(String[] args) throws IOException, DocumentException {
          File file = new File(DEST);
          file.getParentFile().mkdirs();
          new PageSize().manipulatePdf(SRC, DEST);
      }
  
      public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
          PdfReader reader = new PdfReader(src);
          int n = reader.getNumberOfPages();
          PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
          // properties
          PdfContentByte over;
          PdfDictionary pageDict;
          PdfRectangle rect = new PdfRectangle(55, 76, 560, 816);
          PdfArray mediabox;
          float llx, lly, ury,llz;
          // loop over every page
          for (int i = 1; i &lt;= n; i++) {
              pageDict = reader.getPageN(i);
              mediabox = pageDict.getAsArray(PdfName.MEDIABOX);
              llx = mediabox.getAsNumber(0).floatValue();
              lly = mediabox.getAsNumber(1).floatValue();
              llz = mediabox.getAsNumber(2).floatValue();
              ury = mediabox.getAsNumber(3).floatValue();
              mediabox.set(1, new PdfNumber((lly + 50)));
              over = stamper.getOverContent(i);
              over.saveState();
                         over.restoreState();
          }
  
  
  
          stamper.close();
          reader.close();
      }
  }
  </code></pre>
  
  <p><strong>UPDATED</strong>
  Here is the code I used to reduce 11.69" to 11". It works fine. But, it won't increase the width 8.26 to 8.5"</p>
  
  <pre><code>import com.itextpdf.text.DocumentException;
  import com.itextpdf.text.pdf.GrayColor;
  import com.itextpdf.text.pdf.PdfArray;
  import com.itextpdf.text.pdf.PdfContentByte;
  import com.itextpdf.text.pdf.PdfDictionary;
  import com.itextpdf.text.pdf.PdfName;
  import com.itextpdf.text.pdf.PdfNumber;
  import com.itextpdf.text.pdf.PdfReader;
  import com.itextpdf.text.pdf.PdfRectangle;
  import com.itextpdf.text.pdf.PdfStamper;
  
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.IOException;
  
  
  
  public class PageSize {
  
      public static final String SRC = "C:/Temp/Test.pdf";
      public static final String DEST = "C:/Temp/BC_New.pdf";
  
      public static void main(String[] args) throws IOException, DocumentException {
          File file = new File(DEST);
          file.getParentFile().mkdirs();
          new PageSize().manipulatePdf(SRC, DEST);
      }
  
      public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
          PdfReader reader = new PdfReader(src);
          int n = reader.getNumberOfPages();
          PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
          // properties
          PdfContentByte over;
          PdfDictionary pageDict;
          PdfArray mediabox;
          float llx, lly, ury,llz;
          // loop over every page
          for (int i = 1; i &lt;= n; i++) {
              pageDict = reader.getPageN(i);
              mediabox = pageDict.getAsArray(PdfName.MEDIABOX);
              llx = mediabox.getAsNumber(0).floatValue();
              lly = mediabox.getAsNumber(1).floatValue();
              ury = mediabox.getAsNumber(3).floatValue();
              mediabox.set(0, new PdfNumber((llx - 17)));
              mediabox.set(1, new PdfNumber((lly + 50)));
              over = stamper.getOverContent(i);
              over.saveState();
                         over.restoreState();
          }
  
  
  
          stamper.close();
          reader.close();
      }
  }
  </code></pre>
  
  <p><strong>SOLVED:</strong></p>
  
  <p>It was a problem with the source file which I was trying to modify. The original source file was a 8.26" X 11.69". A hava program adds a logo to the file and saves it into a new file. </p>
  
  <p>Somehow due to this conversion, the new source file's X-axis got locked and hence my program was not able to modify the x axis.</p>
  
  <p>Hence, I move my program as the first step and it worked. My program first modifies the x and y axis and then passes the file to the other program which does the logo adding stuff. </p>
  
  <p>Thanks Bruno on this. :)</p>