Underline Annotation in Blazor SfPdfViewer Component

17 Jul 202611 minutes to read

This guide explains how to enable, apply, customize, and manage Underline text markup annotations in the Syncfusion Blazor SfPdfViewer component.

Enable Underline Annotation in the Viewer

Underline is enabled by default. To use the annotation toolbar, add the SfPdfViewer component to your Blazor page:

@using Syncfusion.Blazor.SfPdfViewer

<SfPdfViewer2 DocumentPath="@DocumentPath"
              Width="100%"
              Height="100%">
</SfPdfViewer2>

@code {
    private string DocumentPath { get; set; } = "wwwroot/Data/PDF_Succinctly.pdf";
}

Disable TextMarkup Annotation

To disable all text markup annotations (including underline) so they do not appear in the toolbar or context menu, set EnableTextMarkupAnnotation="false":

@using Syncfusion.Blazor.SfPdfViewer

<SfPdfViewer2 DocumentPath="@DocumentPath"
              EnableTextMarkupAnnotation="false"
              Width="100%"
              Height="100%">
</SfPdfViewer2>

@code {
    private string DocumentPath { get; set; } = "wwwroot/Data/PDF_Succinctly.pdf";
}

Add Underline Annotation

Add Underline Annotation Using the Toolbar

  1. Click the Edit Annotation button in the SfPdfViewer toolbar. An annotation toolbar appears below the main toolbar.
  2. Select the Underline button in the annotation toolbar to enable underline mode.
  3. Select the text in the document to add the underline annotation.
    • Alternatively, select the text first and then click Underline to apply it.
    • If Pan Mode is active, the viewer automatically switches to Text Selection mode.

Underline tool

Apply Underline Annotation Using the Context Menu

  1. Select text in the document.
  2. Right-click the selected text region.
  3. Choose Underline from the context menu.

Underline Context

Enable Underline Annotation Mode Programmatically

Switch the viewer into underline mode using SetAnnotationModeAsync.

@using Syncfusion.Blazor.SfPdfViewer
@using Syncfusion.Blazor.Buttons

<SfButton OnClick="EnableUnderlineMode">Underline</SfButton>
<SfPdfViewer2 DocumentPath="@DocumentPath"
              @ref="Viewer"
              Width="100%"
              Height="100%">
</SfPdfViewer2>

@code {
    private SfPdfViewer2 Viewer;
    private string DocumentPath { get; set; } = "wwwroot/Data/PDF_Succinctly.pdf";

    private async Task EnableUnderlineMode(MouseEventArgs args)
    {
        await Viewer.SetAnnotationModeAsync(AnnotationType.Underline);
    }
}

Exit Underline Annotation Mode

Switch back to normal mode using:

private async Task DisableUnderlineMode()
{
    await Viewer.SetAnnotationModeAsync(AnnotationType.None);
}

Add Underline Annotation Programmatically

Use AddAnnotationAsync() to insert an underline at a specific location.

@using Syncfusion.Blazor.Buttons
@using Syncfusion.Blazor.SfPdfViewer

<SfButton OnClick="@AddUnderline">Add Underline</SfButton>
<SfPdfViewer2 Width="100%" Height="100%" DocumentPath="@DocumentPath" @ref="@Viewer" />

@code {
    private SfPdfViewer2 Viewer;
    private string DocumentPath { get; set; } = "wwwroot/Data/PDF_Succinctly.pdf";

    private async Task AddUnderline(MouseEventArgs args)
    {
        PdfAnnotation annotation = new PdfAnnotation
        {
            Type = AnnotationType.Underline,
            // PageNumber is 0-based in the SfPdfViewer API
            PageNumber = 0,
            Color = "#00aa00",
            Opacity = 0.9,
            Bounds = new List<Bound>
            {
                new Bound { X = 97, Y = 110, Width = 350, Height = 14 }
            }
        };
        await Viewer.AddAnnotationAsync(annotation);
    }
}

Customize Underline Annotation Appearance

Configure default underline settings such as color and opacity using UnderlineSettings.

@using Syncfusion.Blazor.SfPdfViewer

<SfPdfViewer2 @ref="@Viewer"
              DocumentPath="@DocumentPath"
              UnderlineSettings="@UnderlineSettingsModel"
              Height="100%"
              Width="100%">
</SfPdfViewer2>

@code {
    private SfPdfViewer2 Viewer;
    private string DocumentPath { get; set; } = "wwwroot/Data/PDF_Succinctly.pdf";

    private PdfViewerUnderlineSettings UnderlineSettingsModel = new PdfViewerUnderlineSettings
    {
        Color = "#00aa00",
        Opacity = 0.9
    };
}

NOTE

After changing the default color and opacity using the Edit Color and Edit Opacity tools, those values become the new defaults for subsequent annotations.

Manage Underline Annotation (Edit, Delete)

Edit Underline Annotation

Edit Underline Annotation Appearance Using the UI

Use the annotation toolbar:

  • Edit Color tool to change the underline color.

Editing text markup color in Blazor SfPdfViewer

  • Edit Opacity slider to adjust the transparency.

Editing text markup Opacity in Blazor SfPdfViewer

Edit Underline Annotation Programmatically

Modify an existing underline programmatically using EditAnnotationAsync().

@using Syncfusion.Blazor.Buttons
@using Syncfusion.Blazor.SfPdfViewer

<SfButton OnClick="@EditUnderline">Edit Underline</SfButton>
<SfPdfViewer2 Width="100%" Height="100%" DocumentPath="@DocumentPath" @ref="@Viewer" />

@code {
    private SfPdfViewer2 Viewer;
    private string DocumentPath { get; set; } = "wwwroot/Data/PDF_Succinctly.pdf";

    private async Task EditUnderline(MouseEventArgs args)
    {
        // Get the annotation collection
        List<PdfAnnotation> annotationCollection = await Viewer.GetAnnotationsAsync();
        if (annotationCollection is null || annotationCollection.Count == 0)
        {
            return;
        }
        // Select the first underline annotation on the first page
        PdfAnnotation annotation = annotationCollection
            .FirstOrDefault(a => a.Type == AnnotationType.Underline && a.PageNumber == 0)
            ?? annotationCollection[0];
        // Change the color of the underline annotation to blue
        annotation.Color = "#0000ff";
        // Change the opacity to 80% (0.8)
        annotation.Opacity = 0.8;
        await Viewer.EditAnnotationAsync(annotation);
    }
}

Delete Underline Annotation

The SfPdfViewer supports deleting existing annotations through both the UI and the API. To delete from the UI, select the underline and press Delete or use the Delete tool on the annotation toolbar.

To delete programmatically, use DeleteAnnotationAsync():

@using Syncfusion.Blazor.Buttons
@using Syncfusion.Blazor.SfPdfViewer

<SfButton OnClick="@DeleteUnderline">Delete Underline</SfButton>
<SfPdfViewer2 Width="100%" Height="100%" DocumentPath="@DocumentPath" @ref="@Viewer" />

@code {
    private SfPdfViewer2 Viewer;
    private string DocumentPath { get; set; } = "wwwroot/Data/PDF_Succinctly.pdf";

    private async Task DeleteUnderline(MouseEventArgs args)
    {
        // Get the annotation collection
        List<PdfAnnotation> annotationCollection = await Viewer.GetAnnotationsAsync();
        if (annotationCollection is null || annotationCollection.Count == 0)
        {
            return;
        }
        // Select the first underline annotation on the first page
        PdfAnnotation annotation = annotationCollection
            .FirstOrDefault(a => a.Type == AnnotationType.Underline && a.PageNumber == 0)
            ?? annotationCollection[0];
        // Delete the specified annotation
        await Viewer.DeleteAnnotationAsync(annotation);
    }
}

Add Multiple Underline Annotations with Custom Properties

To add several underlines with different colors and positions in one operation, call AddAnnotationAsync() for each PdfAnnotation. This is an extension of the Add Underline Programmatically example.

@using Syncfusion.Blazor.Buttons
@using Syncfusion.Blazor.SfPdfViewer

<SfButton OnClick="@AddMultipleUnderlines">Add Multiple Underlines</SfButton>
<SfPdfViewer2 Width="100%" Height="100%" DocumentPath="@DocumentPath" @ref="@Viewer" />

@code {
    private SfPdfViewer2 Viewer;
    private string DocumentPath { get; set; } = "wwwroot/Data/PDF_Succinctly.pdf";

    private async Task AddMultipleUnderlines(MouseEventArgs args)
    {
        // Underline 1 - Yellow on the first page (index 0)
        await Viewer.AddAnnotationAsync(new PdfAnnotation
        {
            Type = AnnotationType.Underline,
            PageNumber = 0,
            Color = "#ffff00",
            Opacity = 0.9,
            Bounds = new List<Bound>
            {
                new Bound { X = 100, Y = 150, Width = 320, Height = 14 }
            }
        });

        // Underline 2 - Red on the first page (index 0)
        await Viewer.AddAnnotationAsync(new PdfAnnotation
        {
            Type = AnnotationType.Underline,
            PageNumber = 0,
            Color = "#ff1010",
            Opacity = 0.9,
            Bounds = new List<Bound>
            {
                new Bound { X = 110, Y = 220, Width = 300, Height = 14 }
            }
        });
    }
}

Handle Underline Annotation Events

The SfPdfViewer provides annotation life-cycle events that fire when underline annotations are added, modified, selected, or removed. Subscribe to events through the PdfViewerEvents tag.

@using Syncfusion.Blazor.SfPdfViewer

<SfPdfViewer2 DocumentPath="@DocumentPath"
              Height="100%"
              Width="100%">
    <PdfViewerEvents AnnotationAdded="@OnAnnotationAdded"
                     AnnotationRemoved="@OnAnnotationRemoved"
                     AnnotationPropertiesChanged="@OnAnnotationPropertiesChanged" />
</SfPdfViewer2>

@code {
    private string DocumentPath { get; set; } = "wwwroot/Data/PDF_Succinctly.pdf";

    private async Task OnAnnotationAdded(AnnotationAddEventArgs args)
    {
        if (args.AnnotationType == AnnotationType.Underline)
        {
            // Underline was added
        }
    }

    private async Task OnAnnotationRemoved(AnnotationRemoveEventArgs args)
    {
        if (args.AnnotationType == AnnotationType.Underline)
        {
            // Underline was removed
        }
    }

    private async Task OnAnnotationPropertiesChanged(AnnotationPropertiesChangeEventArgs args)
    {
        if (args.AnnotationType == AnnotationType.Underline)
        {
            // Underline color or opacity changed
        }
    }
}

For the full list of available events and their descriptions, see Annotation Events.

Export and Import

The SfPdfViewer supports exporting and importing annotations as JSON or XFDF, allowing you to save annotations as a separate file or load existing annotations back into the viewer. For full details on supported formats and steps to export or import annotations, see Export and Import Annotations.

See also