Sample code · C#
using System; using System.IO; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using SelectPdf.Universal; namespace SelectPdf.Samples.Pages { public class PdfTextWordsModel : PageModel { public void OnGet() { } [BindProperty] public int TxtPageNumber { get; set; } = 1; [BindProperty] public float TxtMinFontSize { get; set; } = 14; public IActionResult OnPost() { // the test file string filePdf = Path.Combine(Startup.ServerPath, "wwwroot", "files", "selectpdf.pdf"); // instantiate a pdf to text converter object PdfToText pdfToText = new PdfToText(); // load PDF file pdfToText.Load(filePdf); // every word of the page, with its rectangle, font name and font size PdfTextWord[] words = pdfToText.GetWords(Math.Max(1, TxtPageNumber)); pdfToText.Close(); // open the existing PDF document in editing mode PdfDocument doc = new PdfDocument(filePdf); // outline every word, and highlight the words drawn in a large font foreach (PdfTextWord word in words) { PdfPage page = doc.Pages[word.PageNumber - 1]; PdfRectangleElement rect = new PdfRectangleElement( word.X, word.Y, word.Width, word.Height); rect.ForeColor = new PdfColor(0, 102, 204); rect.LineStyle.LineWidth = 0.5f; if (word.FontSize >= TxtMinFontSize) { rect.BackColor = new PdfColor(240, 240, 0); rect.Transparency = 30; } page.Add(rect); } // save pdf document byte[] pdf = doc.Save(); // close pdf document doc.Close(); // return resulted pdf document FileResult fileResult = new FileContentResult(pdf, "application/pdf"); fileResult.FileDownloadName = "Document.pdf"; return fileResult; } } }