# docx-editor-mail-merge

> Document editor that merges recipient data into template fields for creating personalized documents.

**Framework:** blazor  **Component:** DocxEditor  **Variant:** mail-merge

## Get this item

**If you are an agent, fetch the JSON.** Source is inlined, so one request is enough and no tooling is required:

```
GET https://ai.syncfusion.com/r/blazor/docx-editor-mail-merge.json
```

**Install Package(s)**

```bash
dotnet add package Syncfusion.Blazor.WordProcessor
```

**Notes**

- Syncfusion release: 2026 Volume 3 (v35.1.37)
- The Syncfusion package is licensed. The composition in this file is source you own and edit. See https://ai.syncfusion.com/licensing.md

## Source files

### src/components/docx-editor-mail-merge/DocxEditor.razor

```razor
@page "/document-editor/mail-merge"
@namespace blazor.Components.Pages.MailMerge.MailMerge
@using System.Text
@using System.Text.Json
@using Syncfusion.Blazor.DocumentEditor;
@using Syncfusion.Blazor.Lists;

<div class="control-section">
    <div id='documenteditor_titlebar' class="e-de-ctn-title"></div>
    <div style="display: flex; gap: 12px; padding-top: 8px;">
        <div style="width: 200px; padding: 8px; border: 1px solid #eee;">
            <h6>Insert Field</h6>
            @foreach (var field in Fields)
            {
                <div style="border: 1px solid #ddd; padding: 4px 6px; margin-bottom: 4px; cursor: pointer;"
                     @onclick="() => InsertField(field.text)">
                    @field.text
                </div>
            }
        </div>
        <div style="flex: 1;">
            <SfDocumentEditorContainer @ref="_containerRef" Height="590px">
                <DocumentEditorContainerEvents Created="OnCreated"></DocumentEditorContainerEvents>
            </SfDocumentEditorContainer>
        </div>
    </div>
</div>

@code {
    private SfDocumentEditorContainer? _containerRef;
    SfDocumentEditorContainer container;

    public List<FieldItem> Fields { get; set; } = new List<FieldItem>
    {
        new FieldItem { text = "FirstName", Category = "Recipients" },
        new FieldItem { text = "LastName",  Category = "Recipients" },
        new FieldItem { text = "Address",   Category = "Recipients" },
        new FieldItem { text = "OrderId",   Category = "Orders" },
    };

    public async Task OnCreated(object args)
    {
        if (_containerRef?.DocumentEditor != null)
        {
            string text = "Pick a field on the left to insert a merge field at the cursor position.";

            var sfdt = new
            {
                sections = new[]
                {
                    new
                    {
                        blocks = new[]
                        {
                            new
                            {
                                inlines = new[]
                                {
                                    new { text }
                                }
                            }
                        },
                        headerFooter = new { },
                        sectionFormat = new
                        {
                            pageWidth = 612,
                            pageHeight = 792,
                            leftMargin = 72,
                            rightMargin = 72,
                            topMargin = 72,
                            bottomMargin = 72
                        }
                    }
                }
            };

            string json = JsonSerializer.Serialize(sfdt);
            await _containerRef.DocumentEditor.OpenAsync(json);
            await InvokeAsync(StateHasChanged);
        }
    }

    // Inserts a MERGEFIELD for the selected item at the current selection.
    private void InsertField(string field)
    {
        if (!string.IsNullOrEmpty(field))
        {
            Console.WriteLine($"Insert merge field: {field}");
        }
    }

    public class FieldItem
    {
        public string text { get; set; } = string.Empty;
        public string Category { get; set; } = string.Empty;
    }
}

```
