title: How to truly crop a PDF file?
body: <p>I am trying to extract and save a certain portion of a PDF file using specific coordinates (x,y,w,h). I am using the following code, which seems to be working okay:</p>
  
  <pre><code>Function CroppedPdf(Source As Byte(), PageNumber As Integer,
          Rect As System.Drawing.Rectangle) As MemoryStream
      Dim reader As New PdfReader(Source)
      Dim h = reader.GetPageSize(1).Height
      Dim document = New iTextSharp.text.Document(New iTextSharp.text.Rectangle(
              Rect.Right / 300 * 72, h - (Rect.Top / 300 * 72), Rect.Left / 300 * 72,
              h - (Rect.Bottom / 300 * 72)))
      document.SetMargins(0, 0, 0, 0)
      Dim destination = New MemoryStream
      Dim writer = PdfWriter.GetInstance(document, destination)
      document.Open()
      Dim cb = writer.DirectContent
      document.NewPage()
      Dim page = writer.GetImportedPage(reader, 1)
      cb.AddTemplate(page, 0, 0)
      document.Close()
      Return destination
  End Function
  </code></pre>
  
  <p>The problem is that the resulting pdf is only <strong>seemingly</strong> cropped. When I try to run text extraction on it, I get back the text of the entire original source document. Furthermore, when splitting up a page in 10 pieces, the same document is actually stored 10 times with differences only in the viewport. How can I truly crop the PDF file, storing only the exact portion of the file I am interested in?</p>