Rank: Newbie
Groups: Registered
Joined: 4/19/2021(UTC) Posts: 4  Location: Johannesburg
|
Thanks for the reply Paul. The post you referenced is where I originally found the code I tried to replicate. Still no luck. Please follow the code below: From Viewer_MouseDown, AnnotationAtLocation is called and sets values for an annotation and accompanying PdfTextObject, if under mouse. Then Viewer_MouseMove should transform both: SetAnnotationRectangle SetTextObjectRectangle In SetTextObjectRectangle, after retrieving the Matrix, transforming it, and setting it back to the pdfTextObject, I still see no change on the Viewer. Should there be a save/refresh before it would reflect? Or is my Matrix code incorrect? Code:
private void Viewer_MouseDown(object sender, MouseEventArgs e)
{
if (Viewer.Document != null)
{
PointF pagePoint = Viewer.ClientToPage(Viewer.CurrentIndex, e.Location);
(currentAnnotation, currentAnnotationTextObject) = AnnotationAtLocation(pagePoint);
mouseDownLocation = e.Location;
}
}
private (PdfTypeDictionary, PdfTextObject) AnnotationAtLocation(PointF location)
{
if (!Viewer.CurrentPage.Dictionary.ContainsKey("Annots"))
return (null, null);
PdfTypeArray annotations = As<PdfTypeArray>(Viewer.CurrentPage.Dictionary["Annots"]);
if (annotations == null)
return (null, null);
foreach (PdfTypeBase annotation in annotations)
{
PdfTypeDictionary dictionary = As<PdfTypeDictionary>(annotation);
if (dictionary == null)
continue;
if (dictionary.TryGetValue("NM", out PdfTypeBase nameValue))
{
if ((nameValue is PdfTypeString pdfTypeString))
{
if (pdfTypeString.UnicodeString.StartsWith("DX_"))
{
RectangleF boundingBox = GetAnnotRect(dictionary);
if (boundingBox.Contains(location))
{
PdfTextObject pdfTextObjectResult = null;
if (dictionary.TryGetValue("Subtype", out PdfTypeBase pdfTypeBase))
{
if (pdfTypeBase is PdfTypeName pdfTypeName)
{
if (pdfTypeName.Value == "Link")
{
foreach (var pageObject in Viewer.CurrentPage.PageObjects)
{
if (pageObject is PdfTextObject pdfTextObject)
{
//• TODO: BETTER TEXT UNDER ANNOTATION CHECKING
if ((int)pdfTextObject.BoundingBox.left == (int)boundingBox.X)
//&&
//(int)pdfTextObject.BoundingBox.top == (int)boundingBox.Y &&
//(int)pdfTextObject.BoundingBox.right == (int)boundingBox.X + (int)boundingBox.Width &&
//(int)pdfTextObject.BoundingBox.bottom == (int)boundingBox.Y + (int)boundingBox.Height)
{
pdfTextObjectResult = pdfTextObject;
break;
}
}
}
}
}
}
return (dictionary, pdfTextObjectResult);
}
}
}
}
}
return (null, null);
}
RectangleF GetAnnotRect(PdfTypeDictionary annotation)
{
PdfTypeArray rect = As<PdfTypeArray>(annotation["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 Viewer_MouseMove(object sender, MouseEventArgs e)
{
if (currentAnnotation != null && e.Button == MouseButtons.Left)
{
Viewer.MouseMode = MouseModes.None;
ChangeMouseCursor();
RectangleF currentRectangle = GetAnnotRect(currentAnnotation);
PointF currentAnnotationLocation = Viewer.PageToClient(Viewer.CurrentIndex, new PointF(currentRectangle.X, currentRectangle.Y));
PointF pagePoint = Viewer.ClientToPage(Viewer.CurrentIndex, new Point(e.X + ((int)currentAnnotationLocation.X - mouseDownLocation.X), e.Y + ((int)currentAnnotationLocation.Y - mouseDownLocation.Y)));
RectangleF newRectangle = new RectangleF(pagePoint, currentRectangle.Size);
#region LIMIT MOVEMENT OFF PAGE
if (newRectangle.Left < 0 || newRectangle.Top < 0)
return;
if (mouseDownLocation.X < e.X && newRectangle.Left + newRectangle.Width > Viewer.Document.Pages[Viewer.CurrentIndex].Width)
return;
if (mouseDownLocation.Y < e.Y && newRectangle.Top + newRectangle.Height > Viewer.Document.Pages[Viewer.CurrentIndex].Height)
return;
#endregion
SetAnnotationRectangle(currentAnnotation, newRectangle);
if (currentAnnotationTextObject != null)
SetTextObjectRectangle(currentAnnotationTextObject, newRectangle);
mouseDownLocation = e.Location;
}
}
private void SetAnnotationRectangle(PdfTypeDictionary annotation, RectangleF rectangle)
{
PdfTypeArray array = PdfTypeArray.Create();
array.Add(PdfTypeNumber.Create(rectangle.Left));
array.Add(PdfTypeNumber.Create(rectangle.Bottom));
array.Add(PdfTypeNumber.Create(rectangle.Right));
array.Add(PdfTypeNumber.Create(rectangle.Top));
annotation["Rect"] = array;
}
private void SetTextObjectRectangle(PdfTextObject pdfTextObject, RectangleF rectangle)
{
FS_MATRIX m = pdfTextObject.Matrix;
m.Translate(m.e - rectangle.X, m.f - rectangle.Y);
pdfTextObject.Matrix = m;
}
|