Locating and Swapping Text in PDFs Using C# and .NET

Are you looking for an efficient way to find and replace text in PDF documents using C# and .NET? Look no further than Sautinsoft, a powerful tool that simplifies the process and allows you to seamlessly modify text within your PDFs.

With Sautinsoft, you can easily locate specific text within your PDF document and replace it with new content. This can be incredibly useful for updating outdated information, correcting errors, or customizing documents to meet your specific needs.

  1. Add SautinSoft.PDF from NuGet.
  2. Load a PDF document. Iterate by all pages.
  3. Find the word "North".
  4. Draw the text "South" by orange font, using the coordinates.
  5. Delete (redact) the text "North".
  6. Save the document in PDF format.
Find and Replace

Complete code

using System;
using System.IO;
using SautinSoft;
using SautinSoft.Pdf;
using SautinSoft.Pdf.Content;

namespace Sample
{
    class Sample
    {
        /// <summary>
        /// Find and Replace text in PDF document using C# and .NET
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/find-and-replace-text-in-pdf-document-csharp-dotnet.php
        /// </remarks>
        static void Main(string[] args)
        {
            // Before starting this example, please get a free 30-day trial key:
            // https://sautinsoft.com/start-for-free/

            // Apply the key here:
            // PdfDocument.SetLicense("...");

            string inpFile = Path.GetFullPath(@"..\..\..\simple text.pdf");            
            string outFile = Path.ChangeExtension(inpFile, ".res.pdf");

            using (PdfDocument document = PdfDocument.Load(inpFile))
            {
                // Assume we want to find the word "North"
                // and replace it to the "South".
                string textFrom = "North";
                string textTo = "South";

                // Iterate by all pages
                foreach (var page in document.Pages)
                {
                    // Find the text.
                    var texts = page.Content.GetText().Find(textFrom);
                    
                    // Get the text coordinates and font from 1st Element;
                    // Draw the new text.
                    foreach (var text in texts)
                    {
                        foreach (var el in text.Elements)
                        {
                            // Get the text formatting, coordinates;
                            // Draw the new text "South".
                            using (var formattedText = new PdfFormattedText())
                            {
                                formattedText.Font = el.Format.Text.Font;
                                // Set "orange" color
                                formattedText.Color = PdfColor.FromRgb(1, 0.647, 0);
                                formattedText.AppendLine(textTo);
                                page.Content.DrawText(formattedText, new PdfPoint(text.Bounds.Left, text.Bounds.Bottom - text.Bounds.Height));
                            }
                            break;
                        }
                        // Remove the text.
                        text.Redact();
                    }
                }
                document.Save(outFile);
            }
            // Show the result.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true });
        }
    }
}

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.