Strike through same word in one paragraph or sentence

Hi,

If same word occurs multiple times in single paragraph or sentence then only first occurrence of word get strike out, other occurrences are not getting strike out.

How can we achieve this?

@Deepali_Shirude,
Could you please ZIP and attach your input and expected output documents? We will then provide you code example according to your requirement.

Hi,

Attached Input and expected output document.
Also attached paragraph input.
Word to be strike out from paragraph is “defend”.
Also want to highlight that same word.

Docs.zip (111.2 KB)

@Deepali_Shirude,
To get the desired result, please use the Range.Replace() method as shown below:

var doc = new Document(@"C:\Temp\InputDoc.docx");

FindReplaceOptions options = new FindReplaceOptions();
options.ApplyFont.StrikeThrough = true;
options.ApplyFont.HighlightColor = Color.Yellow;

doc.Range.Replace("defend", "defend", options);

doc.Save(@"C:\Temp\OutputDoc.docx");

This code example will strike out and highlight the word “defend” in the whole document.

If you want to highlight this word only in one Paragraph, please check the code example below:

foreach (Paragraph p in doc.GetChildNodes(NodeType.Paragraph, true))
    if (p.GetText().StartsWith("Customer integrity has to be defend whatever company hold harmless in their hands."))
        p.Range.Replace("defend", "defend", options);

For more information please check the Find and Replace article.

Hi,

Thanks for the response.
This solution works. But I want to track document revisions as well.
So called document.StartTrackRevisions() before this code.
But after accepting revisions strike word should go away.
We have used range.replace method so it will be present even if we accept the revisions.
Tried using range.delete() but it won’t work. Only first occurrence get deleted.
Can you please provide solution on that?

@Deepali_Shirude,
Please use Range.Replace() method to replace the word you need with empty string:

doc.Range.Replace("defend ", "");

Hi,

Already tried this yesterday.
This works but it is not highlighting the word. Word gets strike out only.
I want to highlight and strike the same word with document track revisions.

Can you please check again?

@Deepali_Shirude,
You can use the following code example to get desired result:

doc.StartTrackRevisions("123");

FindReplaceOptions options = new FindReplaceOptions();
options.ApplyFont.StrikeThrough = true;
options.ApplyFont.HighlightColor = Color.Yellow;

doc.StopTrackRevisions();

doc.Range.Replace("defend", "defend", options);

doc.StartTrackRevisions("123");

doc.Range.Replace("defend ", "");

Hi,

This is not working.
Word is getting strike out and highlighted but revisions are not getting tracked.

@Deepali_Shirude If you want the revision to be tracked in MS Word you should also enable this for a document. Simply add the following code line:

doc.TrackRevisions = true;

However you should note that delete revisions made in MS Word will not look strike out and highlighted. This can be configured in MS Word.

Hi,

I don’t want it to be configured in MS Word as Aspose itself provides the methods for tracking revisions i.e. doc.StartTrackRevisions().
And we are not deleting anything here. We are replacing with “” to make it appear as strike out.
Any other solution for this?

@Deepali_Shirude Could you please create an expected output using MS Word and attach it here to help us to better understand your requirements? We will check once again and try to help you.

Hi,

Attaching expected output document.

OutputDoc.zip (58.6 KB)

@Deepali_Shirude Thank you for additional information. What you are asking about is tracking formation changes. However, currently Aspose.Words supports tracking of node insertions and deletions only. Formatting changes are not recorded as revisions.
I have linked you request to the appropriate issue WORDSNET-15792 we will keep you informed and let you know once this feature is available.

Hi,

Thanks for your response.