Replacer.Replace
Replace(string, string, string, string)
Replaces all occurrences of a specified character string pattern with a replacement string.
public static int Replace(string inputFileName, string outputFileName, string pattern,
string replacement)
Parameter | Type | Description |
---|---|---|
inputFileName | String | The input file name. |
outputFileName | String | The output file name. |
pattern | String | A string to be replaced. |
replacement | String | A string to replace all occurrences of pattern. |
Return Value
The number of replacements made.
Remarks
If the output format is an image (BMP, EMF, EPS, GIF, JPEG, PNG, or WebP), each page of the output will be saved as a separate file. The specified output file name will be used to generate file names for each part following the rule: outputFile_partIndex.extension.
If the output format is TIFF, the output will be saved as a single multi-frame TIFF file.
Examples
Shows how to replace text in the document.
int replacements = Replacer.Replace(MyDir + "Replacing.docx", ArtifactsDir + "Replacer.Replace.docx", "findme", "Replacement Text");
See Also
- class Replacer
- namespace Wordize.Replacing
- assembly Wordize
Replace(string, string, SaveFormat, string, string, FindReplaceOptions)
Replaces all occurrences of a specified character string pattern with a replacement string.
public static int Replace(string inputFileName, string outputFileName, SaveFormat saveFormat,
string pattern, string replacement, FindReplaceOptions options = null)
Parameter | Type | Description |
---|---|---|
inputFileName | String | The input file name. |
outputFileName | String | The output file name. |
saveFormat | SaveFormat | The save format. |
pattern | String | A string to be replaced. |
replacement | String | A string to replace all occurrences of pattern. |
options | FindReplaceOptions | FindReplaceOptions object to specify additional options. |
Return Value
The number of replacements made.
Remarks
If the output format is an image (BMP, EMF, EPS, GIF, JPEG, PNG, or WebP), each page of the output will be saved as a separate file. The specified output file name will be used to generate file names for each part following the rule: outputFile_partIndex.extension.
If the output format is TIFF, the output will be saved as a single multi-frame TIFF file.
Examples
Shows how to replace text in the document using customized find/replace options.
FindReplaceOptions options = new FindReplaceOptions();
options.MatchCase = true;
options.FindWholeWordsOnly = true;
int replacements = Replacer.Replace(MyDir + "Replacing.docx",
ArtifactsDir + "Replacer.Replace1.docx",
SaveFormat.Docx,
"findme",
"Replacement Text",
options);
See Also
- enum SaveFormat
- class FindReplaceOptions
- class Replacer
- namespace Wordize.Replacing
- assembly Wordize
Replace(Stream, Stream, SaveFormat, string, string, FindReplaceOptions)
Replaces all occurrences of a specified character string pattern with a replacement string.
public static int Replace(Stream inputStream, Stream outputStream, SaveFormat saveFormat,
string pattern, string replacement, FindReplaceOptions options = null)
Parameter | Type | Description |
---|---|---|
inputStream | Stream | The input stream. |
outputStream | Stream | The output stream. |
saveFormat | SaveFormat | The save format. |
pattern | String | A string to be replaced. |
replacement | String | A string to replace all occurrences of pattern. |
options | FindReplaceOptions | FindReplaceOptions object to specify additional options. |
Return Value
The number of replacements made.
Remarks
If the output format is an image (BMP, EMF, EPS, GIF, JPEG, PNG, or WebP), only the first page of the output will be saved to the specified stream.
If the output format is TIFF, the output will be saved as a single multi-frame TIFF to the specified stream.
Examples
Shows how to replace text in the document loaded from stream using customized find/replace options.
FindReplaceOptions options = new FindReplaceOptions();
options.UseSubstitutions = true;
using (Stream input = File.OpenRead(MyDir + "Replacing.docx"))
using (Stream output = File.Create(ArtifactsDir + "Replacer.Replace2.docx"))
{
int replacements = Replacer.Replace(input, output, SaveFormat.Docx, "findme", "We Replaced \"$0\" Text", options);
}
See Also
- enum SaveFormat
- class FindReplaceOptions
- class Replacer
- namespace Wordize.Replacing
- assembly Wordize
Replace(string, string, Regex, string)
Replaces all occurrences of a specified character string pattern with a replacement string.
public static int Replace(string inputFileName, string outputFileName, Regex pattern,
string replacement)
Parameter | Type | Description |
---|---|---|
inputFileName | String | The input file name. |
outputFileName | String | The output file name. |
pattern | Regex | A regular expression pattern used to find matches. |
replacement | String | A string to replace all occurrences of pattern. |
Return Value
The number of replacements made.
Remarks
If the output format is an image (BMP, EMF, EPS, GIF, JPEG, PNG, or WebP), each page of the output will be saved as a separate file. The specified output file name will be used to generate file names for each part following the rule: outputFile_partIndex.extension.
If the output format is TIFF, the output will be saved as a single multi-frame TIFF file.
Examples
Shows how to replace text in the document using regular expression pattern.
int replacements = Replacer.Replace(MyDir + "Replacing.docx", ArtifactsDir + "Replacer.Replace3.docx", new Regex(@"[a-zA-Z]{6}\d+"), "Replacement Text");
See Also
- class Replacer
- namespace Wordize.Replacing
- assembly Wordize
Replace(string, string, SaveFormat, Regex, string, FindReplaceOptions)
Replaces all occurrences of a specified character string pattern with a replacement string.
public static int Replace(string inputFileName, string outputFileName, SaveFormat saveFormat,
Regex pattern, string replacement, FindReplaceOptions options = null)
Parameter | Type | Description |
---|---|---|
inputFileName | String | The input file name. |
outputFileName | String | The output file name. |
saveFormat | SaveFormat | The save format. |
pattern | Regex | A regular expression pattern used to find matches. |
replacement | String | A string to replace all occurrences of pattern. |
options | FindReplaceOptions | FindReplaceOptions object to specify additional options. |
Return Value
The number of replacements made.
Remarks
If the output format is an image (BMP, EMF, EPS, GIF, JPEG, PNG, or WebP), each page of the output will be saved as a separate file. The specified output file name will be used to generate file names for each part following the rule: outputFile_partIndex.extension.
If the output format is TIFF, the output will be saved as a single multi-frame TIFF file.
Examples
Shows how to replace text in the document using regular expression pattern with MD formatted text.
FindReplaceOptions options = new FindReplaceOptions();
options.ReplacementFormat = ReplacementFormat.Markdown;
Regex pattern = new Regex(@"[a-zA-Z]{6}\d+");
string mdReplacement = "**This** *Is* `Replacement`";
int replacements = Replacer.Replace(MyDir + "Replacing.docx",
ArtifactsDir + "Replacer.Replace4.docx",
SaveFormat.Docx,
pattern,
mdReplacement,
options);
See Also
- enum SaveFormat
- class FindReplaceOptions
- class Replacer
- namespace Wordize.Replacing
- assembly Wordize
Replace(Stream, Stream, SaveFormat, Regex, string, FindReplaceOptions)
Replaces all occurrences of a specified character string pattern with a replacement string.
public static int Replace(Stream inputStream, Stream outputStream, SaveFormat saveFormat,
Regex pattern, string replacement, FindReplaceOptions options = null)
Parameter | Type | Description |
---|---|---|
inputStream | Stream | The input stream. |
outputStream | Stream | The output stream. |
saveFormat | SaveFormat | The save format. |
pattern | Regex | A regular expression pattern used to find matches. |
replacement | String | A string to replace all occurrences of pattern. |
options | FindReplaceOptions | FindReplaceOptions object to specify additional options. |
Return Value
The number of replacements made.
Remarks
If the output format is an image (BMP, EMF, EPS, GIF, JPEG, PNG, or WebP), only the first page of the output will be saved to the specified stream.
If the output format is TIFF, the output will be saved as a single multi-frame TIFF to the specified stream.
Examples
Shows how to replace text in the document loaded from stream using regular expression pattern with HTML formatted text.
FindReplaceOptions options = new FindReplaceOptions();
options.ReplacementFormat = ReplacementFormat.Html;
Regex pattern = new Regex(@"[a-zA-Z]{6}\d+");
string htmlReplacement = "<b>This</b> <i>Is</i> <span style='color:green'>Replacement</span>";
using (Stream input = File.OpenRead(MyDir + "Replacing.docx"))
using (Stream output = File.Create(ArtifactsDir + "Replacer.Replace5.docx"))
{
int replacements = Replacer.Replace(input, output, SaveFormat.Docx, pattern, htmlReplacement, options);
}
See Also
- enum SaveFormat
- class FindReplaceOptions
- class Replacer
- namespace Wordize.Replacing
- assembly Wordize