Replace placeholder with image in docx

Our requirement is like we have to insert the image file with hyperlink in the docx file in the specific “place holder”. This placeholder can be in any document page#.
Could you please provide the sample code (with explanation) for getting placeholder and replacing it with image.

Note:
On click of image it will redirect the user to the specific url location in the new browser.

Hi Laurie,

Thanks for your inquiry. Could you please attach your input and expected output Word documents here for testing? We will investigate the issue on our side and provide you more information.

We have to insert image in the docx placeholder on fly (with image and hyperlink) which is inserted from GUI.
If image/url is not inserted from GUI then we have to remove the placeholder else replace placeholder with image and hyperlink.
Please find the attachment for expected input and output docx file.

Hi Laurie,

Thanks for sharing the document. Please use following code example to achieve your requirements. Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document(MyDir + "Input.docx");
Pattern regex = Pattern.compile("\\$\\{insertVideo\\}", Pattern.CASE_INSENSITIVE);
FindandInsertHyperlinkImage obj = new FindandInsertHyperlinkImage("http://www.aspose.com/", MyDir +"in.png");
doc.getRange().replace(regex, obj, false);
doc.save(MyDir + "Out.docx");
class FindandInsertHyperlinkImage implements IReplacingCallback
{
    String hyperlink;
    String image;
    DocumentBuilder builder;
    FindandInsertHyperlinkImage(String link, String imagepath)
    {
        hyperlink = link;
        image = imagepath;
    }
    /**
     * This method is called by the Aspose.Words find and replace engine for each match.
     */
    public int replacing(ReplacingArgs e) throws Exception
    {
        // This is a Run node that contains either the beginning or the complete match.
        Node currentNode = e.getMatchNode();
        if(builder == null)
            builder = new DocumentBuilder((Document)currentNode.getDocument());
        // 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 highlighting.
        ArrayList runs = new ArrayList();
        // 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(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.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(currentNode);
        }
        builder.moveTo((Run)runs.get(0));
        Shape shape = builder.insertImage(image);
        shape.setHRef(hyperlink);
        for (Run run : (Iterable) runs)
            run.remove();
        // Signal to the replace engine to do nothing because we have already done all what we wanted.
        return ReplaceAction.SKIP;
    }
    private Boolean isLink(Run run, String text) throws Exception
    {
        for(Field field : run.getParentParagraph().getRange().getFields())
        {
            if(field.getType() == FieldType.FIELD_HYPERLINK
                    &&field.getResult().trim().equals(text))
            {
                return true;
            }
        }
        return false;
    }
    /**
     * Splits text of the specified run into two runs.
     * Inserts the new run just after the specified run.
     */
    private Run splitRun(Run run, int position) throws Exception
    {
        Run afterRun = (Run)run.deepClone(true);
        afterRun.setText(run.getText().substring(position));
        run.setText(run.getText().substring((0), (0) + (position)));
        run.getParentNode().insertAfter(afterRun, run);
        return afterRun;
    }
}