Articles in this section
Category / Section

How to find text in PDF document Using C# in ASP.NET Core?

3 mins read

The Syncfusion Essential PDF is a .NET PDF library allows you to find text from an existing PDF document.

Steps to find text in a PDF programmatically:

  1. Create a new ASP.NET Core application project.

    aspnetcore1.png

    Screenshot (992).png

    Screenshot (993).png

  2. Install the Syncfusion.Pdf.Net.Core NuGet package as reference to your .NET Framework application from NuGet.org.

    creation3.png

  3. Include the following namespaces in HomeController.cs file.

using Find_text_in_PDF_document.Models;
using Microsoft.AspNetCore.Mvc;
using Syncfusion.Drawing;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;
using System.Diagnostics;
  1. Use the following code samples in Index.cshtml.
@{
   Html.BeginForm("FindText", "Home", FormMethod.Post);
   {
       <div>
           <input type="submit" value="Find text" style="width:150px;height:27px" />
       </div>
   }
   Html.EndForm();
}
  1. Use the following code sample in HomeController.cs.
[HttpPost]
       public IActionResult FindText(string ViewTemplate, string Find)
       {
           FileStream fileStreamInput = new FileStream("../input.pdf", FileMode.Open, FileAccess.Read);
           PdfLoadedDocument loadedDocument = new PdfLoadedDocument(fileStreamInput);
           Dictionary<int, List<RectangleF>> matchRects = new Dictionary<int, List<RectangleF>>();
           loadedDocument.FindText("Name", out matchRects);
           for (int i = 0; i < loadedDocument.Pages.Count; i++)
           {
               List<RectangleF> rectCoords = matchRects[i];
               for (int j = 0; j < rectCoords.Count; j++)
               {
                   RectangleF bounds = new RectangleF(rectCoords[j].X, rectCoords[j].Y, rectCoords[j].Width, rectCoords[j].Height);
                   //Add text markup annotation on the bounds of highlighting text.
                   PdfTextMarkupAnnotation textmarkup = new PdfTextMarkupAnnotation(bounds);
                   //Set the markup annotation type as HighLight.
                   textmarkup.TextMarkupAnnotationType = PdfTextMarkupAnnotationType.Highlight;
                   //Set the content of the annotation
                   textmarkup.Text = "Highlight Text";
                   //Set the highlighting color.
                   textmarkup.TextMarkupColor = new PdfColor(Color.Yellow);
                   //Add annotation into the page.
                   loadedDocument.Pages[i].Annotations.Add(textmarkup);
               }
           }
           //Create memory stream.
           MemoryStream stream = new MemoryStream();
           //Save and close the document. 
           loadedDocument.Save(stream);
           loadedDocument.Close();
           return File(stream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Pdf, "output.pdf");
           loadedDocument.Close(true);
       }

By executing the program, you will get the PDF document as follows.Screenshot (1010).png

You can download a complete working sample from Find_text_in_PDF document.zip.

Take a moment to peruse the documentation, where you can find other options like extract text and extract text with bounds . Also, the features like headers and footers, bookmarks, tables, hyperlink, annotationPDF documents, and more with code examples

Conclusion
I hope you enjoyed learning about how to find text in PDF document Using C# in ASP.NET Core.

You can refer to our ASP.NET Core feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied