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

Free Support Forum - aspose.com

Text Highlight of a String in Specific Paragraph/Table

Hi Team,

We are doing the analysis of the ASPOSE word for our need, using the trial version.

We need to highlight the difference text of Two word document.

So we like to know, how can we highlight a String in a specific table/Paragraph.

We tried with Sample code for JAVA, which will highlight text in all the pages of document.

How can we achieve our requirement.

Can you please guide us.

@madhusanmani,

Thanks for your inquiry. Could you please share your input documents along expected output document here for our reference? We will investigate how you want your final Word output be generated like. We will then provide you more information on this along with code.

Best Regards,
Tahir Manzoor

HI,

Thank for your response. i like to High-light EUR in the below line, not the other instance of EUR.

If i search for Calculation Agent : EUR, i am not able to search it using the regex the whole line.
I am attaching the document for your reference

line :- Calculation Agent : EURsample.PNG (44.3 KB)

@madhusanmani,

Thanks for sharing the detail. Please refer to the following article:
How to Find and Highlight Text

The text Calculation Agent EUR is inside two table’s cell. In your case, we suggest you please call the Range.Repalce method twice as shown below. Please get the code of ReplaceEvaluatorFindAndHighlight in above article. Hope this helps you.

Pattern regex = Pattern.compile("Calculation Agent", Pattern.CASE_INSENSITIVE);
FindReplaceOptions findReplaceOptions = new FindReplaceOptions();
findReplaceOptions.setReplacingCallback(new ReplaceEvaluatorFindAndHighlight());
doc.getRange().replace(regex, "", findReplaceOptions);

regex = Pattern.compile("EUR", Pattern.CASE_INSENSITIVE);
findReplaceOptions = new FindReplaceOptions();
findReplaceOptions.setReplacingCallback(new ReplaceEvaluatorFindAndHighlight());
doc.getRange().replace(regex, "", findReplaceOptions);

doc.save(MyDir + "17.7.docx");

We have logged a feature request as WORDSNET-15646 in our issue tracking system to search the text in a table’s row. You will be notified via this forum thread once this feature is available. We apologize for your inconvenience.

Best Regards,
Tahir Manzoor

Hi Tahir,

Thank for the help,i tried with the belwo code, but it highlighted all teh instance of the Word document not only the

Calculation Agent : EUR.

My requirement is to only highlight the above line instance of Calculation Agent and EUR"

Code:-
FindReplaceOptions options = new FindReplaceOptions();
options.ReplacingCallback = new ReplaceEvaluatorFindAndHighlight();
Pattern regex = Pattern.compile(“Calculation Agent”, Pattern.CASE_INSENSITIVE);
doc.getRange().replace(regex, “”, options);

   options = new FindReplaceOptions();
   options.ReplacingCallback = new ReplaceEvaluatorFindAndHighlight();
   regex = Pattern.compile("EUR", Pattern.CASE_INSENSITIVE);
   doc.getRange().replace(regex, "", options);
   

    // Save the output document.
    doc.save(word_text + "TestFile Out22222222.doc");<a class="attachment" href="/uploads/discourse_instance3/4002">sample22.PNG</a> (13.3 KB)

sample22.PNG (13.3 KB)

@madhusanmani

Thanks for your feedback. We will appreciate it if you please share your sample input and expected output document here. It will help us to understand your requirements exactly and address it accordingly.

Best Regards,

Hi Tilal,

I have attached the sample input and expected output.

I like to highlight text in particular in word document.Expected_output.pdf (56.2 KB)
input.pdf (56.2 KB)

H Team,

My request is similar to the below request, which is already answered by support team.

I like to know the respective sample in java to highlight the changes.

Can you please share the sample java class for the same?

@madhusanmani,

We are working over your query and will get back to you soon.

Best regards
Tahir Manzoor

@madhusanmani,

Thanks for your patience. [quote=“madhusanmani, post:5, topic:160222”]
i tried with the belwo code, but it highlighted all teh instance of the Word document not only the

Calculation Agent : EUR.

My requirement is to only highlight the above line instance of Calculation Agent and EUR"
[/quote]
Please use the following code example to achieve your requirement. Hope this helps you.

Document doc = new Document(MyDir + "in.docx");

Pattern regex = Pattern.compile("EUR", Pattern.CASE_INSENSITIVE);
FindReplaceOptions findReplaceOptions = new FindReplaceOptions();
findReplaceOptions.setReplacingCallback(new ReplaceEvaluatorFindAndHighlight("Calculation Agent"));
doc.getRange().replace(regex, "", findReplaceOptions);

doc.save(MyDir + "17.7.docx");
----------------------------------------------------------------
class ReplaceEvaluatorFindAndHighlight implements IReplacingCallback {
    String text;
    ReplaceEvaluatorFindAndHighlight(String text)
    {
        this.text = text;
    }
    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);
        }

        Cell cell = (Cell)((Run)runs.get(0)).getAncestor(NodeType.CELL);
        if(cell != null)
        {
            if(cell.getPreviousSibling().toString(SaveFormat.TEXT).trim().equals(this.text))
            {
                // Now highlight all runs in the sequence.
                for (Run run : (Iterable<Run>) runs)
                    run.getFont().setHighlightColor(Color.YELLOW);

                //Uncomment the following lines if you want to highlight text "Calculation Agent"
                //for (Run run : (Iterable<Run>) ((Cell)cell.getPreviousSibling()).getChildNodes(NodeType.RUN, true))
                //    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  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;
    }
}

If you want to compare two documents and highlight the changes, please use the following code example. Hope this helps you.

Document doc = new Document(MyDir + "in.docx");
Document doc2 = new Document(MyDir + "in2.docx");

doc.compare(doc2, "tahir", new java.util.Date());

for(Revision revision : doc.getRevisions())
{
    if(revision.getParentNode().getNodeType() == NodeType.RUN)
    {
        ((Run)revision.getParentNode()).getFont().setHighlightColor(Color.yellow);
    }
}
doc.save(MyDir + "output.docx");

Best regards
Tahir Manzoor

Hi Tahir,

The below code works thank you, but is it possible only to highlight without adding the diff content of the other file in the comapre document.

FOr EX:

FIle1-DOCX : Calculation EUR
FILE2-DOCX: Calculation USA

in The ouput just highlight EUR, instead of EUR (Strike off) USA

Document doc = new Document(MyDir + “in.docx”);
Document doc2 = new Document(MyDir + “in2.docx”);

doc.compare(doc2, “tahir”, new java.util.Date());

for(Revision revision : doc.getRevisions())
{
if(revision.getParentNode().getNodeType() == NodeType.RUN)
{
((Run)revision.getParentNode()).getFont().setHighlightColor(Color.yellow);
}
}
doc.save(MyDir + “output.docx”);
[/quote]

@madhusanmani,

Thanks for your inquiry. Please call Document.acceptAllRevisions method before saving the document. This method accepts all tracked changes in the document. Hope this helps you.

doc.acceptAllRevisions();
doc.save(MyDir + "output.docx");

If you still face problem, please manually create your expected output Word document using Microsoft Word and attach it here for our reference. We will investigate how you want your final Word output be generated like. We will then provide you more information on this along with code.

Best regards
Tahir Manzoor