AdjustCoords.cs
// 完毕:
using System;
using System.IO;
using System.Drawing;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Pdf.Annotations;

namespace DsPdfWeb.Demos
{
    // 此示例演示如何使用 Page.AdjustCooperatives() 方法
    // 转换视觉坐标(从左上角开始测量)
    // 根据 GcDocs.Pdf 规则)纠正 PDF 中的坐标
    // 已加载到 GcPdfDocument 中,并且可能具有任意未知数
    // 应用于其页面的转换。调整后的坐标可以是
    // 用于定位注释(在本例中为编辑)等。
    // 
    // 此示例中使用的 PDF 有一页包含示例发票的扫描件
    // 顺时针旋转了 90 度。 PDF 页面旋转 270 度
    // 补偿(以便页面在视觉上具有正确的纵向方向)。
    public class AdjustCoords
    {
        public int CreatePDF(Stream stream)
        {
            var doc = new GcPdfDocument();
            using (var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "invoice-scan-rot270.pdf")))
            {
                doc.Load(fs);

                if (doc.Pages.Count != 1)
                    throw new Exception("Unexpected: sample invoice should have exactly one page.");

                var page = doc.Pages[0];

                // 要编辑的区域的边界矩形,
                // 从 PDF 页面的左上角开始测量。
                // 如果按原样使用这个矩形,它将错过目标内容:
                var rectToRedact = new RectangleF(20, 170, 200, 40);

                // 调整后的边界矩形,考虑到
                // 任何可能的页面转换(在本例中为旋转):
                var adjusted = page.AdjustCoordinates(rectToRedact);

                // 注意:如果使用“rectToRedact”而不是“调整”,
                // 编辑将错过目标(客户名称/地址):
                var redact = new RedactAnnotation()
                {
                    Rect = adjusted,
                    OverlayFillColor = Color.Orange,
                    OverlayText = "已编辑",
                    Page = page
                };
                // 应用编辑:
                doc.Redact(redact);

                // 完毕:
                doc.Save(stream);
                return doc.Pages.Count;
            }
        }
    }
}