Get PDF document’s data from Blazor SfPdfViewer Component

4 Jul 20241 minute to read

You can get the loaded PDF document’s data from the SfPdfViewer component using the GetDocumentAsync() method of SfPdfViewer.

The following code example shows how to get the loaded/edited document data and re-load the document.

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

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

<SfButton @onclick="load">ReloadRetrievedDocument</SfButton>

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

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

    byte[]? save;

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

    public async void load()
    {
        //Converts the byte array into base64 string.
        string base64String = Convert.ToBase64String(save);
        //Loads the PDF document from the specified base64 string.
        await viewer.LoadAsync("data:application/pdf;base64," + base64String, null);
    }
}

View sample in GitHub.

See also