Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Thu Jul 22, 2021 5:21 pm

Hello,

I need to search a word document for a text, for example "Lastname".

And then all lines / paragraphs in the document that contain this text I need to draw a border around.

Is this possible?

Example:
Search for "Lastname", draw border

Expected result:
Last edited by Heinz4711 on Fri Jul 23, 2021 5:44 am, edited 3 times in total.

Heinz4711
 
Posts: 20
Joined: Thu Feb 07, 2019 5:22 pm

Fri Jul 23, 2021 2:04 am

Hello,

Thanks for your inquiry.
Please refer to the following code to meet your needs.

Code: Select all
            Document document = new Document();
            document.LoadFromFile("test.docx");

            TextSelection[] textSelections = document.FindAllString("Lastname", false, true);
            foreach (TextSelection selection in textSelections)
            {
                selection.GetAsOneRange().CharacterFormat.HighlightColor = Color.Yellow;

                selection.GetAsOneRange().CharacterFormat.Border.BorderType = BorderStyle.Hairline;
                selection.GetAsOneRange().CharacterFormat.Border.Color = Color.Red;               
            }

            document.SaveToFile("result.docx", FileFormat.Docx);

If this cannot help you, please provide your input file and your desired output for further investigation. You can send them to us ([email protected]) via email. Thanks in advance.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Fri Jul 23, 2021 6:10 am

Hello,

Thanks for providing more details via email.
Please refer to the following modified code.
Code: Select all
            Document document = new Document();
            document.LoadFromFile("test.docx");

            TextSelection[] textSelections = document.FindAllString("Lastname", false, true);
            foreach (TextSelection selection in textSelections)
            {
                Paragraph ownerParagraph = selection.GetAsOneRange().OwnerParagraph;
                ownerParagraph.Format.Borders.BorderType= BorderStyle.Hairline;
                ownerParagraph.Format.Borders.Color = Color.Black;
            }

            document.SaveToFile("result.docx", FileFormat.Docx);


If there are any other issues related to our products, just feel free to contact us.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Return to Spire.Doc