How to Start Convert PDF Read PDF Build PDF Work with PDF Modules PDF Document PDF Pages Text Image Graph & Path Annotation, Markup & Drawing Redaction Security Digital Signature Forms Watermark Bookmark Link File Attachment File Metadata Printing Work with Other SDKs Barcode read Barcode create OCR Twain

C# PDF Text Editor Library
How to find, replace text in PDF file using C# .net


C# Demo Code to Replace Text in PDF File with C#.NET PDF Library









  • Free PDF SDK library for enable users the ability to replace PDF text in Visual C# .NET framework project
  • Support .NET WinForms, ASP.NET MVC in IIS, ASP.NET Ajax, Azure cloud service, DNN (DotNetNuke), SharePoint
  • A Professional C#.NET PDF edit control able to replace PDF text in .NET WinForms and ASP.NET web sever project
  • C#.NET class source codes for manipulating PDF text replacing function in Visual Studio .NET
  • Replace text in PDF file in preview on ASPX webpage
  • Able to replace PDF text in ASP.NET program
  • Other PDF edit functionalities, like add PDF text, add PDF text box and field






C#: how to search and find text from the PDF file


To replace text from PDF pages, you need search, find, locate the text first. With XDoc.PDF for .NET SDK, you can search, find and location text through the following methods:


  1. Do search and find text
  2. Using regular expression to search and find text
  3. Find all text inside a page region
  4. Find the text char by the page position


Simple text search


Using c#, you run searches to find specific text items in PDF file. You can run a simple text search, looking for a search term within list of PDF pages, or a page region. Or you can use advanced search options, and search PDF document. Search Options and example C# source code:


  1. WholeWord: Finds only occurrences of the complete word. For example, if you search for the word inside, the words in and side aren't found.
  2. IgnoreCase: Finds only occurrences of the words that match the capitalization you provide. For example, if you search for the word White, the words white and WHITE aren't found.
  3. ContextExpansion: The number or chars will be returned with searched text


RESearchOption searchOps = new RESearchOption();
searchOps.MatchString = "RasterEdge";
searchOps.IgnoreCase = true;
searchOps.WholeWord = false;
searchOps.ContextExpansion = 0;


Text search with regular expression


In C#, you can do advanced text search with regular expression. The following C# example source code support text search on urls.



//  Search pattern for URL
String pattern = @"\b(\S+)://(\S+)\b";
RegexOptions regexOps = RegexOptions.IgnoreCase;






Search and replace text from the specified page(s)


The C# example source code below shows how to do text search, and replace text from PDF page ranges.



String inputFilePath = @"C:\1.pdf";
String outputFilePath = @"C:\output.pdf";

//  Open file
PDFDocument doc = new PDFDocument(inputFilePath);

//  Search text "RasterEdge"
String matchString = "RasterEdge";
//  Set search option
RESearchOption searchOps = new RESearchOption();
searchOps.MatchString = matchString;
searchOps.IgnoreCase = true;
searchOps.WholeWord = false;
searchOps.ContextExpansion = 0;
//  Set search range (from page 1 to 3)
int pageOffset = 0;
int pageCount = 3;

//  New string to replace the old value.
String replaceText = "[Replace]";
//  Set replace text settings
PDFDocument.TextReplaceOption replaceOps = new PDFDocument.TextReplaceOption();
replaceOps.TextFont = new System.Drawing.Font("Arial", 16F, FontStyle.Regular);
replaceOps.TextColor = System.Drawing.Color.Red;
//  Apply string replacing
doc.Replace(matchString, replaceText, searchOps, pageOffset, pageCount, replaceOps);

//  Save file
doc.Save(outputFilePath);






Search and replace text from the specified page region


The C# example source code below shows how to do text search, and replace text from a PDF page region.



String inputFilePath = @"C:\1.pdf";
String outputFilePath = @"C:\output.pdf";

//  Open file
PDFDocument doc = new PDFDocument(inputFilePath);

//  Search text "RasterEdge"
String matchString = "RasterEdge";
//  Set search option
RESearchOption searchOps = new RESearchOption();
searchOps.MatchString = matchString;
searchOps.IgnoreCase = true;
searchOps.WholeWord = false;
searchOps.ContextExpansion = 0;
//  Set target page region in the 1st page.
int pageIndex = 0;
//  Region: start point (0,0), with = 500, height = 300. Unit: pixel (in 96 dpi).
RectangleF pageRegion = new RectangleF(0, 0, 500, 300);

//  New string to replace the old value.
String replaceText = "[Replace]";
//  Set replace text settings
PDFDocument.TextReplaceOption replaceOps = new PDFDocument.TextReplaceOption();
replaceOps.TextFont = new System.Drawing.Font("Arial", 16F, FontStyle.Regular);
replaceOps.TextColor = System.Drawing.Color.Red;
//  Apply string replacing
doc.Replace(matchString, replaceText, searchOps, pageIndex, pageRegion, replaceOps);

//  Save file
doc.Save(outputFilePath);






Search and replace text with regular expression from the specified page(s)


The C# example source code below shows how to do text search with regular expression, and replace text from pdf page ranges.



String inputFilePath = @"C:\1.pdf";
String outputFilePath = @"C:\output.pdf";

//  Open file
PDFDocument doc = new PDFDocument(inputFilePath);

//  Search pattern for URL
String pattern = @"\b(\S+)://(\S+)\b";
RegexOptions regexOps = RegexOptions.IgnoreCase;
//  Set search range (from page 1 to 3)
int pageOffset = 0;
int pageCount = 3;

//  New string to replace the old value.
String replaceText = "[Replace]";
//  Set replace text settings
PDFDocument.TextReplaceOption replaceOps = new PDFDocument.TextReplaceOption();
replaceOps.TextFont = new System.Drawing.Font("Arial", 16F, FontStyle.Regular);
replaceOps.TextColor = System.Drawing.Color.Red;
//  Apply string replacing
doc.Replace(pattern, regexOps, replaceText, pageOffset, pageCount, replaceOps);

//  Save file
doc.Save(outputFilePath);






Search and replace text with regular expression from specified page region


The C# example source code below shows how to do text search with regular expression, and replace text from pdf page region.



String inputFilePath = @"C:\1.pdf";
String outputFilePath = @"C:\output.pdf";

//  Open a PDF file
PDFDocument doc = new PDFDocument(inputFilePath);

//  Search pattern for URL
String pattern = @"\b(\S+)://(\S+)\b";
RegexOptions regexOps = RegexOptions.IgnoreCase;
//  Set target page region in the 1st page.
int pageIndex = 0;
//  Region: start point (0,0), with = 500, height = 300. Unit: pixel (in 96 dpi).
RectangleF pageRegion = new RectangleF(0, 0, 500, 300);

//  New string to replace the old value.
String replaceText = "[Replace]";
//  Set replace text settings
PDFDocument.TextReplaceOption replaceOps = new PDFDocument.TextReplaceOption();
replaceOps.TextFont = new System.Drawing.Font("Arial", 16F, FontStyle.Regular);
replaceOps.TextColor = System.Drawing.Color.Red;
//  Apply string replacing
doc.Replace(pattern, regexOps, replaceText, pageIndex, pageRegion, replaceOps);

//  Save file
doc.Save(outputFilePath);