Edit PDF Text with C# & .NET

In the modern world, there is often a need to remove or hide confidential information from PDF documents before distributing them. This can be related to the protection of personal data, trade secrets, or other reasons. SautinSoft.PDF provides a convenient set of tools for working with PDF documents, including the ability to hide text. In this article, we will look at how to hide text in a PDF document using C# and .NET.

  1. Add SautinSoft.PDF from NuGet.
  2. Load a PDF document.
  3. Redact (delete) all words "North" from the 1st page.
  4. Draw green rectangles atop the all removed words.
  5. Save the document in PDF format.

Using SautinSoft.PDF, you can easily hide any information in PDF documents, making it an indispensable tool for working with confidential data. By following the steps outlined above, you will be able to effectively manage the content of your PDF files.

Redact

Complete code

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

namespace Sample
{
    class Sample
    {
        /// <summary>
        /// Redact
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/redact.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 pdfFile = Path.GetFullPath(@"..\..\..\simple text.pdf");

            var document = PdfDocument.Load(pdfFile);
            {
                // Assume we want to redact the word "North".
                string textToRedact = "North";

                var page = document.Pages[0];
                var texts = page.Content.GetText().Find(textToRedact);
                
                foreach (var text in texts)
                {
                    text.Redact();
                    // If you want, draw a green rectangle 
                    // at the places where was the text.
                    var bounds = text.Bounds;
                    var rectangle = page.Content.Elements.AddPath().AddRectangle(new PdfPoint(bounds.Left, bounds.Bottom), new PdfSize(bounds.Width, bounds.Height));
                    rectangle.Format.Fill.IsApplied = true;
                    rectangle.Format.Fill.Color = PdfColor.FromRgb(0, 1, 0);
                }
                // Save PDF Document.
                document.Save("out.pdf");
            }
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("out.pdf") { 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.