title: PDF - Remove White Margins
body: <p>I would like to know a way to remove white margins from a PDF file. Just like Adobe Acrobat X Pro does. I understand it will not work with every PDF file. </p>
  
  <p>I would guess that the way to do it, is by getting the text margins, then cropping out of that margins. </p>
  
  <p>PyPdf is preferred. </p>
  
  <p>iText finds text margins based on this code:</p>
  
  <pre><code>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;
      for (int i = 1; i &lt;= reader.getNumberOfPages(); i++) {
          finder = parser.processContent(i, new TextMarginFinder());
          PdfContentByte cb = stamper.getOverContent(i);
          cb.rectangle(finder.getLlx(), finder.getLly(),
              finder.getWidth(), finder.getHeight());
          cb.stroke();
      }
      stamper.close();
  }
  </code></pre>