When working with documents in C# and .NET, finding and replacing text is a common task. Whether you are processing DOCX, PDF, or other document formats, SautinSoft provides powerful tools to simplify this process. Below, we will explore how to find and replace text using different methods with SautinSoft components.
Method 1. Using the SautinSoft.Pdf library, you can easily find and replace text in files.
Step-by-step guide:
using System;
using System.IO;
using System.Xml.Linq;
using SautinSoft;
using SautinSoft.Pdf;
using SautinSoft.Pdf.Content;
namespace Sample
{
class Sample
{
static void Main(string[] args)
{
string inpFile = @"..\..\..\Test.pdf";
string outFile = "Result.pdf";
using var document = PdfDocument.Load(inpFile, new PdfLoadOptions() {});
document.Load();
var page = document.Pages[0];
var textsPRICEATGRANT = page.Content.GetText().Find("PRICEATGRANT").ToList();
var textsGRANT_DATE = page.Content.GetText().Find("GRANT_DATE").ToList();
var textsEMPLOYEE_MAIL_ADDRESS = page.Content.GetText().Find("EMPLOYEE_MAIL_ADDRESS").ToList();
var textsEMPLOYEE_CPF = page.Content.GetText().Find("EMPLOYEE_CPF").ToList();
using var formattedText = new PdfFormattedText();
foreach (var text in textsPRICEATGRANT)
{
var element = text.Elements.First();
var font = element.Format.Text.Font;
formattedText.Clear();
formattedText.Append("23.50");
page.Content.DrawText(formattedText, new PdfPoint(text.Bounds.Left, text.Bounds.Bottom));
text.Redact();
}
foreach (var text in textsGRANT_DATE)
{
var element = text.Elements.First();
var font = element.Format.Text.Font;
formattedText.Clear();
formattedText.Append("23/05/2025");
page.Content.DrawText(formattedText, new PdfPoint(text.Bounds.Left, text.Bounds.Bottom));
text.Redact();
}
foreach (var text in textsEMPLOYEE_MAIL_ADDRESS)
{
var element = text.Elements.First();
var font = element.Format.Text.Font;
formattedText.Clear();
formattedText.Append("dev.teste@sautinsoft.com");
page.Content.DrawText(formattedText, new PdfPoint(text.Bounds.Left, text.Bounds.Bottom));
text.Redact();
}
foreach (var text in textsEMPLOYEE_CPF)
{
var element = text.Elements.First();
var font = element.Format.Text.Font;
formattedText.Clear();
formattedText.Append("12345678909");
page.Content.DrawText(formattedText, new PdfPoint(text.Bounds.Left, text.Bounds.Bottom));
text.Redact();
}
document.Save(outFile, new PdfSaveOptions() { Version = PdfVersion.PDF_A_1A});
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(inpFile) { UseShellExecute = true });
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true });
}
}
}
Method 2. Let's look at another search and replace option using the SautinSoft.Document library.
Step-by-step guide:
using System;
using System.IO;
using System.Linq;
using SautinSoft.Document;
namespace Example
{
class Program
{
static void Main(string[] args)
{
FindAndReplaceInParagraphs();
}
static void FindAndReplaceInParagraphs()
{
string filePath = @"..\..\..\Test.pdf";
string fileResult = @"Result.pdf";
DocumentCore dc = DocumentCore.Load(filePath, new PdfLoadOptions() { PreserveEmbeddedFonts = true});
foreach (Paragraph par in dc.GetChildElements(true, ElementType.Paragraph))
foreach (ContentRange item in par.Content.Find("PRICEATGRANT").Reverse())
{
item.Replace("23.50");
}
foreach (Paragraph par in dc.GetChildElements(true, ElementType.Paragraph))
foreach (ContentRange item in par.Content.Find("GRANT_DATE").Reverse())
{
item.Replace("23/05/2025");
}
foreach (Paragraph par in dc.GetChildElements(true, ElementType.Paragraph))
foreach (ContentRange item in par.Content.Find("EMPLOYEE_MAIL_ADDRESS").Reverse())
{
item.Replace("dev.teste@sautinsoft.com");
}
foreach (Paragraph par in dc.GetChildElements(true, ElementType.Paragraph))
foreach (ContentRange item in par.Content.Find("EMPLOYEE_CPF").Reverse())
{
item.Replace("12345678909");
}
dc.Save(fileResult, new PdfSaveOptions() { Compliance = PdfCompliance.PDF_A1a});
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(filePath) { UseShellExecute = true });
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(fileResult) { UseShellExecute = true });
}
}
}
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: