Replace text with image one by one (Like merging)

Hi,

I have a requirement to replace portions of text with different images. Our end user maintains a word template which contains several img* tags where * representing a number. A given template may contains upto 100 tags with each identifying an individual image to be replaced with. The current Range.replace globally replaces the matching ones, but in my case I have to first get all the image tags and then for each, get the image id placed in between and then do the actual replacement. Could you give some direction on how to achieve this?

Regards
Sateesh

Hi

Thanks for your request. You can achieve this using ReplaceEvaluator. Please see the following link for more information:
https://reference.aspose.com/words/java/com.aspose.words/range#replace(java.util.regex.Pattern,java.lang.String,com.aspose.words.FindReplaceOptions)
Code will look like the following:

// Create regex
Pattern regex = Pattern.compile("img(\\d+)");
// Open document
Document doc = new Document("C:\\Temp\\in.doc");
// find placeholders
doc.getRange().replace(regex, new ReplaceEvaluatorReplaceWithImage(), true);
// Save document
doc.save("C:\\Temp\\out.doc");

===============================================================

public class ReplaceEvaluatorReplaceWithImage implements ReplaceEvaluator
{
    public synchronized int replace(Object object, ReplaceEvaluatorArgs e) throws Exception
    {
        // Get ID of image
        String imgID = e.getMatch().group(1);
        // Create DocumentBuilder and move it to matched node
        DocumentBuilder builder = new DocumentBuilder(e.getMatchNode().getDocument());
        builder.moveTo(e.getMatchNode());
        // Here you will insert your image.
        builder.insertImage("C:\\Temp\\test.jpg");
        System.out.println(imgID);
        return ReplaceAction.REPLACE;
    }
}

Hope this helps.
Best regards.

thnks thnks thnks

one million times.

it also worth for me…

Hi

The solution worked great. However I have a follow-up question. How do I know which instance of img(\d+) got replaced and which one not? The method doc.getRange().replace only returns how many number of elements got replaced and replace method in ReplaceEvaluator returns ReplaceAction.REPLACE.

As always, would appreciate your solution.

Regards
Sateesh

Hi

Thanks for your inquiry. You can add your own counter to ReplaceEvaluator. For example, you can try using the following code to collect IDs of images that were replaced.

// Create regex
Pattern regex = Pattern.compile("img(\\d+)");
// Open document
Document doc = new Document("C:\\Temp\\in.doc");
// Create evaluator
ReplaceEvaluatorReplaceWithImage evaluator = new ReplaceEvaluatorReplaceWithImage();
// find placeholders
doc.getRange().replace(regex, evaluator, true);
// get collection of replaced images IDs
for(String id : evaluator.getImageIds())
System.out.println(id);
// Save document
doc.save("C:\\Temp\\out.doc");

================================================

public class ReplaceEvaluatorReplaceWithImage implements ReplaceEvaluator
{
    public synchronized int replace(Object object, ReplaceEvaluatorArgs e) throws Exception
    {
        // Get ID of image
        String imgID = e.getMatch().group(1);
        // Create DocumentBuilder and move it to matched node
        DocumentBuilder builder = new DocumentBuilder(e.getMatchNode().getDocument());
        builder.moveTo(e.getMatchNode());
        // Here you will insert your image.
        builder.insertImage("C:\\Temp\\test.jpg");
        // Add ID to collection
        mImageIds.add(imgID);
        return ReplaceAction.REPLACE;
    }
    public List<String> getImageIds()
    {
        return mImageIds;
    }
    private List<String> mImageIds = new ArrayList<String>();
}

Hope this helps.
Best regards.

Thank you. Much appreciated.

Regards
Sateesh

Hi,

Pattern regex = Pattern.compile(“img(\d+)”);

Can you tell me where the Pattern class is defined as I cannot seem to locate this in the Aspose.Words model.

Also it seems that the ReplaceEvaluator is no longer available.

What I am tryign to do is take a Word document containing repeating text elements of %%Signature%% and replace them with a signature from a named file path.

Regards,

Andrew

Hi Andrew,

Thanks for your inquiry.

*Andrew Morrell:

Can you tell me where the Pattern class is defined as I cannot seem to locate this in the Aspose.Words model.*

Aspose.Words API do not have the Pattern class. It is java.util.regex.Pattern. Please see the detail from here:
http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

*Andrew Morrell:

Also it seems that the ReplaceEvaluator is no longer available.*

Yes, the latest version of Aspose.Words does not contain this interface. Please use the latest version of Aspose.Words for Java 13.10.0 and implement the IReplacingCallback interface in your code.

*Andrew Morrell:

What I am tryign to do is take a Word document containing repeating text elements of %%Signature%% and replace them with a signature from a named file path.*

Yes, you can achieve your requirement by implementing IReplacingCallback interface. Please use the same approach shared at following documentation link to achieve your requirements.
https://docs.aspose.com/words/java/find-and-replace/

Please
check the highlighted section of following code example (ReplaceEvaluatorTest class) and read
following documentation link for your kind reference.
https://docs.aspose.com/words/java/find-and-replace/

Hope
this helps
you. If you still face any issue, please share your input and expected
output document here for our reference. We will then provide you more
information on this along with code.

Document doc = new Document(MyDir + "TestFile.doc");
Pattern regex = Pattern.compile("%%Signature%%", Pattern.CASE_INSENSITIVE);
doc.getRange().replace(regex, new ReplaceEvaluatorTest(), true);
// Save the output document.
doc.save(MyDir + "TestFile Out.doc");
class ReplaceEvaluatorTest implements IReplacingCallback
{
    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();
        // 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.
        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);
        }
        DocumentBuilder builder = new DocumentBuilder((Document)currentNode.getDocument());
        builder.moveTo((Run)runs.get(0));
        builder.insertImage(MyDir + "Aspose Logo.png");
        // Remove runs
        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;
    }
    /**
     * 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;
    }
}

Ok but I am not on a Java platform so how do I do this in .Net with Apsoe.Words for .Net?

Hi Andrew,

Thanks for your inquiry. Please check the following .NET documentation links.

https://reference.aspose.com/words/net/aspose.words.replacing/ireplacingcallback/
https://docs.aspose.com/words/java/find-and-replace/
https://docs.aspose.com/words/java/find-and-replace/

Please use the following code snippet to achieve your requirement. Hope this helps you.

Document doc = new Document(MyDir + "in.docx");
// We want the "your document" phrase to be highlighted.
Regex regex = new Regex("%%Signature%%", RegexOptions.IgnoreCase);
doc.Range.Replace(regex, new ReplaceEvaluatorFindAndHighlight(), true);
// Save the output document.
doc.Save(MyDir + "Out.docx");
private class ReplaceEvaluatorTest : IReplacingCallback
{
    /// 
    /// This is called during a replace operation each time a match is found.
    /// This method appends a number to the match string and returns it as a replacement string.
    /// 
    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);
        // 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.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[runs.Count - 1]);
        builder.InsertImage("Aspose Logo.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;
    }
}