Ajay Jadhav
Ajay Jadhav

Reputation: 2467

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

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

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.

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

Upvotes: 4

Views: 3238

Answers (2)

Dangerous Dev
Dangerous Dev

Reputation: 537

After trying multiple times and follow up with pdfsharp forum. I got the solution. Happy Coding!! Original Question and Answer here

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);
}

Upvotes: 0

Alexander Zinoviev
Alexander Zinoviev

Reputation: 144

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);
            }
        }
    }
}

Upvotes: 2

Related Questions