C# 使用iTextSharp向旋转裁剪框添加注释
因此,我试图简单地将文本注释添加到pdf文档左上角的pdf中。当前代码如下所示:C# 使用iTextSharp向旋转裁剪框添加注释,c#,itextsharp,C#,Itextsharp,因此,我试图简单地将文本注释添加到pdf文档左上角的pdf中。当前代码如下所示: public static byte[] StampPDFDocument(byte[] pdf, string stampString) { using (var ms = new MemoryStream()) { var reader = new iTextSharp.text.pdf.PdfReader(pdf); v
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();
}
}
现在,原始代码只是使用box=reader.GetPageSize(1)。嗯,我很快意识到,如果文档被旋转,会导致问题。好啊没问题,有一个名为reader.GetPageSizeWithRotation的函数。这很有魅力。然而,现在我得到的文档有不同的cropbox。所以我添加的注释在cropbox区域之外。因此,当前代码仅适用于非旋转文档。问题是,如何在pdf文档中获得左上角的corener,而不管该文档是旋转的还是包含与文档不同的cropbox?以下是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;
}
因此,您只需编写一个函数来调用getCropBox()
,而不是getPageSize()
PS:
getCropBox()
如果没有裁剪框,将返回媒体框,因此您不必分别调用getCropBox和getPageSize。以下是我的结论
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();
}
}
如果存在“trimbox”,该如何处理?PdfReader可以使用getBoxSize():,java.lang.String从给定的PDF页面获取任何“box”)