How to Start Tutorials Troubleshooting Main Operations Convert PDF Read PDF Edit PDF PDF Report Generator 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 search, replace text in PDF File using C#.NET PDF Library











In this tutorial, you learn how to search, replace text in PDF file in the C# ASP.NET MVC Web, Windows applications.

  • Find, replace text in PDF document, pages, page region with coordinates
  • Find, replace text using regular expression
  • Find, replace text with advanced settings
  • Find, replace horizontal and vertical text in PDF
  • Find, replace text with inherited font styles

How to find and replace text in PDF file using C#

  1. Download XDoc.PDF Text Editor C# library
  2. Install C# library to search text inside PDF document
  3. Step by Step Tutorial


























  • 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
  • C# PDF library: c# extract text from pdf, c# remove images from pdf, remove text from pdf c#, c# add png to pdf, how to search text in pdf using c#.
  • 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 horizontal and vertical text in ASP.NET MVC web app
  • 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;






Find and replace text from the specified PDF page(s) using C#


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);






Find, replace vertical text or text with rotation angles in PDF file using C#


Sometimes the replaced text in PDF is not horizontal, it is vertical or with rotation angles. There are two options to apply the text replacement in PDF file.


  1. TextHoriAlignment: Set text alignment for the replace text in horizontal. Default: HorizontalAlignment.Left
  2. TextVertAlignment: Set text alignment for the replace text in vertical. Default: VerticalAlignment.Bottom


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

int pageOffset = 0;
int pageCount = 1;

PDFDocument.TextReplaceOption replaceOps = new PDFDocument.TextReplaceOption();
replaceOps.TextFont = new System.Drawing.Font("ISOCPEUR", 15.3F, System.Drawing.FontStyle.Regular);
replaceOps.TextColor = Color.Red;
//  Set the rotate angle of the replace text.
//  Value range: 0 ~ 360 degrees (excluding 360); otherwise, this property is ignored.
//  If UseHighPrecisionRotateAngle is false, this property must be a multiple of 5.
replaceOps.TextRotateAngle = rotateAngle;
//  Indicate if use high precision mode for rotate angle (in 1 degree).
//  Default: false
//replaceOps.UseHighPrecisionRotateAngle = true;

//  Set text alignment for the replace text in horizontal.
//  Default: HorizontalAlignment.Left
replaceOps.TextHoriAlignment = HorizontalAlignment.Center;
//  Set text alignment for the replace text in vertical.
//  Default: VerticalAlignment.Bottom
replaceOps.TextVertAlignment = VerticalAlignment.Bottom;

//  do replacement
doc.Replace(matchString, repalceText, searchOps, pageOffset, pageCount, replaceOps);






Find, replace text with inherited font styles (font name, style, font size) using C# in ASP.NET, Windows app


If the replace text is using the same font styles with the replaced ones, there are three options to apply


  1. UseReplacedTextFont: If flag is set, it would use the embedded font in the source PDF file instead of the system font set by the property TextFont.
    All characters in the replace text MUST HAVE mapping glyphs in the embedded font, otherwise, it would use the specified system font.
    Default: false
  2. EnableSubstituteChar: If flag is set, it would use the substitue character to replace those characters which are not in the embeded font.
    This feature could be used to verify if all characters in the replace text are in the embedded font.
    Default: false
  3. SubstituteChar: The substitute character MUST HAVE valid glyph in the embedded font; otherwise, those characters been replaced would be ignored.
    Default: '?'


    PDFDocument doc = new PDFDocument(inputFilePath);

    String matchString = "Werkstoff";
    String repalceText = "Hello";
    RESearchOption searchOps = new RESearchOption();
    searchOps.MatchString = matchString;
    searchOps.IgnoreCase = true;
    searchOps.WholeWord = false;
    searchOps.ContextExpansion = 0;

    int pageOffset = 0;
    int pageCount = 1;

    PDFDocument.TextReplaceOption replaceOps = new PDFDocument.TextReplaceOption();
    //  Set font for the replace text
    replaceOps.TextFont = new System.Drawing.Font("ISOCPEUR", 15.3F, System.Drawing.FontStyle.Regular);
    replaceOps.TextColor = Color.Red;

    //  If flag is set, it would use the embedded font in the source PDF file instead of
    //  the system font set by the property TextFont.
    //  All characters in the replace text MUST HAVE mapping glyphs in the embedded font,
    //  otherwise, it would use the specified system font.
    //  Default: false
    replaceOps.UseReplacedTextFont = true;
    //  If flag is set, it would use the substitue character to replace those characters
    //  which are not in the embeded font.
    //  This feature could be used to verify if all characters in the replace text are in
    //  the embedded font.
    //  Default: false
    replaceOps.EnableSubstituteChar = false;
    //  The substitute character MUST HAVE valid glyph in the embedded font; otherwise,
    //  those characters been replaced would be ignored.
    //  Default: '?'
    replaceOps.SubstituteChar = '?';

    //  do replacement
    doc.Replace(matchString, repalceText, searchOps, pageOffset, pageCount, replaceOps);

    doc.Save(outputFilePath);