如何使用 PdfSharp 创建注释以突出显示现有 PDF 的文本

How to create annotation using PdfSharp to highlight text of existing PDF

我想知道是否可以使用 PdfSharp 在现有 PDF 中创建文本高亮注释?

在 PdfSharp documentation 中,我看到 PdfTextAnnotationPdfRubberStampAnnotation 的示例,但没有找到文档中提到的以下注释的示例代码。

PdfLineAnnotation, PdfSquareAnnotation, PdfCircleAnnotation, 
PdfMarkupAnnotation, PdfHighlightAnnotation, PdfUnderlineAnnotation,
PdfSquigglyAnnotation, PdfSoundAnnotation, PdfMovieAnnotation.

这些注释是否尚未在 PdfSharp 中实现?如果有人已经实施,请指出代码示例。

我也遇到过这样的问题,我在 PdfSharp 库中没有找到任何其他注释类型。于是看了PDF Reference的文章《12.5注解》。例如,它表示要创建文本标记注释,开发人员需要指定 SubtypeQuadPoints 条目。请参阅下面的源代码。

namespace PdfSharp.Pdf.Annotations
{
    using Extensions;
    using System.Collections.Generic;

    public class PdfHighlightAnnotation : PdfMarkupAnnotation
    {
        public PdfHighlightAnnotation()
        {
            Initialize();
        }

        public PdfHighlightAnnotation(PdfDocument document)
            : base(document)
        {
            Initialize();
        }

        void Initialize()
        {
            Elements.SetName(Keys.Subtype, "/Highlight");
        }
    }

    public class PdfStrikeOutAnnotation : PdfMarkupAnnotation
    {
        public PdfStrikeOutAnnotation()
        {
            Initialize();
        }

        public PdfStrikeOutAnnotation(PdfDocument document)
            : base(document)
        {
            Initialize();
        }

        void Initialize()
        {
            Elements.SetName(Keys.Subtype, "/StrikeOut");
        }
    }

    public abstract class PdfMarkupAnnotation : PdfAnnotation
    {
        protected PdfMarkupAnnotation()
        { }

        protected PdfMarkupAnnotation(PdfDocument document)
            : base(document)
        { }

        public IEnumerable<PdfRectangle> Quadrilaterals
        {
            set {
                var points = new PdfArray();
                foreach (var r in value) {
                    points.Elements.AddRange(ToQuadPoints(r));
                }
                Elements.SetValue("/QuadPoints", points);
            }
        }

        private IEnumerable<PdfItem> ToQuadPoints(PdfRectangle r)
        {
            // Conversion from PdfRectangle coordinates
            //
            // Y ^
            //   |                     (X2 Y2)
            //   |        +-----------+
            //   |        |           |
            //   |        |           |
            //   |        +-----------+
            //   | (X1 Y1)
            //   |                              
            //   +-----------------------------> 
            //                                 X
            // to QuadPoints coordinates (x1 y1 x2 y2 x3 y3 x4 y4)
            //
            // Y ^
            //   | (x4 y4)             (x3 y3)
            //   |        +-----------+
            //   |        |           |
            //   |        |           |
            //   |        +-----------+
            //   | (x1 y1)             (x2 y2)
            //   |                              
            //   +-----------------------------> 
            //                                 X
            //
            return new List<PdfItem> { new PdfReal(r.X1), new PdfReal(r.Y1),
                                       new PdfReal(r.X2), new PdfReal(r.Y1),
                                       new PdfReal(r.X1), new PdfReal(r.Y2),
                                       new PdfReal(r.X2), new PdfReal(r.Y2)};
        }
    }
}

namespace PdfSharp.Extensions
{
    using PdfSharp.Pdf;
    using System.Collections.Generic;

    public static class ArrayElementsExtensions
    {
        public static void AddRange(this PdfArray.ArrayElements elements, IEnumerable<PdfItem> values)
        {
            foreach (var v in values) {
                elements.Add(v);
            }
        }
    }
}

多次尝试后在pdfsharp论坛跟进。我得到了解决方案。快乐编码! 原问答

private static void AddAnnotation(XGraphics graphics, PdfPage page, double x, double y, double height, double width)
{
    PdfSharp.Drawing.XRect rect = graphics.Transformer.WorldToDefaultPage(new XRect(new XPoint(x, y), new XSize(width, height)));
    XRect annotrect = new XRect(new XPoint(x, y), new XSize(width, height));
    // Create a PDF text annotation
    PdfTextAnnotation textAnnot = new PdfTextAnnotation();
    textAnnot.Title = "This is the title";
    textAnnot.Subject = "This is the subject";
    textAnnot.Contents = "This is the contents of the annotation.\rThis is the 2nd line.";            
    textAnnot.Rectangle = new PdfSharp.Pdf.PdfRectangle(rect);
    // The following lines of code did the trick
    textAnnot.Elements.Remove("/Subtype");
    textAnnot.Elements.Add("/Subtype", new PdfLiteral("/Square"));
    textAnnot.Elements.Add("/IC", new PdfLiteral("[0 0 0]"));
    textAnnot.Elements.Add("/CA", new PdfLiteral("0"));

    // Add the annotation to the page
    page.Annotations.Add(textAnnot);
}