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).
|
|
|
|
Rank: Administration
Groups: Administrators
Joined: 1/5/2016(UTC) Posts: 961
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
|
|
|
|
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.
|
|
|
|
Rank: Administration
Groups: Administrators
Joined: 1/5/2016(UTC) Posts: 961
Thanks: 3 times Was thanked: 115 time(s) in 112 post(s)
|
|
|
|
|
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.
Important Information:
The Patagames Software Support Forum uses cookies. By continuing to browse this site, you are agreeing to our use of cookies.
More Details
Close