Find text and replace it with image using C#

Hi All,
Please help me with the below query.

  1. I have a word doc
    2.I have an image.
    Now I need to find a word in this document say in this document I have a text “Signature” I need to insert the image above this word and replace the Signature with some Name .
    So the word "Signature should get replaced by image and Name below the image

@sangamer

In your case, you can use find and replace feature to achieve your requirement. We suggest you following solution.

  1. Implement IReplacingCallback interface.
  2. In IReplacingCallback.Replacing, move the cursor to the matched node.
  3. Insert the image using the DocumentBuilder.InsertImage.
  4. Remove the matched nodes.

Hope this helps you.

Could you please help me with the code to find the current node of a text in word document plz also the image should be inserted above the text later the text needs to be replaced with some value

C# version of teh code

@sangamer

Following code example shows how to find the text and insert the image. Hope this helps you.

 Document doc = new Document(MyDir + "input.docx");

FindReplaceOptions options = new FindReplaceOptions();
options.ReplacingCallback = new FindandInsertImage();
options.Direction = FindReplaceDirection.Backward;

Regex regex = new Regex("Insert Image", RegexOptions.IgnoreCase);
doc.Range.Replace(regex, "", options);

doc.Save(MyDir + "output.docx");

private class FindandInsertImage : IReplacingCallback
{
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
    {
        // 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);

        ArrayList runs = new ArrayList();

        // 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(currentNode);
            remainingLength = 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(currentNode);
        }

        DocumentBuilder builder = new DocumentBuilder((Document)e.MatchNode.Document);
        builder.MoveTo((Run)runs[0]);
        builder.InsertImage(MyDir + "image.png");

        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);
    afterRun.Text = run.Text.Substring(position);
    run.Text = run.Text.Substring((0), (0) + (position));
    run.ParentNode.InsertAfter(afterRun, run);
    return afterRun;
}