Highlight multiple sentences at one go

Hi,

We are using 30 days trial licence of Aspose java.
I have used find & highlight functionality .It works fine for one sentence.

Pattern regex = Pattern.compile(“the precise answer of the question articulated in natural”, Pattern.CASE_INSENSITIVE);

Now I have 2 sentences which I want to highlight.
Can I highlight 2 sentences at one go?

Pattern regex = Pattern.compile(“the precise answer of the question articulated in natural|We have limited this work to Hindi and Marathi language.”, Pattern.CASE_INSENSITIVE);



My Code :

package test;

import java.awt.Color;
import java.io.FileInputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.regex.Pattern;

import com.aspose.words.Document;
import com.aspose.words.IReplacingCallback;
import com.aspose.words.License;
import com.aspose.words.Node;
import com.aspose.words.NodeType;
import com.aspose.words.ReplaceAction;
import com.aspose.words.ReplacingArgs;
import com.aspose.words.Run;


public class FindAndHighlight
{
public static void main(String[] args) throws Exception
{
FileInputStream fstream = null;
try
{
//Create a stream object containing the license file
fstream=new FileInputStream("/home/user/Aspose.Total.Java.lic");

//Instantiate the License class
License license=new License();

//Set the license through the stream object
license.setLicense(fstream);
}
catch(Exception ex)
{
//Printing the exception, if it occurs
System.out.println(ex.toString());
}
finally
{
//Closing the stream finally
if(fstream != null)
fstream.close();
}
// Sample infrastructure.
URI exeDir = FindAndHighlight.class.getResource("").toURI();
//String dataDir = new File(exeDir.resolve("/home/MultiFiles")) + File.separator;
// String str = args[0];
String inputstr = args[1];
String outputstr = args[2];
Document doc = new Document(inputstr);

// We want the “your document” phrase to be highlighted.
Pattern regex = Pattern.compile(“the precise answer of the question articulated in natural|We have limited this work to Hindi and Marathi language.”, Pattern.CASE_INSENSITIVE);
// Generally it is recommend if you are modifying the document in a custom replacement evaluator
// then you should use backward replacement by specifying false value to the third parameter of the replace method.
doc.getRange().replace(regex, new ReplaceEvaluatorFindAndHighlight(), false);

// Save the output document.
doc.save(outputstr);
}
}

class ReplaceEvaluatorFindAndHighlight 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);
}

// Now highlight all runs in the sequence.
for (Run run : (Iterable) runs)
run.getFont().setHighlightColor(Color.YELLOW);

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

Hi,

Please reply me…!

Hi Crimson,


Thanks for your inquiry. Unfortunately, currently, you cannot use special character in replacement and captured strings. This is mentioned in the documentation: “An exception is thrown if a captured or replacement string contain one or more special characters: paragraph break, cell break, section break, field start, field separator, field end, inline picture, drawing object, footnote.”

In future we will consider improving Replace method in order to support special characters. Your request has been linked to the appropriate issue (i.e. WORDSNET-1252) in our bug tracking system and you will be notified as soon as it is resolved. Sorry for the inconvenience.

The issues you have found earlier (filed as WORDSNET-1252) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.