FindAll for a single Paragraph

The WordDocument class has FindAll, which would be great if I wanted to search the entire document.  But I only need to search a single WParagraph.  There is a .Find method to find the first instance, but in some cases there will be more than one instance of the text in the paragraph.  What's the best approach to handle this?

3 Replies 1 reply marked as answer

HC Hemalatha Chiranjeevulu Syncfusion Team November 4, 2020 06:30 PM UTC

Hi Greg,

Thank you for contacting Syncfusion support.

To achieve your requirement, we suggest you to find all the text in Word document and perform the changes for the text found in particular paragraph only.

Please refer the below code example to achieve your requirement: 
//Loads an existing Word document
WordDocument document = new WordDocument(@"D:\Temp\Input.docx");
//Get the paragraph where you need to find the text
WParagraph paragraph = document.LastSection.Paragraphs[0];
//Find the text in document
TextSelection[] textSelections = document.FindAll("DocIO", false, false);
for (int i = 0; i < textSelections.Length; i++)
{
 WTextRange foundTextRange = textSelections[i].GetAsOneRange();
//Check found text is from the expected
paragraph if(foundTextRange.OwnerParagraph == paragraph)
{
 //Here, do the necessary formatting for found text
//Sets the highlight color for the searched text as Yellow. foundTextRange.CharacterFormat.HighlightColor = System.Drawing.Color.Yellow;
}
}
//Saves and closes the Word document
document.Save("Sample.docx");
document.Close();
 

Please refer the below links to know more about find and replace functionality in DocIO:
https://help.syncfusion.com/file-formats/docio/working-with-find-and-replace
https://github.com/SyncfusionExamples/Word-Find-and-Replace-Examples

Please let us know if you have any other questions.

Regards,
Hemalatha C



Marked as answer

GR Greg November 5, 2020 06:01 PM UTC

Thanks...this worked perfectly.

Now I am wondering how to highlight an image as well.  I don't see an option to set image borders.  Is there a way to do this?


HC Hemalatha Chiranjeevulu Syncfusion Team November 6, 2020 08:23 PM UTC

Hi Greg,

Thank you for your update.

To achieve your requirement, we suggest you to set the border for an image and then save the Word document.

Please refer the below code example to achieve your requirement:

 
WordDocument document = new WordDocument();
document.EnsureMinimal();
IWPicture picture= document.LastParagraph.AppendPicture(Image.FromFile(@"download.png"));
picture.CharacterFormat.Border.LineWidth = 2;
document.Save("Image.docx");
Process.Start("Image.docx");
 
 
 

Please let us know if you have any other questions.

Regards,
Hemalatha C
 


Loader.
Up arrow icon