ByteScout PDF Suite - C# - Regular Expression Search and Highlight in PDF - ByteScout

ByteScout PDF Suite – C# – Regular Expression Search and Highlight in PDF

  • Home
  • /
  • Articles
  • /
  • ByteScout PDF Suite – C# – Regular Expression Search and Highlight in PDF

How to Do Regular Expression Search and Highlight in PDF in C# with ByteScout PDF Suite

Learn to regular expression search and highlight in PDF in C#

Source code documentation samples give a simple and easy method to install a needed feature into your application. ByteScout PDF Suite is the bundle that provides six different SDK libraries to work with PDF from generating rich PDF reports to extracting data from PDF documents and converting them to HTML. This bundle includes PDF (Generator) SDK, PDF Renderer SDK, PDF Extractor SDK, PDF to HTML SDK, PDF Viewer SDK, and PDF Generator SDK for Javascript. It can regular expression search and highlight in PDF in C#.

This prolific sample source code in C# for ByteScout PDF Suite contains various functions and other necessary options you should do by calling the API to regular expression search and highlight in PDF. Just copy and paste the code into your C# application’s code and follow the instructions. This basic programming language sample code for C# will do the whole work for you to regular expression search and highlight in PDF.

The trial version of ByteScout PDF Suite can be downloaded for free from our website. It also includes source code samples for C# and other programming languages.

On-demand (REST Web API) version:
 Web API (on-demand version)

On-premise offline SDK for Windows:
 60 Day Free Trial (on-premise)

Program.cs

      
using System.Diagnostics; using System.Drawing; using Bytescout.PDFExtractor; using Bytescout.PDF; using Pen = Bytescout.PDF.Pen; using SolidBrush = Bytescout.PDF.SolidBrush; namespace SearchAndHighlightExample { class Program { static void Main(string[] args) { const string inputFile = @"sample.pdf"; const int pageIndex = 0; const string searchPattern = "\\d+\\.\\d+"; // Prepare TextExtractor using (TextExtractor textExtractor = new TextExtractor("demo", "demo")) { textExtractor.RegexSearch = true; textExtractor.LoadDocumentFromFile(inputFile); // Load document with PDF SDK using (Document pdfDocument = new Document(inputFile)) { pdfDocument.RegistrationName = "demo"; pdfDocument.RegistrationKey = "demo"; Page pdfDocumentPage = pdfDocument.Pages[pageIndex]; Canvas canvas = pdfDocumentPage.Canvas; SolidBrush fillBrush = new SolidBrush(new ColorRGB(255, 0, 0)); fillBrush.Opacity = 50; // make the brush transparent // Search for pattern and highlight found pieces if (textExtractor.Find(pageIndex, searchPattern, caseSensitive: false)) { do { foreach (var foundPiece in textExtractor.FoundText.Elements) { // Inflate the rectangle a bit RectangleF rect = RectangleF.Inflate(foundPiece.Bounds, 1, 2); // Draw rectangle over the PDF page canvas.DrawRectangle(fillBrush, rect); } } while (textExtractor.FindNext()); } // Save as new PDF document pdfDocument.Save("result.pdf"); // Open result document in default associated application (for demo purposes) Process.Start("result.pdf"); } } } } }

Regular Expression Search and Highlight in PDF using ByteScout PDF SDK

In this tutorial, we will Search and Highlight in PDF using C#. To begin, let’s copy the sample code and paste it into the Visual Studio editor. I added the sample code link in the description box below. To Search and Highlight in PDF, we will use the ByteScout PDF SDK. You can add a reference to the PDF SDK DLL in Solution Explorer. Right-click on reference and select add reference. Look at the ByteScout PDF SDK and add.

RegExPDF

After that, copy the sample file into your folder. Then add the sample file in the solution explorer and make sure to set the output directory into copy always.

We will put the sample file in the input file and type zero for the page index. For the search pattern, we will type “\\d+\\.\\d+”;. For the Regex search, type- True. Then load the input file in the load document from the file property. Now add your registration name and registration key and their corresponding properties accordingly. You can get your license details in the ByteScout dashboard. Then type your output PDF file name.

After setting up, you may now execute the program. You will see the result in your program folder. We can see the sample PDF and the output result.

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit ByteScout PDF Suite Home Page

Explore ByteScout PDF Suite Documentation

Explore Samples

Sign Up for ByteScout PDF Suite Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

Video Guide

Tutorials:

prev
next