We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Find Text

Good Morning,

i have a question concerning Finding of Text within a Document for further Processing. I looked at the examples in the Docs. There the main purpose is, to replace text based on a Pattern.
What I need to do is, that i want to Search the Document based on a Pattern and at the specific point this pattern occurs, i need to do further processing, for example add a Picture at the place of a predefined Placeholder. My first implementation was, to simply loop the whole Document.

Is there a more comfortable Way to do this provided through the APIs?

best regards and Thanks
Rob

I just noticed, that the Custom ReplacingCallback is ment to fit to the problem i described. Somehow i overread this possibility at a first glance…


Hi Robert,


Thanks for your inquiry. Yes, please implement IReplacingCallback interface to achieve this. In case you have further inquires or need any help, please let us know.

Best regards,

Thanks Awais for your reply,

i tried the IReplacingCallback and played a little bit around. I now have a Question on this…
If i use a regular expression which i search for:
Pattern.compile("\<(NAME|AGE)>([.^]*.)");

I search for e.g. My Name. In a Document. I now sometimes get not the match, as it seems to be splitted in multiple runs in the Document. Is there a Chance to set as option to search over multiple Nodes, like i would use the Find/Replace Function in Word itself?

best regards and thanks
Rob

Hi Rob,


Thanks for your inquiry. Please try executing the following code:
static class ReplaceEvaluatorFindAndReplace implements IReplacingCallback {
<font color="RED"><b>public</b></font> <font color="RED"><b>int</b></font> replacing<font color="BLUE"><b>(</b></font>ReplacingArgs e<font color="BLUE"><b>)</b></font> <font color="RED"><b>throws</b></font> Exception <font color="BLUE"><b>{</b></font>
    Node currentNode <font color="BLUE">=</font> e<font color="BLUE"><b>.</b></font>getMatchNode<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

    <font color="GREEN"><i>// 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());

    <font color="GREEN"><i>// This array is used to store all nodes of the match for further removing.

ArrayList runs = new ArrayList();

    <font color="GREEN"><i>// Find all runs that contain parts of the match string.

int remainingLength = e.getMatch().group(0).length();
while (
(remainingLength > 0) &&
(currentNode != null) &&
(currentNode.getText().length() <= remainingLength)) {
runs.add(currentNode);
remainingLength = remainingLength - currentNode.getText().length();

        <font color="GREEN"><i>// 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));
}

    <font color="GREEN"><i>// 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 <font color="BLUE">=</font> <font color="RED"><b>new</b></font> DocumentBuilder<font color="BLUE"><b>(</b></font><font color="BLUE"><b>(</b></font>Document<font color="BLUE"><b>)</b></font> e<font color="BLUE"><b>.</b></font>getMatchNode<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>getDocument<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
    builder<font color="BLUE"><b>.</b></font>moveTo<font color="BLUE"><b>(</b></font><font color="BLUE"><b>(</b></font>Run<font color="BLUE"><b>)</b></font> runs<font color="BLUE"><b>.</b></font>get<font color="BLUE"><b>(</b></font>runs<font color="BLUE"><b>.</b></font>size<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font> <font color="BLUE">-</font> <font color="BROWN">1</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
    builder<font color="BLUE"><b>.</b></font>write<font color="BLUE"><b>(</b></font><font color="PURPLE">"new value"</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

    <font color="GREEN"><i>//Now remove all runs in the sequence.

for (Run run : (Iterable<Run>) runs)
run.remove();

    <font color="RED"><b>return</b></font> ReplaceAction<font color="BLUE"><b>.</b></font>SKIP<font color="BLUE"><b>;</b></font>
<font color="BLUE"><b>}</b></font>

}

private static 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, position));
run.getParentNode().insertAfter(afterRun, run);
return afterRun;
}

I hope, this helps.

Best regards,