与iTextSharp的添加注解旋转裁剪框

与iTextSharp的添加注解旋转裁剪框

本文介绍了与iTextSharp的添加注解旋转裁剪框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我想在一个PDF文档的左上角有一个文本注释只需添加到PDF。当前的代码是这样的:

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()) {

                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();
            }
        }

现在,原来的代码只是用盒子=读者。 getpagesize的(1)。好吧,那我很快就意识到会产生问题,如果该文档已被旋转。好。没有问题,有一种叫做reader.GetPageSizeWithRotation函数。这工作就像一个魅力。不过,现在我得到具有不同的裁剪框的文档。所以,我是加注释是裁剪框区域之外。所以这个当前的代码只适用于非旋转的文件。现在的问题是,一个人如何得到一个PDF文档中左上角corener无论文档是否被旋转或包含不同的裁剪框不是文档?

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?

推荐答案

下面就是我结束了。

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();
    }
}

这篇关于与iTextSharp的添加注解旋转裁剪框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 09:47