Aspose.Word - how to add checkboxes with FindReplaceOptions

I have a requirement to add checkboxes into word document and its dynamic,
can anyone share how we can do that using FindReplaceOptions.

@ishara119 You can achieve this using IReplacingCallback. For example see the following code:

Document doc = new Document(@"C:\Temp\in.docx");
FindReplaceOptions opt = new FindReplaceOptions();
opt.ReplacingCallback = new ReplaceEvaluatorFindAndReplaceWithCheckbox();
doc.Range.Replace(new Regex(@"(\[checked\])|(\[unchecked\])"), "", opt);
doc.Save(@"C:\Temp\out.docx");
internal class ReplaceEvaluatorFindAndReplaceWithCheckbox : IReplacingCallback
{
    /// <summary>
    /// This method is called by the Aspose.Words find and replace engine for each match.
    /// </summary>
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
    {
        Document doc = (Document)e.MatchNode.Document;

        // This is a Run node that contains either the beginning or the complete match.
        Node currentNode = e.MatchNode;

        // The first (and may be the only) run can contain text before the match, 
        // in this case it is necessary to split the run.
        if (e.MatchOffset > 0)
            currentNode = SplitRun((Run)currentNode, e.MatchOffset);

        // This array is used to store all nodes of the match for further deleting.
        List<Run> runs = new List<Run>();

        // Find all runs that contain parts of the match string.
        int remainingLength = e.Match.Value.Length;
        while (
            remainingLength > 0 &&
            currentNode != null &&
            currentNode.GetText().Length <= remainingLength)
        {
            runs.Add((Run)currentNode);
            remainingLength -= currentNode.GetText().Length;

            // Select the next Run node.
            // Have to loop because there could be other nodes such as BookmarkStart etc.
            do
            {
                currentNode = currentNode.NextSibling;
            } while (currentNode != null && currentNode.NodeType != NodeType.Run);
        }

        // Split the last run that contains the match if there is any text left.
        if (currentNode != null && remainingLength > 0)
        {
            SplitRun((Run)currentNode, remainingLength);
            runs.Add((Run)currentNode);
        }

        // Create DocumentBuilder to insert HTML.
        DocumentBuilder builder = new DocumentBuilder(doc);
        // Move builder to the first run.
        builder.MoveTo(runs[0]);

        // Insert a checkbox form field.
        builder.InsertCheckBox(string.Format("checlbox_{0}", mCounter++), e.Match.Value == "[checked]", 10);

        // Alternatively, you can use SDT checkbox. Comment the above line of code and uocomment the following block.
        //StructuredDocumentTag sdt = new StructuredDocumentTag(doc, SdtType.Checkbox, MarkupLevel.Inline);
        //sdt.Title = string.Format("checlbox_{0}", mCounter++);
        //sdt.Checked = e.Match.Value == "[checked]";
        //builder.InsertNode(sdt);


        // Delete matched runs
        foreach (Run run in runs)
            run.Remove();

        // Signal to the replace engine to do nothing because we have already done all what we wanted.
        return ReplaceAction.Skip;
    }

    private static Run SplitRun(Run run, int position)
    {
        Run afterRun = (Run)run.Clone(true);
        run.ParentNode.InsertAfter(afterRun, run);
        afterRun.Text = run.Text.Substring(position);
        run.Text = run.Text.Substring((0), (0) + (position));
        return afterRun;
    }

    private int mCounter = 0;
}

In the input document a simple placeholders like [checked] and [unchecked], you can use any other format of placeholders: in.docx (12.2 KB)

Please node, there are two ways to insert a checkbox into the MS Word document - using form field and using structured document tags. The first can be achieve using code like this:

builder.InsertCheckBox(string.Format("checlbox_{0}", mCounter++), e.Match.Value == "[checked]", 10);

The second using code like this:

StructuredDocumentTag sdt = new StructuredDocumentTag(doc, SdtType.Checkbox, MarkupLevel.Inline);
sdt.Title = string.Format("checlbox_{0}", mCounter++);
sdt.Checked = e.Match.Value == "[checked]";
builder.InsertNode(sdt);

See the comments in the code example. Here are output documents produced by both options:
out_formfield.docx (9.8 KB)
out_sdt.docx (9.8 KB)

1 Like

This works, Thanks a lot for the support.

1 Like