Replacing text with image

Hi, i would like to replace text in my document with image, i want to use same position as the text was. Is there any way to make this work? In the word document there are substitutors “<%SIGNATURE%>” and want them to be replaced with signature image…

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

Document doc = new Document("C:\\Temp\\in.docx");
    
FindReplaceOptions findReplaceOptions = new FindReplaceOptions();
findReplaceOptions.setDirection(FindReplaceDirection.BACKWARD);
findReplaceOptions.setReplacingCallback(new ReplaceWithImageCallback());
doc.getRange().replace("<%SIGNATURE%>", "C:\\Temp\\signature.png", findReplaceOptions);
        
doc.save("C:\\Temp\\out.docx");
import com.aspose.words.*;
import java.util.ArrayList;

public class ReplaceWithImageCallback implements IReplacingCallback {
    
    /**
     * This method is called by the Aspose.Words find and replace engine for each match.
     */
    @Override
    public int replacing(ReplacingArgs e) throws Exception {
        
        Document doc = (Document)e.getMatchNode().getDocument();
        
        // This is a Run node that contains either the beginning or the complete match.
        Node currentNode = e.getMatchNode();
        
        // 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.getMatchOffset() > 0)
            currentNode = splitRun((Run)currentNode, e.getMatchOffset());
        
        // This array is used to store all nodes of the match for further deleting.
        ArrayList<Run> runs = new ArrayList<Run>();
        
        // Find all runs that contain parts of the match string.
        int remainingLength = e.getMatch().group().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.getNextSibling();
            } while (currentNode != null && currentNode.getNodeType() != 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 Image.
        DocumentBuilder builder = new DocumentBuilder(doc);
        // Move builder to the first run.
        builder.moveTo(runs.get(0));
        // Insert image.
        builder.insertImage(e.getReplacement());
        
        // Delete matched runs
        for (Run run : 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.deepClone(true);
        run.getParentNode().insertAfter(afterRun, run);
        afterRun.setText(run.getText().substring(position));
        run.setText(run.getText().substring(0, position));
        return afterRun;
    }
    
    private int mBookmarkIndex = 0;
}

in.docx (12.7 KB)
signature.png (5.8 KB)
out.docx (21.0 KB)

Thanks for your response, this really helped me :slight_smile:

1 Like