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;
}
}