How to Get PDF document data in Blazor PDF Viewer

14 Aug 20261 minute to read

Use the GetDocumentAsync method of the SfPdfViewer component to retrieve the currently loaded PDF document as a byte array (Task<byte[]>), including user edits, annotations, and form field data. The byte array can be reloaded into the viewer using LoadAsync, which accepts a data URI/base64 string and an optional document password.

Retrieve and reload the document

The following example retrieves the current document data and then reloads the same document into the viewer.

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

<SfButton @onclick="retrieve">Retrieve Document</SfButton>

<SfButton @onclick="load">Reload Retrieved Document</SfButton>

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

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

    private byte[]? save;

    private async Task retrieve()
    {
        //Gets the loaded PDF document
        save = await viewer.GetDocumentAsync();
    }

    private async Task load()
    {
        //Converts the byte array into base64 string.
        string base64String = Convert.ToBase64String(save);
        //Loads the PDF document from the specified base64 string. The second parameter is the document password (null for unprotected PDFs).
        await viewer.LoadAsync("data:application/pdf;base64," + base64String, null);
    }
}

View sample in GitHub.

See also