Import and Export annotations in Blazor SfPdfViewer Component

21 Jun 20232 minutes to read

The SfPdfViewer control provides the support to import and export annotations using JSON object in the PDF document.

  • Click the Add or Edit annotation button in the SfPdfViewer toolbar.

Blazor SfPdfViewer with Edit Button

  • The annotation toolbar will appear.
  • Click the Comment Panel button in the annotation toolbar.

Blazor SfPdfViewer with Comment Panel

  • The comments panel will be displayed.
  • Click the More Option button in the comment panel container.

Displaying More Option in Blazor SfPdfViewer

Importing annotation to the PDF document

  • Click the Add or Edit annotation button in the SfPdfViewer toolbar.
  • The annotation toolbar will appear.
  • Click the Comment Panel button in the annotation toolbar.
  • The comments panel will displayed.
  • Click the More Option button in the comment panel container.
  • Select the Import Annotations Option.

Importing Annotation in Blazor SfPdfViewer

  • Then the file explorer dialog will be opened. Choose the JSON file to be imported into the loaded PDF document.

Blazor PDFViewer with Imported Annotation

Importing annotation using SfPdfViewer API

You can import annotations using JSON file or JSON object in code behind like the following code snippet.

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

<SfButton OnClick="@OnImportAnnotationsClick">Import Annotation</SfButton>
<SfPdfViewer2 Width="100%" Height="100%" DocumentPath="@DocumentPath" @ref="@Viewer" />

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

    public async void OnImportAnnotationsClick(MouseEventArgs args)
    {
        //The json file has been placed inside the data folder.
        byte[] bytes = System.IO.File.ReadAllBytes("wwwroot/Data/PDF_Succinctly.json");
        await Viewer.ImportAnnotationAsync(new MemoryStream(bytes));
    }
}

View sample in GitHub.

NOTE

The JSON file for importing the annotation should be placed in the desired location and the path has to be provided correctly.

Exporting annotation from the PDF document

The SfPdfViewer control provides the support to export the annotations as JSON file and JSON object using annotation toolbar.

  • Click the Add or Edit annotation button in the SfPdfViewer toolbar.
  • The annotation toolbar will appear.
  • Click the Comment Panel button in the annotation toolbar.
  • The comments panel will be displayed.
  • Click the More Option button in the comment panel container.
  • Select the Export Annotations Option.

Exporting Annotation in Blazor SfPdfViewer

NOTE

Export annotations will be in the disabled state when the loaded PDF document does not contain any annotations.

Exporting annotation using SfPdfViewer API

You can export annotations as JSON file in code behind like the following code snippet.

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

<SfButton OnClick="@OnExportAnnotationsClick">Export Annotation</SfButton>
<SfPdfViewer2 Width="100%" Height="100%" DocumentPath="@DocumentPath" @ref="@Viewer" />

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

    public async void OnExportAnnotationsClick(MouseEventArgs args)
    {
        await Viewer.ExportAnnotationAsync();
    }
}

View sample in GitHub.

See also