Documents for PDF .NET Edition
Features / Text Search
In This Topic
    Text Search
    In This Topic

    GcPdf allows text search in a PDF document to find all occurences of the specified text and highlights them. It also works across line breaks, so logically connected text that is rendered on different text lines can also be found. The FindText method of GcPdfDocument class can be used for the same. This method accepts object of FindTextParams and OutputRange class as parameters to find all the occurrences of the searched string in the loaded document.

    Search Text 

    Search Text

    To search text in PDF document:

    1. Create an object of GcPdfDocument class.
    2. Load any existing PDF file using the Load method.
    3. Use the FindText method of GcPdfDocument class to perform text search.
      C#
      Copy Code
      public void CreatePDF(Stream stream)
      {
          var doc = new GcPdfDocument();
      
          // The original file stream must be kept open while working with the loaded PDF
          using (var fs = new FileStream(Path.Combine("Resources","PDFs","BalancedColumns.pdf"),
            FileMode.Open, FileAccess.Read))
          {
              doc.Load(fs);
              // Find all 'lorem', using case-insensitive word search:
              var findsLorem = doc.FindText
             (new FindTextParams("lorem", true, false), OutputRange.All);
      
              // Highlight all 'lorem' using semi-transparent orange red:
              foreach (var find in findsLorem)
                  doc.Pages[find.PageIndex].Graphics.FillPolygon
                 (find.Bounds[0], Color.FromArgb(100, Color.OrangeRed));
      
              // Done:
              doc.Save(stream);
          }
      
    Back to Top

    For more information about implementation of text search using GcPdf, see GcPdf sample browser.