With HiQPdf library you can search a text in a PDF document using the PdfTextExtractSearchText(String, String, Int32, Int32, Boolean, Boolean) method HiQPdfClientPdfTextExtract class. You can choose to match the case or to match the whole word only when searching using this method parameters.
Search Text in PDF Demo
In this demo you can learn how to search a text in a PDF document. You can choose the text to search, you can choose to match or not the case or whole word only and you can choose the range of PDF pages where to search the text. The found text is highlighted in the original PDF.
Demo Source Code
C#
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Hosting; using HiQPdfClient; namespace HiQPdf_Demo.Controllers { public class SearchTextInPdfController : Controller { IWebHostEnvironment m_hostingEnvironment; public SearchTextInPdfController(IWebHostEnvironment hostingEnvironment) { m_hostingEnvironment = hostingEnvironment; } // GET: SearchTextInPdf public ActionResult Index() { SetCrtPageUri(); return View(); } [HttpPost] public ActionResult SearchText(IFormCollection collection) { string serverIP = collection["textBoxServerIP"]; uint serverPort = uint.Parse(collection["textBoxServerPort"]); string serverPassword = collection["textBoxServerPassword"]; // get the PDF file string pdfFile = m_hostingEnvironment.WebRootPath + "/DemoFiles/Pdf/InputPdf.pdf"; // get the text to search string textToSearch = collection["textBoxTextToSearch"]; // create the PDF text extractor PdfTextExtract pdfTextExtract = new PdfTextExtract(serverIP, serverPort); // use server password if necessary if (serverPassword.Length > 0) pdfTextExtract.ServerPassword = serverPassword; // set a demo serial number pdfTextExtract.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ=="; int fromPdfPageNumber = int.Parse(collection["textBoxFromPage"]); int toPdfPageNumber = collection["textBoxToPage"][0].Length > 0 ? int.Parse(collection["textBoxToPage"]) : 0; // search the text in PDF document PdfTextSearchItem[] searchTextInstances = pdfTextExtract.SearchText(pdfFile, textToSearch, fromPdfPageNumber, toPdfPageNumber, collection["checkBoxMatchCase"].Count > 0, collection["checkBoxMatchWholeWord"].Count > 0); // assign a string to this variable if the PDF document to edit is password protected string pdfOpenPassword = null; // load the PDF file to highlight the searched text PdfDocument pdfDocument = PdfDocument.FromFile(serverIP, serverPort, serverPassword, pdfFile, pdfOpenPassword); // set a demo serial number pdfDocument.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ=="; // highlight the searched text in PDF document foreach (PdfTextSearchItem searchTextInstance in searchTextInstances) { PdfRectangle pdfRectangle = new PdfRectangle(searchTextInstance.BoundingRectangle); // set rectangle color and opacity pdfRectangle.BackColor = PdfColor.Yellow; pdfRectangle.Opacity = 30; // highlight the text pdfDocument.Pages[searchTextInstance.PdfPageNumber - 1].Layout(pdfRectangle); } // write the modified PDF document try { // write the PDF document to a memory buffer byte[] pdfBuffer = pdfDocument.WriteToMemory(); FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf"); fileResult.FileDownloadName = "SearchText.pdf"; return fileResult; } finally { pdfDocument.Close(); } } private void SetCrtPageUri() { HttpRequest httpRequest = this.ControllerContext.HttpContext.Request; UriBuilder uriBuilder = new UriBuilder(); uriBuilder.Scheme = httpRequest.Scheme; uriBuilder.Host = httpRequest.Host.Host; if (httpRequest.Host.Port != null) uriBuilder.Port = (int)httpRequest.Host.Port; uriBuilder.Path = httpRequest.PathBase.ToString() + httpRequest.Path.ToString(); uriBuilder.Query = httpRequest.QueryString.ToString(); ViewData["CrtPageUri"] = uriBuilder.Uri.AbsoluteUri; } } }
See Also