Determine if annotation is in viewable area of viewer.

Questions, comments and suggestions concerning VintaSoft PDF .NET Plug-in.

Moderator: Alex

Post Reply
IntegraHarlan
Posts: 84
Joined: Fri Jan 24, 2020 3:37 am

Determine if annotation is in viewable area of viewer.

Post by IntegraHarlan »

Hi,
I want to be able to scroll to an annotation only if it is not in the viewable area of the viewer.
I might have the viewer sized so that I can only see half a page. I want to be able to automatically scroll to an annotation
that is at the bottom of the page, but not in the viewable area. If the annotation is already in the viewable area, I do not want to scroll.
I might also have a multi page document and an annotation I want to scroll to might be on the last page.
I can scroll to the page I need by determining which image the annotation is on, but if the viewer is sized smaller than the page size I want to scroll the page so the annotation is visible.

I tried using the viewer ScrollToPoint method, but it will move the page even if the annotation is in the viewable areas.

Is there a way to do this?

Thanks
Alex
Site Admin
Posts: 2305
Joined: Thu Jul 10, 2008 2:21 pm

Re: Determine if annotation is in viewable area of viewer.

Post by Alex »

Hi Harlan,

Here is example that shows how to scroll to the PDF annotation in image viewer:

Code: Select all

/// <summary>
/// Scrolls to the annotation.
/// </summary>
/// <param name="viewer">The image viewer.</param>
/// <param name="image">The image that contains PDF annotation.</param>
/// <param name="annotation">The PDF annotation.</param>
/// <exception cref="System.ArgumentNullException">Thrown if <i>viewer</i> or <i>image</i> or <i>annotation</i> is <b>null</b>.</exception>
public void ScrollToAnnotation(ImageViewer viewer, VintasoftImage image, PdfAnnotation annotation)
{
    if (viewer == null)
        throw new ArgumentNullException("viewer");
    if (image == null)
        throw new ArgumentNullException("image");
    if (annotation == null)
        throw new ArgumentNullException("annotation");


    // get image index in image viewer
    int focusedIndex = viewer.Images.IndexOf(image);
    // if image viewer does not contain image
    if (focusedIndex == -1)
        return;
    // update focused index in image viewer
    viewer.FocusedIndex = focusedIndex;


    // get PDF page
    PdfPage page = PdfDocumentController.GetPageAssociatedWithImage(image);
    // get transform from annotation space to the image space
    AffineMatrix transformFromAnnotationToImage = page.GetTransformFromPageSpaceToImageSpace(image.Resolution);

    // get annotation bounding box in image space
    RectangleF annotationBoundingBoxInImageSpace = PointFAffineTransform.TransformBoundingBox(
            PointFAffineTransform.FromMatrix(transformFromAnnotationToImage), 
            annotation.Rectangle);


    // get the image region, which is visible in image viewer
    RectangleF imageVisibleRect = viewer.ViewerState.ImageVisibleRect;
    // if annotation region is not visible in the image viewer
    if (!imageVisibleRect.Contains(annotationBoundingBoxInImageSpace.Left, annotationBoundingBoxInImageSpace.Top) ||
        !imageVisibleRect.Contains(annotationBoundingBoxInImageSpace.Right, annotationBoundingBoxInImageSpace.Bottom))
    {
        // get annotation bounding box center in image space
        PointF annotationBoundingBoxCenter = new PointF(
            annotationBoundingBoxInImageSpace.X + annotationBoundingBoxInImageSpace.Width / 2,
            annotationBoundingBoxInImageSpace.Y + annotationBoundingBoxInImageSpace.Height / 2);

        // scroll to the annotation bounding box center
        viewer.ScrollToPoint(annotationBoundingBoxCenter);
    }
}
Best regards, Alexander
Post Reply