How to merge the merge fields and image fields in HTML document in java using aspose?

how to merge the merge fields and image fields in HTML document in java using aspose?
I need complete working code using java.


Thanks for your request. As Adam mentioned, you can use the same technique as described here to preserve merge fields in HTML:
I translated the code to Java for you:

public void Test001() throws Exception
    // Open the source document.
    Document doc = new Document("C:\\Temp\\in.doc");
    // Convert document to HTML."C:\\temp\\out.html");
    // Open HTML document, and replace placeholders with mergefields.
    Document doc1 = new Document("C:\\Temp\\out.html");
    doc1.getRange().replace(Pattern.compile("½(\\S+)╗"), new ReplaceEvaluatorinsertMergeField(), false);
    // Execute mail merge (just for testing)
    // doc1.getMailMerge().execute(doc1.getMailMerge().getFieldNames(), doc1.getMailMerge().getFieldNames());
    // Save output document"C:\\Temp\\out.doc");
private static class ReplaceEvaluatorinsertMergeField implements ReplaceEvaluator
    /// This method is called by the Aspose.Words find and replace engine for each match.
    /// This method replaces the match string with mergield.
    public int replace(Object sender, ReplaceEvaluatorArgs e) throws Exception
        // 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 removing.
        ArrayList runs = new ArrayList();
        // Find all runs that contain parts of the match string.
        int remainingLength = e.getMatch().end() - e.getMatch().start();
        while ((remainingLength> 0) &&
            (currentNode != null) &&
            (currentNode.getText().length() <= remainingLength))
            runs.add((Run) 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((Run) currentNode);
        // Create Document Buidler aond insert MergeField
        DocumentBuilder builder = new DocumentBuilder((Document) e.getMatchNode().getDocument());
        builder.moveTo((Run) runs.get(runs.size() - 1));
        String fieldName = e.getMatch().group(1);
        builder.insertField(String.format("MERGEFIELD %s", fieldName), String.format("½%s╗", fieldName));
        // Now remove all runs in the sequence.
        for (Run run: runs)
        // Signal to the replace engine to do nothing because we have already done all what we wanted.
        return ReplaceAction.SKIP;
    /// 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);
        run.setText(run.getText().substring(0, position));
        run.getParentNode().insertAfter(afterRun, run);
        return afterRun;

Hope this helps.
Best regards,

Just a quick note, in the most recent versions of Aspose.Words this functionality can be achieved directly by using the new mustache template syntax which allows mail merging from plain text field markers.

Please see the following blog post for further information