Insert page number and navigate to page in Blazor Document Editor

2 Aug 20232 minutes to read

You can insert page number and navigate to specific page in Blazor Document Editor component by following ways.

Insert page number

You can use InsertFieldAsync API in Editor module to insert the Page number in current position. By default, Page number will insert in Arabic number style.

NOTE

Currently, Document Editor have options to insert page number at current cursor position.

The following example code illustrates how to insert page number in header.

@using Syncfusion.Blazor.DocumentEditor

<SfDocumentEditorContainer @ref="container" EnableToolbar=true>
    <DocumentEditorContainerEvents Created="OnCreated"></DocumentEditorContainerEvents>
</SfDocumentEditorContainer>

@code {
    SfDocumentEditorContainer container;

    public async void OnCreated(object args)
    {
        // To insert text in cursor position
        await container.DocumentEditor.Editor.InsertTextAsync("Document editor");
        // To move the selection to header
        await container.DocumentEditor.Selection.GoToHeaderAsync();
        // Insert page number in the current cursor position
        await container.DocumentEditor.Editor.InsertFieldAsync("PAGE \\* MERGEFORMAT", "1");
    }
}

Get page count

You can use GetPageCountAsync API to gets the total number of pages in Document.

The following example code illustrates how to get the number of pages in Document.

@using Syncfusion.Blazor.DocumentEditor

<SfDocumentEditorContainer @ref="container" EnableToolbar=true>
    <DocumentEditorContainerEvents Created="OnCreated"></DocumentEditorContainerEvents>
</SfDocumentEditorContainer>

@code {
    SfDocumentEditorContainer container;

    public async void OnCreated(object args)
    {
        // To insert text in cursor position
        await container.DocumentEditor.Editor.InsertTextAsync("Document editor");
        // To get the total number of pages
        Task<int> pageCount = container.DocumentEditor.GetPageCountAsync();
    }
}

You can use GoToPageAsync API in Selection module to move selection to the start of the specified page number.

The following example code illustrates how to move selection to specific page.

@using Syncfusion.Blazor.DocumentEditor

<SfDocumentEditorContainer @ref="container" EnableToolbar=true>
    <DocumentEditorContainerEvents Created="OnCreated"></DocumentEditorContainerEvents>
</SfDocumentEditorContainer>

@code {
    SfDocumentEditorContainer container;

    public async void OnCreated(object args)
    {
        // To move selection to page number 2
        await container.DocumentEditor.Selection.GoToPageAsync(2);
    }
}