title: Adding annotations with iTextSharp to rotated crop box
body: <p>So, I'm trying to simply add a text annotation to a pdf at the top left corner of a pdf document. Current code is like this:</p>
  
  <pre><code>public static byte[] StampPDFDocument(byte[] pdf, string stampString) {
              using (var ms = new MemoryStream()) {
  
                  var reader = new iTextSharp.text.pdf.PdfReader(pdf);
                  var stamper = new iTextSharp.text.pdf.PdfStamper(reader, ms);
  
                  var box = reader.GetCropBox(1);
                  var left = box.Left;
                  var top = box.Top;
  
                  iTextSharp.text.Rectangle newRectangle = new iTextSharp.text.Rectangle(left + 20, top - 20, left + 250, top - 40);
                  var pcb = new iTextSharp.text.pdf.PdfContentByte(stamper.Writer);
                  pcb.SetColorFill(iTextSharp.text.BaseColor.RED);
  
                  var annot = iTextSharp.text.pdf.PdfAnnotation.CreateFreeText(stamper.Writer, newRectangle, stampString, pcb);
                  annot.Flags = iTextSharp.text.pdf.PdfAnnotation.FLAGS_PRINT;
  
                  annot.BorderStyle = new iTextSharp.text.pdf.PdfBorderDictionary(0, 0);
                  stamper.AddAnnotation(annot, 1);
                  stamper.Close();
                  return ms.ToArray();
              }
          }
  </code></pre>
  
  <p>Now, original code was just using box = reader.GetPageSize(1). Well, that I soon realized causes problems if the document has been rotated. Ok. No problem, there is a function called reader.GetPageSizeWithRotation. That worked like a charm. However, now I'm getting documents that have a different cropbox. So the annotation that I was adding was outside the cropbox area. So this current code only works for non rotated documents. The question is, how does one get the top left corener in a pdf document regardless of whether the document is rotated or contains a different cropbox than the document?</p>