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

问题描述 投票:1回答:1

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

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

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

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

c# pdf pdf-generation pdfsharp
1个回答
2
投票

我也遇到了这样的问题,我没有在PdfSharp库中找到任何额外的注释类型。所以我查看了PDF Reference上的文章“12.5 Annotations”。例如,它表示要创建文本标记注释,开发人员需要指定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);
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.