DEV Community

loading...

Find, highlight and replace text on all the pages of PDF document in C#

eiceblue profile image E-iceblue Product Family ・2 min read

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
Highlight searched text
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:

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

Find and Replace:
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.

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
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.

Discussion

pic
Editor guide