Dear Team,
We are evaluating aspose.words for some of our business requirements.One of which is as below.We would like to find the content based on a regular expression and replace it with a new text or an image.Below are business cases,
I am assuming #!# as the delimiter.
Case1 : Lets assume that the document has a delimited text as below.#!##!#.And we should place a text called “New Content” between the delimiter. i.e the end result would be as below.
#!#New Content#!#
Here #!# is the delimiter.i.e, We should be able to search for an expression of kind which begins with #!# and ends with #!# and no matter what it has in the middle.Regular expression would be #!|#*#!#.
Case2:Just like we inserted a text in the previous case, we should able to place an image file between the delimiters i. e.#!# Image file #!#
Case3: Now that we have placed either a new text or an image ,we should be able to again search for those delimiters and replace them repeatedly with the text or an image or in a mixed combination.
i.e Considering the previous example : #!#New Content #!# is the new text now.
Now I should replace the content between #!# and #!# with a new content say “Replaced Content”.So end result would be #!#Replaced Content#!#.
Also, Now that I want to replace an image which was previously placed between the delimiters, I should be able to place a new image.
The whole idea behind the above cases is that we should be able to locate a kind of place holders in the word document by using delimiters having special characters and the replace the content between the delimiters with the new content or an image repeatedly.
Requirements as simple steps
1)
Initially : #!##!#
Next : #!#New Content#!#
Then:#!#Replaced Content#!#
2)
Initially:#!##!#
Next:#!#Image file#!#
Then : #!#Replaced Image File#!#
We are evaluating aspose.words for java for the above requirements.
Could you please assist in this regard?
regards
Pavan
Hi Pavan,
Thanks for your inquiry.
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 (ReplaceEvaluatorFindAndHighlightTest class) and read
following documentation link for your kind reference.
https://docs.aspose.com/words/java/find-and-replace/
If you want to insert image instead of text, please use DocumentBuilder.insertImage method at highlighted section below. 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.
class ReplaceEvaluatorFindAndHighlightTest implements IReplacingCallback
{
/**
* This method is called by the Aspose.Words find and replace engine for each match.
* This method highlights the match string, even if it spans multiple runs.
*/
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 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);
}
DocumentBuilder builder = new DocumentBuilder((Document)currentNode.getDocument());
builder.moveTo((Run)runs.get(0));
builder.write("New Text");
// 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;
}
}
Document doc = new Document(MyDir + "TestFile.doc");
// We want the "your document" phrase to be highlighted.
Pattern regex = Pattern.compile("your document", Pattern.CASE_INSENSITIVE);
doc.getRange().replace(regex, new ReplaceEvaluatorFindAndHighlightTest(), true);
// Save the output document.
doc.save(MyDir + "TestFile Out.doc");