title: Cropping PDF using iText (java PDF library)
body: <p>I need to crop PDF file using this <a href="http://itextpdf.com/examples/iia.php?id=280" rel="nofollow">code</a>. <code>cb</code> will output the needed coordinates for the cropping. </p>
  
  <p>How can efficiently do this for every page in a PDF file?</p>
  
  <p>Did this code, but does not work for every pdf input: </p>
  
  <pre><code>public class CropPages {
  
  public static final String PREFACE = "input.pdf";
  public static final String RESULT = "cropped.pdf";
  
  
  public void addMarginRectangle(String src, String dest)
      throws IOException, DocumentException {
      PdfReader reader = new PdfReader(src);
      PdfReaderContentParser parser = new PdfReaderContentParser(reader);
      PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
      TextMarginFinder finder;
      PdfDictionary pageDict;
      for (int i = 1; i &lt;= reader.getNumberOfPages(); i++) {
          finder = parser.processContent(i, new TextMarginFinder());
          //PdfContentByte cb = stamper.getOverContent(i);
          PdfRectangle rect = new PdfRectangle((finder.getLlx()-5), (finder.getLly()-5), (finder.getUrx()+5), (finder.getUry()+5));
          if (i &lt;= 10){
                   System.out.println(rect);
                   }
          pageDict = reader.getPageN(i);
          pageDict.put(PdfName.CROPBOX, rect);
       }
      stamper.close();
  }
  
  public static void main(String[] args) throws IOException, DocumentException {
      new CropPages().addMarginRectangle(PREFACE, RESULT);
  }
  </code></pre>
  
  <p>}</p>