Scroll to an Annotation’s Position
17 Jul 20261 minute to read
Use the Blazor SfPdfViewer component to scroll to the location of an annotation in a loaded PDF by calling the GoToBookmarkAsync() method, inherited from PdfViewerBase, with the target page number and vertical offset (from the top of the page).
The following example shows how to scroll to an annotation.
@using Syncfusion.Blazor.SfPdfViewer
<button @onclick="ScrollToAnnotation">Scroll to Annotation</button>
<SfPdfViewer2 @ref="PdfViewer" DocumentPath="@DocumentPath">
<PdfViewerEvents DocumentLoaded="DocumentLoad"></PdfViewerEvents>
</SfPdfViewer2>
@code {
private SfPdfViewer2 PdfViewer;
private string DocumentPath { get; set; } = "wwwroot/data/PDF Succinctly.pdf";
private Dictionary<int, System.Drawing.SizeF> PageSize { get; set; }
private void DocumentLoad(LoadEventArgs args)
{
PageSize = args.PageData.PageSizes;
}
private async Task ScrollToAnnotation()
{
var annotationCollection = await PdfViewer.GetAnnotationsAsync();
var pageNumber = annotationCollection[0].PageNumber;
// Bound.Top is measured from the top of the page in PDF coordinates; subtract from the page height to convert to the viewer's vertical offset.
var Y = annotationCollection[0].Bound.Top;
await PdfViewer.GoToBookmarkAsync(pageNumber, (PageSize[pageNumber].Height - Y));
}
}