GemBox.Document : DOCX Template+ Json = MailMerge

Hey. May I ask you how to merge DOCX Template with Json file and save the result in PDF file.

I have the next:

Thanks

Hi Dekan,

Try this, here is the “input.docx” file:

Here is the sample code:

static void Main()
{
    string json = """
    {
        "SomeField": "Some value",
        "Table1": [
        {
            "Name": "Name 1",
            "Description": "Description 1"
        },
        {
            "Name": "Name 2",
            "Description": "Description 2"
        }
        ],
        "Table2": [
        {
            "Name": "Name 3",
            "Description": "Description 3"
        },
        {
            "Name": "Name 4",
            "Description": "Description 4"
        }
        ]
    }
    """;

    var jsonOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true, Converters = { new JsonObjectConverter() } };
    var jsonSource = JsonSerializer.Deserialize<Dictionary<string, object>>(json, jsonOptions);

    var document = DocumentModel.Load("input.docx");
    document.MailMerge.Execute(jsonSource);
    document.Save("output.docx");
}

public sealed class JsonObjectConverter : JsonConverter<object>
{
    public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        // The objects ({}) are mapped as Dictionary<string, object>
        // and arrays ([]) are mapped as List<Dictionary<string, object>>.
        return reader.TokenType switch
        {
            JsonTokenType.StartObject => JsonSerializer.Deserialize<Dictionary<string, object>>(ref reader, options),
            JsonTokenType.StartArray => JsonSerializer.Deserialize<List<Dictionary<string, object>>>(ref reader, options),
            JsonTokenType.String => reader.GetString(),
            JsonTokenType.Number => reader.GetDouble(),
            JsonTokenType.True or JsonTokenType.False => reader.GetBoolean(),
            _ => JsonSerializer.Deserialize<object>(ref reader, options)
        };
    }

    public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
        => JsonSerializer.Serialize(writer, value, value.GetType(), options);
}

And here is the “output.docx” file:

I hope this helps.

Regards,
Mario

Actually, I can’t launch it on my side…

The beginning of the JSON was missing, the ‘{’ start character.
Instead of this:

    string json = """
        "SomeField": "Some value",

Use this:

    string json = """
    {
        "SomeField": "Some value",

Excellent. Mario, the last question. Could you send me “input.docx” by email: webdekan{AT}gmail.com? Because I am trying to create RangeStart /End in MS Word, but can’t do that.

You just need to create a “normal” MERGERANGE and name it as “RangeStart:Table1”.

You can read more about merge ranges on:

From that linked example, you can download the input “MergeRanges.docx” file, which contains “RangeStart:Items” and “RangeEnd:Items” fields. Here is a direct download location for that file:
https://www.gemboxsoftware.com/document/examples/903/resources/MergeRanges.docx

Regards,
Mario

Thank you. Now, It works well