Stamp Annotations in .NET MAUI PDF Viewer (SfPdfViewer)

12 Jul 20247 minutes to read

The stamp annotations feature of SfPdfViewer allows you to add, remove and modify stamps or custom images in the PDF document. This section will go through the various types and functions available in PDF Viewer for working with stamp annotations.

Types of stamps

The following stamp annotation types are now available in the PDF Viewer.

  1. Standard (or built-in) stamps.
  2. Custom (or image) stamps.

Add stamps to a PDF document

This section will go through how to add stamp annotations to a PDF page using toolbar as well as programmatically.

Add standard stamps using toolbar

On the built-in toolbar, a tool for standard stamps is available that contains 18 standard stamps, which are most commonly used in documents. Using that, you can choose your standard stamp and add it to the tapped position.

The following example explains how to choose a standard stamp and add it to the first page of a PDF document using the built-in toolbar on the mobile.

Mobile Standard Stamp

Add custom stamps using toolbar

On the built-in toolbar, you can create your own custom stamp using the built-in dialog box and add it to a PDF document.

The following example explains how to create a custom stamp using the built-in dialog box and add it to a PDF document on the desktop.

Desktop Custom Stamp

The following example explains how to create a custom stamp using the built-in dialog box and add it to a PDF document on the mobile.

Mobile Custom Stamp

Add standard stamps without using toolbar

There are 18 standard stamp types are available in the SfPdfViewer that are most commonly used in documents. The appropriate standard stamp type can be selected from the StampType enumeration.

The following example explains how to create a Approved standard stamp and add it to the first page of a PDF document using the AddAnnotation method of the SfPdfViewer.

StampAnnotation CreateApprovedStandardStamp()
{
    int pageNumber = 1;

    // Define the position to place the stamp.
    PointF position = new PointF(100, 100);

    // Create an approved standard stamp.
    StampAnnotation approvedStamp = new StampAnnotation(StampType.Approved, pageNumber, position);
    
    //return the stamp
    return approvedStamp;
}

void AddStampAnnotation()
{
    StampAnnotation stampAnnotation = CreateApprovedStandardStamp();

    // Add the stamp to the PDF document using `SfPdfViewer` instance.
    PdfViewer.AddAnnotation(stampAnnotation);
}

The following image represents the approved standard stamp appearance in the PDF document.

Standard Approved Stamp.

Add custom stamps without using toolbar

You can create a custom stamp from any images and add it to a PDF document. The following example explains how to create a custom stamp from an image in the application and add it to a PDF document using the AddAnnotation method of the SfPdfViewer.

StampAnnotation CreateCustomStamp()
{
    int pageNumber = 1;

    // Define the position and size for the stamp to be placed in the PDF page.
    RectF bounds = new RectF(50, 50, 200, 100);

    // Create image stream from the image to be used as stamp.
    Stream imageStream = this.GetType().Assembly.GetManifestResourceStream("Annotations.Assets." + "Logo.png");

    // Create a custom stamp annotation using the image steeam.
    StampAnnotation customStamp = new StampAnnotation(imageStream,pageNumber,bounds);

    // Return the stamp annotation.
    return customStamp;
}

void AddCustomStampAnnotation()
{
    StampAnnotation stampAnnotation = CreateCustomStamp();

    // Add the stamp to the PDF document using `SfPdfViewer` instance.
    PdfViewer.AddAnnotation(stampAnnotation);
}

Edit the selected stamp

You can edit the properties of the selected stamp annotation programmatically by accessing the selected annotation instance. The selected annotation instance may be obtained from the AnnotationSelected event.

The following example shows how to edit some of the properties of the selected stamp annotation. Similarly, you can modify the other properties if required.

/// <summary>
/// Edit the selected stamp annotation.
/// </summary>
/// <param name="selectedAnnotation">The selected annotation instance that may be obtained from the annotation selected event</param>
void EditSelectedStampAnnotation(Annotation selectedAnnotation)
{
    // Type cast the selected annotation as stamp annotation.
    if (selectedAnnotation is StampAnnotation stampAnnotation)
    {
        // Change the opacity to 75%.
        stampAnnotation.Opacity = 0.75f;
    }
}

Custom stamp modal view

The custom stamp modal view appears when the user wants to create a custom stamp from a typed text. The SfPdfViewer notifies when the modal view is appearing and disappearing through events. The events help you in hiding and showing elements that are part of the app UI that are not necessary as long as the modal view is visible.

Mobile:

Custom stamp modal view mobile

Desktop:

Custom stamp modal view desktop

The SfPdfViewer.CustomStampModalViewAppearing event is triggered when the modal view opens for creating a custom stamp.

PdfViewer.CustomStampModalViewAppearing += PdfViewer_CustomStampModalViewAppearing;

private void PdfViewer_CustomStampModalViewAppearing(object? sender, AnnotationModalViewAppearingEventArgs e)
{
    // Implement the logic to hide unwanted UI elements such as toolbar items add in the app UI. 
}

The Sfpdfviewer.CustomStampModalViewDisappearing event is triggered whenever the modal view for stamp annotation is closing.

pdfviewer.CustomStampModalViewDisappearing += PdfViewer_CustomStampModalViewDisappearing;

private void PdfViewer_CustomStampModalViewDisappearing(object? sender, EventArgs e)
{
    // Implement the logic to show the UI elements that were hidden from the CustomStampModalViewAppearing event handler.
}

Supressing the custom stamp modal view and implement your own UI

The SfPdfViewer allows you to supress the custom stamp modal view and use your own UI in its place. This can be achieved by setting the AnnotationModalViewAppearingEventArgs.Cancel property to true in the CustomStampModalViewAppearing event handler.

The below code snippet illustrates supressing the custom stamp modal view and using a UI implemented in the app in its place. In this illustration, when the user types a text in your own dialog and clicks the ok button, the text is converted into an image stream and a StampAnnotation instance is created. When the PDF viewer is tapped, the stamp annotation is added in the tapped position.

Stream stampImageStream;
pdfviewer.CustomStampModalViewAppearing += PdfViewer_CustomStampModalViewAppearing;
pdfViewer.Tapped += PdfViewer_Tapped;

private void PdfViewer_CustomStampModalViewAppearing(object? Sender, AnnotationModalViewAppearingEventArgs e)
{
    e.Cancel = true;
    // Implement your own UI for sticky note editor and show it.
    ShowCustomDialog();
}

Private void customDialogOkButton_Clicked(object sender, EventArgs e)
{
   //Get the typed text from the custom dialog 
   string newText = customDialog.Text;

   // Implement the logic to create an image stream instance from the typed text. 
   stampImageStream = GetStampAnnotationFromText();
}

private void PdfViewer_Tapped(object sender, GestureEventArgs e)
{
    PointF position = e.PagePosition;
    int pageNumber = e.PageNumber;
    StampAnnotation stamp = new StampAnnotation(stampImageStream, pageNumber,  new RectF(position.X, position.Y, 200, 200));
    pdfViewer.AddAnnotation(stamp);
}