title: iTextSharp - Crop PDF File (C#)
body: <p>I want to crop PDF File using iTextSharp and rectangle (0,1000,600,155). Everything is fine and when you open created *.pdf file you can see only that cropped content, BUT! If you parse that pdf, there are still information and text from not visible part of document, I can't accept that. How can I remove that data completly?</p>
  
  <p>Here is my code sample:</p>
  
  <pre><code>        static void cropiTxtSharp(){
          string file ="C:\\testpdf.pdf";
          string oldchar = "testpdf.pdf";
          string repChar = "test.pdf";
          PdfReader reader = new PdfReader(file);
          PdfDictionary pageDict;
          PdfRectangle rect = new PdfRectangle(0, 1000, 600, 115);
          pageDict = reader.GetPageN(1);
          pageDict.Put(PdfName.CROPBOX, rect);
          PdfStamper stamper = new PdfStamper(reader, new FileStream(file.Replace(oldchar, repChar), FileMode.Create, FileAccess.Write));
          stamper.Close();
          reader.Close();
      }
  </code></pre>
  
  <p>EDIT:
  Here is code which works, I spend some hours but finally I did it :P
  <br/>First, add following to project:</p>
  
  <pre><code>using iTextSharp.text.pdf;
  using iTextSharp.text;
  using iTextSharp.xtra.iTextSharp.text.pdf.pdfcleanup;
  </code></pre>
  
  <p><br/>Then you can use my code:</p>
  
  <pre><code>    static void textsharpie()
      {
          string file = "C:\\testpdf.pdf";
          string oldchar = "testpdf.pdf";
          string repChar = "test.pdf";
          PdfReader reader = new PdfReader(file);
          PdfStamper stamper = new PdfStamper(reader, new FileStream(file.Replace(oldchar, repChar), FileMode.Create, FileAccess.Write));
          List&lt;PdfCleanUpLocation&gt; cleanUpLocations = new List&lt;PdfCleanUpLocation&gt;();
          cleanUpLocations.Add(new PdfCleanUpLocation(1, new iTextSharp.text.Rectangle(0f, 0f, 600f, 115f), iTextSharp.text.BaseColor.WHITE));
          PdfCleanUpProcessor cleaner = new PdfCleanUpProcessor(cleanUpLocations, stamper);
          cleaner.CleanUp();
          stamper.Close();
          reader.Close();
      }
  </code></pre>
  
  <p><br/> Unfortunatelly I can't use that code if I want to commercialize my application without paying for license, so I had to think on different library...</p>