Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Fri Jul 26, 2019 4:00 pm

Is it possible to search for text and outline it if found?

damadake
 
Posts: 20
Joined: Wed Jan 18, 2017 12:56 pm

Mon Jul 29, 2019 2:35 am

Hello,

Thanks for your inquiry.
Below code shows how to find text and highlight it. If this is not what you want, please provide your expected effect to help us have a better investigation.
Code: Select all
static void Main(string[] args)
        {
            //Load the document from disk
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile("test.pdf");

            PdfTextFind[] result = null;
            foreach (PdfPageBase page in pdf.Pages)
            {
                //Find text
                result = page.FindText("science").Finds;

                foreach (PdfTextFind find in result)
                {
                    //Highlight searched text
                    find.ApplyHighLight();
                }
            }

            //Save the document
            pdf.SaveToFile("result.pdf", FileFormat.PDF);

            //View the Pdf file
            System.Diagnostics.Process.Start("result.pdf");
        }


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Mon Jul 29, 2019 11:43 am

I don't want to highlight but add an outline or border around a word or words. Similar to the look of a text box.

Thank you

damadake
 
Posts: 20
Joined: Wed Jan 18, 2017 12:56 pm

Tue Jul 30, 2019 1:50 am

Hello,

Thanks for your feedback.
Below code shows how to find text and add a border around it. If there is any question, just feel free to contact us.
Code: Select all
static void Main(string[] args)
        {
            //Load the document from disk
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile("test.pdf");

            //Set the pen
            PdfPen pen = new PdfPen(PdfBrushes.Red, 1f);

            PdfTextFind[] result = null;
            foreach (PdfPageBase page in pdf.Pages)
            {
                //Find text
                result = page.FindText("science").Finds;

                foreach (PdfTextFind find in result)
                {
                    //Get the bounds of the searched text
                    RectangleF rect = find.Bounds;
                    //Draw a rectangle
                    page.Canvas.DrawRectangle(pen, rect);
                }
            }

            //Save the document
            pdf.SaveToFile("result.pdf", FileFormat.PDF);

            //View the Pdf file
            System.Diagnostics.Process.Start("result.pdf");
        }


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Thu Aug 01, 2019 6:58 am

Hello,

Greetings from E-iceblue!
Have you tested my code? Could you please give us some feedback at your convenience?

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Thu Aug 08, 2019 11:51 am

Yes, this worked, thank you. Sorry i didn't respond sooner

damadake
 
Posts: 20
Joined: Wed Jan 18, 2017 12:56 pm

Fri Aug 09, 2019 1:06 am

Hello,

Thanks for your feedback.
If you encounter any issue ralated to our products in th future, please feel free to write back. Wish you all the best!

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.PDF