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

Notification

Icon
Error

Post a reply
From:
Message:

Maximum number of characters in each post is: 32767
Bold Italic Underline   Highlight Quote Choose Language for Syntax Highlighting Insert Image Insert an existing Attachment or upload a new File... Create Link   Unordered List Ordered List   Left Justify Center Justify Right Justify   Outdent Indent   More BBCode Tags
Font Color Font Size
Security Image:
Enter The Letters From The Security Image:
  Preview Post Cancel

Last 10 Posts (In reverse order)
Paul Rayman Posted: Wednesday, January 13, 2021 5:28:31 PM(UTC)
 
The API to access/create/modify annotations was released in v 4.0.2704
Here is documentation
https://pdfium.patagames...ml/WorkingSDK_Annots.htm
Guest Posted: Tuesday, September 5, 2017 6:11:06 AM(UTC)
 
Hello,

With your example I could create an working Viewer.

I am very satisfied with your support.

Paul Rayman Posted: Monday, September 4, 2017 4:44:06 PM(UTC)
 
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;
    }
}
Guest Posted: Monday, September 4, 2017 4:36:11 PM(UTC)
 
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 97 time(s).