Adding annotations with iTextSharp to rotated crop box
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:
public static byte[] StampPDFDocument(byte[] pdf, string stampString) {
using (var ms = new MemoryStream开发者_Python百科()) {
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();
}
}
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?
Here's what I ended up with.
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);
int rotation = reader.GetPageRotation(1);
var box = reader.GetPageSizeWithRotation(1);
var cropbox = reader.GetCropBox(1);
float left = cropbox.Left;
float top = cropbox.Top;
if (rotation == 90) {
left = cropbox.Bottom;
top = box.Height - cropbox.Left;
cropbox = new iTextSharp.text.Rectangle(left, top, left + cropbox.Height, top - cropbox.Width);
}
else if (rotation == 180) {
left = box.Width - cropbox.Left - cropbox.Width;
top = box.Height - cropbox.Bottom;
cropbox = new iTextSharp.text.Rectangle(left, top, left + cropbox.Width, top - cropbox.Height);
}
else if (rotation == 270) {
left = box.Width - cropbox.Top;
top = cropbox.Right;
cropbox = new iTextSharp.text.Rectangle(left, top, left + cropbox.Height, top - cropbox.Width);
}
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.Rotate = reader.GetPageRotation(1);
annot.BorderStyle = new iTextSharp.text.pdf.PdfBorderDictionary(0, 0);
stamper.AddAnnotation(annot, 1);
stamper.Close();
return ms.ToArray();
}
}
Here's the source for getPageSizeWithRotation:
public Rectangle getPageSizeWithRotation(int index) {
return getPageSizeWithRotation(pageRefs.getPageNRelease(index));
}
public Rectangle getPageSizeWithRotation(PdfDictionary page) {
Rectangle rect = getPageSize(page);
int rotation = getPageRotation(page);
while (rotation > 0) {
rect = rect.rotate();
rotation -= 90;
}
return rect;
}
So all you need to do to roll your own is to write a function that calls getCropBox()
instead of getPageSize()
.
PS: getCropBox()
will return the media box if there's no crop box, so you don't have to call getCropBox and getPageSize separately.
精彩评论