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.

Wed Apr 17, 2024 9:47 am

Hi

Your TextFind example works with PdfTextFinder but I can't find PdfTextFinder in the API reference.
In the API reference I find PdfTextExtractor instead.

what is the difference

Regards Peter

peterInStIngbert
 
Posts: 14
Joined: Wed Nov 25, 2020 2:08 pm

Thu Apr 18, 2024 2:13 am

Hi,

Thanks for your inquiry.
The effect of PdfTextFinder object is similar with the feature of finding text in Adobe acrobat, as shown in the following screenshot. Which is used to find and replace text, or find and highlight text.
A797AA0E-0A3E-4bf8-9DAD-EE7ACDA9F609.png


1) You can refer to the following help document to find and highlight text.
Code: Select all
https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Text/Find-and-Highlight-Selected-Text-in-PDF-in-C-VB.NET.html

2) You can refer to the following code to find and replace text
Code: Select all
String input = @"..\..\..\..\..\..\Data\SearchReplaceTemplate.pdf";
            PdfDocument doc = new PdfDocument();

            // Read a pdf file
            doc.LoadFromFile(input);

            // Get the first page of pdf file
            PdfPageBase page = doc.Pages[0];

            // Create a PdfTextFinder object for searching text within the page
            PdfTextFinder finder = new PdfTextFinder(page);
            finder.Options.Parameter = Spire.Pdf.Texts.TextFindParameter.IgnoreCase;

            // Find occurrences of the specified text within the page
            List<PdfTextFragment> finds = finder.Find("Spire.PDF for .NET");

            String newText = "E-iceblue Spire.PDF";

            // Creates a brush
            PdfBrush brush = new PdfSolidBrush(Color.DarkBlue);

            // Defines a font
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 12f, FontStyle.Regular));

            RectangleF rec;

            // Iterate through each found text fragment
            foreach (PdfTextFragment find in finds)
            {
                // Gets the bound of the found text in page
                rec = find.Bounds[0];

                page.Canvas.DrawRectangle(PdfBrushes.White, rec);
                // Draws new text as defined font and color
                page.Canvas.DrawString(newText, font, brush, rec);

                // This method can directly replace old text with newText,but it just can set the background color, can not set font/forecolor
                // find.ApplyRecoverString(newText, Color.Gray);
            }

            String result = "ReplaceAllSearchedText_out.pdf";

            //Save the document
            doc.SaveToFile(result);

The effect of PdfTextExtractor object is that extract text from pdf file according to the extraction condition. You can refer to the following help document:
Code: Select all
https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/How-to-Extract-Text-from-PDF-Document-with-C-/VB.NET.html


Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Return to Spire.PDF