Find, highlight and replace text on all the pages of PDF document in C#
Tags: dotnet, csharp, pdf, text
Published: 2019-09-24 07:21:41

---
title: Find, highlight and replace text on all the pages of PDF document in C#
published: true
description: find and highlight text on PDF; find and replace text on all the pages of PDF
tags: dotnet, csharp, pdf, text
---

Introduction
Sometimes, we need to search the PDF file to get specific text, but there may be numerous results returned. In order to confirm the location more conveniently, we need to highlight the selected text with background color while searching. Besides the find and highlight feature, Spire.PDF also supports to replace the searched text in all the pages of PDF document. In this article, we will give solutions of how to search, highlight and replaced selected text in PDF files pro-grammatically in C# without using any third-party tools.
•Find and highlight the searched text on all the pages of PDF file
•Find and replace text string in PDF document with new text string 
<b>Highlight searched text</b>
We can use page.FindText(string) method and foreach sentence to find specified text in entire PDF pages , save the search results in a PdfTextFind array, then highlight selected text by invoking find.ApplyHighLight() method:

```C#
using Spire.Pdf;
using Spire.Pdf.General.Find;

namespace FindandHighlightTextonPDF
{
    class Program
    {
        static void Main(string[] args)
        {

            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile("Sample.pdf");

            PdfTextFind[] result = null;

            foreach (PdfPageBase page in doc.Pages)
            {
                
                result = page.FindText("Spire.PDF for .NET",TextFindParameter.IgnoreCase).Finds;
                foreach (PdfTextFind find in result)
                {
                    //Apply highlight
                    find.ApplyHighLight();
                }
            }
                    
            //Save the document
            doc.SaveToFile("Highlight.pdf");

        }
    }
```
![Find and Highlight](https://thepracticaldev.s3.amazonaws.com/i/hcz8fvrdfr01xw3iiffu.png) 

<b>Find and Replace:</b>
We can use page.FindText(string) method and foreach sentence to find specified text in entire PDF pages , and then draw the new text string by setting its font and size to cover them.

```C#
using Spire.Pdf;
using Spire.Pdf.Fields;
using Spire.Pdf.General.Find;
using System.Drawing;
using Spire.Pdf.Graphics;
using System;

namespace FindandReplaceTextonPDF
{
    class Program
    {
        static void Main(string[] args)
        {

            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile("Sample.pdf");

            // Get the first page of pdf file
            PdfPageBase page = doc.Pages[0];

            //Searches "Spire.PDF for .NET" by ignoring case
            PdfTextFindCollection collection = page.FindText("Spire.PDF for .NET", TextFindParameter.IgnoreCase);

            String newText = "E-iceblue Spire.PDF";

            //Creates a brush
            PdfBrush brush = new PdfSolidBrush(Color.DarkBlue);
            //Defines a font
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 12f, FontStyle.Regular));

            RectangleF rec;
            foreach (PdfTextFind find in collection.Finds)
            {
                // Gets the bound of the found text in the first page
                rec = find.Bounds;

                page.Canvas.DrawRectangle(PdfBrushes.White, rec);
                // Draws new text use defined font and color
                page.Canvas.DrawString(newText, font, brush, rec);

            }

            //Save the document
            doc.SaveToFile("Result.pdf");


        }
    }
}
```
![Find and Replace](https://thepracticaldev.s3.amazonaws.com/i/dpuzqeplpa4bp06qftpw.png)
Conclusion

It is quite easy for us to use find, highlight and replace the text on the PDF files in C#. Thanks for your reading and wish it helps.