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

namespace DsPdfWeb.Demos.Basics
{
    // 演示如何向 PDF 文档添加注释。
    public class AnnotationTypes
    {
        public int CreatePDF(Stream stream)
        {
            var doc = new GcPdfDocument();
            var page = doc.NewPage();
            var g = page.Graphics;
            // 注释作者的用户名:
            var user1 = "杰米·史密斯";
            var user2 = "简·多纳休";

            var noteWidth = 72 * 4;
            var gap = 8;

            var rc = Common.Util.AddNote(
                "此示例演示了可以使用 GcDocs.PDF 创建的某些类型的注释。\n" +
                "请注意,某些注释类型可能不会在某些查看器(例如内置浏览器查看器)中显示。" +
                "要查看此页面上的所有注释,请在 Acrobat Reader 或其他功能齐全的 PDF 查看器中打开它。" +
                "",
                page);

            // 文字注释:
            var ip = new PointF(rc.X, rc.Bottom + gap);
            rc = Common.Util.AddNote(
                "红色文本注释位于此注释的右侧。",
                page, new RectangleF(ip.X, ip.Y, noteWidth, 100));
            var textAnnot = new TextAnnotation()
            {
                UserName = user1,
                Contents = "这是注释 1,红色的。",
                Rect = new RectangleF(rc.Right, rc.Top, 72 * 2, 72),
                Color = Color.Red,
            };
            page.Annotations.Add(textAnnot);
            // 对之前注释的回复:
            var textAnnotReply = new TextAnnotation()
            {
                UserName = user2,
                Contents = "这是对第一个注释的回复。",
                Rect = new RectangleF(rc.Right, rc.Top, 72 * 2, 72),
                ReferenceAnnotation = textAnnot,
                ReferenceType = AnnotationReferenceType.Reply,
            };
            page.Annotations.Add(textAnnotReply);

            // 最初打开的文本注释:
            ip = new PointF(rc.X, rc.Bottom + gap);
            rc = Common.Util.AddNote(
                "最初打开的绿色文本注释放置在该注释的右侧。",
                page, new RectangleF(ip.X, ip.Y, noteWidth, 100));
            var textAnnotOpen = new TextAnnotation()
            {
                Open = true,
                UserName = user1,
                Contents = "这是最初打开的注释(绿色)。",
                Rect = new RectangleF(rc.Right, rc.Top, 72 * 2, 72),
                Color = Color.Green,
            };
            page.Annotations.Add(textAnnotOpen);

            // 自由文本注释(直接显示在页面上):
            ip = new PointF(rc.X, rc.Bottom + gap);
            rc = Common.Util.AddNote(
                "蓝色自由文本注释位于右侧下方,并带有从该注释到此注释的标注。",
                page, new RectangleF(ip.X, ip.Y, noteWidth, 100));
            var freeAnnot = new FreeTextAnnotation()
            {
                Rect = new RectangleF(rc.Right + 18, rc.Bottom + 9, 72 * 2, 72),
                CalloutLine = new PointF[]
                {
                    new PointF(rc.Left + rc.Width / 2, rc.Bottom),
                    new PointF(rc.Left + rc.Width / 2, rc.Bottom + 9 + 36),
                    new PointF(rc.Right + 18, rc.Bottom + 9 + 36),
                },
                LineWidth = 1,
                LineEndStyle = LineEndingStyle.OpenArrow,
                LineDashPattern = new float[] { 8, 4 },
                Contents = "这是一个自由文本注释,带有指向左侧注释的标注线。",
                Color = Color.LightSkyBlue,
            };
            page.Annotations.Add(freeAnnot);

            // 另一个自由文本注释,里面有一些富文本:
            ip = new PointF(rc.X, freeAnnot.Rect.Bottom + gap);
            var freeRichAnnot = new FreeTextAnnotation()
            {
                Rect = new RectangleF(ip.X - 144, ip.Y, 72 * 5, 72),
                LineWidth = 1,
                Color = Color.LightSalmon,
                RichText =
                    "<body><p>这是另一个<i>自由文本注释</i>,包含<b><i>富文本</i></b>内容。</p></body>"
            };
            page.Annotations.Add(freeRichAnnot);

            // 注释周围的方形注释:
            ip = new PointF(rc.X, freeRichAnnot.Rect.Bottom + gap * 2);
            rc = Common.Util.AddNote(
                "在该注释周围用 3 磅宽的橙色线绘制的方形注释具有与其关联的丰富文本。",
                page, new RectangleF(ip.X, ip.Y, noteWidth, 100));

            rc.Inflate(8, 8);
            var squareAnnot = new SquareAnnotation()
            {
                UserName = user2,
                Rect = rc,
                LineWidth = 3,
                Color = Color.Orange,
                RichText =
                    "<body><p>此<b><i>富文本</i></b>与文本注释周围的方形注释相关联。</p></body>"
            };
            page.Annotations.Add(squareAnnot);

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