如何使用PdfSharp创建注释以突出显示现有PDF的文本 [英] How to create annotation using PdfSharp to highlight text of existing PDF

查看:271
本文介绍了如何使用PdfSharp创建注释以突出显示现有PDF的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以使用PdfSharp在现有的PDF中创建文本突出显示注释?

I would like know whether it is possible to create an text highlight annotation in an existing PDF using PdfSharp?

在PdfSharp 文档中,我看到了PdfTextAnnotation& PdfRubberStampAnnotation,但找不到文档中提到的以下注释的示例代码.

In the PdfSharp documentation, I see examples of PdfTextAnnotation & PdfRubberStampAnnotation, but did not find sample code for following annotations mentioned in the documentation.

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

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

Are these annotations yet to be implemented in PdfSharp? If someone has already implemented, please point me to the code samples.

推荐答案

我也遇到了这样的问题,我在PdfSharp库中没有找到任何其他注释类型.因此,我查看了

I also faced with such problem and I didn't find any additional annotation types at the PdfSharp library. So I looked at the article "12.5 Annotations" from the PDF Reference. For example, it says that to create a text markup annotation a developer need to specify the Subtype and QuadPoints entries. Please see the source code below.

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创建注释以突出显示现有PDF的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆