logo
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

New Topic Post Reply
Options
Go to last post Go to first unread
Guest  
#1 Posted : Monday, September 4, 2017 7:36:11 PM(UTC)
Quote
Guest

Rank: Guest

Groups: Guests
Joined: 1/5/2016(UTC)
Posts: 154

Was thanked: 4 time(s) in 4 post(s)
Hello,

We bought the Pdfium.net SDK last year and we’re going to extend the license for an additional year.

Last year I asked for Note Actions, that man can move Notes inside a PDFDocument.
We need this functionalty, because our customers get PDF from their vendors with Notes.
These Notes are used to mark some regions on a car image e.g. if it has a damage on the left mudgard.

Could you please provide an example how to achieve these goals?
PDF document is attached
PDFNote.pdf (103kb) downloaded 37 time(s).
Paul Rayman  
#2 Posted : Monday, September 4, 2017 7:44:06 PM(UTC)
Quote
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 963

Thanks: 3 times
Was thanked: 115 time(s) in 112 post(s)
Hi,

Currently there is no any helper classes to work with anntotions. We are working on it and such classes may be released in few months I hope.

But you can get access to annotations through the Annots dictionary.

Please look at code below that demonstrate how to move annotation by a mouse.
Short description
1. Create a derived class of PdfViewer
2. Override MouseMove and MouseDown methods
3. FInd the annotation under mouse pointer
4. Change the Rect entry of the annotation dictionary.

Code:

public class MyPdfViewer : PdfViewer
{
    private PdfTypeDictionary _activeAnnot = null;

    protected override void OnMouseDown(MouseEventArgs e)
    {
        if (Document == null || CurrentPage == null)
        {
            base.OnMouseDown(e);
            return;
        }

        //Converts mouse coordinates to PDF user space.
        var pagePt = ClientToPage(CurrentIndex, e.Location);
        //Gets annotation under mouse pointer
        _activeAnnot = GetAnnotUnderMouse(pagePt);

        base.OnMouseDown(e);
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        if (Document == null || CurrentPage == null)
        {
            base.OnMouseMove(e);
            return;
        }

        if (_activeAnnot == null)
        {
            //Converts mouse coordinates to PDF user space.
            var pagePt = ClientToPage(CurrentIndex, e.Location);
            //Gets annot unde mouse pointer
            var annot = GetAnnotUnderMouse(pagePt);
            //Change mouse cursor's shape
            ChangeMouseCursor(annot);
            if(annot== null) //Also we should prevent the default behaviour if the cursor under  annotation
                base.OnMouseMove(e);
        }
        else //If mouse was pressed under an annotation
        {
            //Gets annot current rect;
            var rect = GetAnnotRect(_activeAnnot);
            //Converts mouse coordinate to PDF user space.
            var pagePt = ClientToPage(CurrentIndex, e.Location);
            //Calculates the new rect
            var newRect = new RectangleF(pagePt, rect.Size);
            //And set it to annotation
            SetAnnotRect(_activeAnnot, newRect);
            //Invalidate the viewer
            Invalidate();
        }
    }

    protected override void OnMouseUp(MouseEventArgs e)
    {
        _activeAnnot = null;
        base.OnMouseUp(e);
    }

    private void ChangeMouseCursor(PdfTypeDictionary annot)
    {
        if (annot != null && Cursor != Cursors.Hand)
            Cursor = Cursors.Hand;
        else if (annot == null && Cursor == Cursors.Hand)
            Cursor = DefaultCursor;
    }

    private PdfTypeDictionary GetAnnotUnderMouse(PointF mousePt)
    {
        if (!CurrentPage.Dictionary.ContainsKey("Annots"))
            return null;

        var annots = As<PdfTypeArray>(CurrentPage.Dictionary["Annots"]);
        if (annots == null)
            return null;

        foreach (var ann in annots)
        {
            var dict = As<PdfTypeDictionary>(ann);
            if (dict == null)
                continue;
            var name = As<PdfTypeName>(dict["Subtype"]);
            if (name == null || name.Value != "Text")
                continue;
            var bbox = GetAnnotRect(dict);
            if (bbox.Contains(mousePt))
                return dict;
        }

        return null;
    }

    RectangleF GetAnnotRect(PdfTypeDictionary annot)
    {
        var rect = As<PdfTypeArray>(annot["Rect"]);
        if (rect == null || rect.Count != 4)
            return default(RectangleF);

        float l = As<PdfTypeNumber>(rect[0]).FloatValue;
        float b = As<PdfTypeNumber>(rect[1]).FloatValue;
        float r = As<PdfTypeNumber>(rect[2]).FloatValue;
        float t = As<PdfTypeNumber>(rect[3]).FloatValue;
        RectangleF bbox = new RectangleF(
            Math.Min(l, r),
            Math.Min(t, b),
            Math.Max(l, r) - Math.Min(l, r),
            Math.Max(t, b) - Math.Min(t, b));

        return bbox;
    }

    private void SetAnnotRect(PdfTypeDictionary annot, RectangleF rect)
    {
        var array = PdfTypeArray.Create();
        array.Add(PdfTypeNumber.Create(rect.Left));
        array.Add(PdfTypeNumber.Create(rect.Bottom));
        array.Add(PdfTypeNumber.Create(rect.Right));
        array.Add(PdfTypeNumber.Create(rect.Top));

        annot["Rect"] = array;
    }

    public static T As<T>(PdfTypeBase pdfType) where T : PdfTypeBase
    {
        if (typeof(T) == pdfType.GetType())
            return (T)pdfType;
        else if (pdfType is PdfTypeIndirect)
        {
            PdfTypeBase obj = (pdfType as PdfTypeIndirect).Direct;
            return As<T>(obj);
        }
        return null;
    }
}

Edited by user Monday, September 4, 2017 7:47:49 PM(UTC)  | Reason: Not specified

Guest  
#3 Posted : Tuesday, September 5, 2017 9:11:06 AM(UTC)
Quote
Guest

Rank: Guest

Groups: Guests
Joined: 1/5/2016(UTC)
Posts: 154

Was thanked: 4 time(s) in 4 post(s)
Hello,

With your example I could create an working Viewer.

I am very satisfied with your support.

Paul Rayman  
#4 Posted : Wednesday, January 13, 2021 8:28:31 PM(UTC)
Quote
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 963

Thanks: 3 times
Was thanked: 115 time(s) in 112 post(s)
The API to access/create/modify annotations was released in v 4.0.2704
Here is documentation
https://pdfium.patagames...ml/WorkingSDK_Annots.htm
Quick Reply Show Quick Reply
Users browsing this topic
New Topic Post Reply
Forum Jump  
You can post new topics in this forum.
You can reply to topics in this forum.
You can delete your posts in this forum.
You can edit your posts in this forum.
You cannot create polls in this forum.
You can vote in polls in this forum.