Replacer.Replace

Replacer Replace method. Replaces all occurrences of a specified character string pattern with a replacement string.

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)
ParameterTypeDescription
inputFileNameStringThe input file name.
outputFileNameStringThe output file name.
patternStringA string to be replaced.
replacementStringA 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


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)
ParameterTypeDescription
inputFileNameStringThe input file name.
outputFileNameStringThe output file name.
saveFormatSaveFormatThe save format.
patternStringA string to be replaced.
replacementStringA string to replace all occurrences of pattern.
optionsFindReplaceOptionsFindReplaceOptions 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


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)
ParameterTypeDescription
inputStreamStreamThe input stream.
outputStreamStreamThe output stream.
saveFormatSaveFormatThe save format.
patternStringA string to be replaced.
replacementStringA string to replace all occurrences of pattern.
optionsFindReplaceOptionsFindReplaceOptions 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


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)
ParameterTypeDescription
inputFileNameStringThe input file name.
outputFileNameStringThe output file name.
patternRegexA regular expression pattern used to find matches.
replacementStringA 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


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)
ParameterTypeDescription
inputFileNameStringThe input file name.
outputFileNameStringThe output file name.
saveFormatSaveFormatThe save format.
patternRegexA regular expression pattern used to find matches.
replacementStringA string to replace all occurrences of pattern.
optionsFindReplaceOptionsFindReplaceOptions 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


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)
ParameterTypeDescription
inputStreamStreamThe input stream.
outputStreamStreamThe output stream.
saveFormatSaveFormatThe save format.
patternRegexA regular expression pattern used to find matches.
replacementStringA string to replace all occurrences of pattern.
optionsFindReplaceOptionsFindReplaceOptions 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