Replacer

Inheritance: java.lang.Object

public class Replacer

Provides methods intended to find and replace text in the document.

Methods

MethodDescription
replace(InputStream inputStream, OutputStream outputStream, int saveFormat, String pattern, String replacement)
replace(InputStream inputStream, OutputStream outputStream, int saveFormat, String pattern, String replacement, FindReplaceOptions options)
replace(InputStream inputStream, OutputStream outputStream, int saveFormat, Pattern pattern, String replacement)
replace(InputStream inputStream, OutputStream outputStream, int saveFormat, Pattern pattern, String replacement, FindReplaceOptions options)
replace(String inputFileName, String outputFileName, int saveFormat, String pattern, String replacement)
replace(String inputFileName, String outputFileName, int saveFormat, String pattern, String replacement, FindReplaceOptions options)
replace(String inputFileName, String outputFileName, int saveFormat, Pattern pattern, String replacement)
replace(String inputFileName, String outputFileName, int saveFormat, Pattern pattern, String replacement, FindReplaceOptions options)
replace(String inputFileName, String outputFileName, String pattern, String replacement)Replaces all occurrences of a specified character string pattern with a replacement string in the input file.
replace(String inputFileName, String outputFileName, Pattern pattern, String replacement)Replaces all occurrences of a specified character string pattern with a replacement string in the input file using a regular expression.

replace(InputStream inputStream, OutputStream outputStream, int saveFormat, String pattern, String replacement)

public static int replace(InputStream inputStream, OutputStream outputStream, int saveFormat, String pattern, String replacement)

Parameters:

ParameterTypeDescription
inputStreamjava.io.InputStream
outputStreamjava.io.OutputStream
saveFormatint
patternjava.lang.String
replacementjava.lang.String

Returns: int

replace(InputStream inputStream, OutputStream outputStream, int saveFormat, String pattern, String replacement, FindReplaceOptions options)

public static int replace(InputStream inputStream, OutputStream outputStream, int saveFormat, String pattern, String replacement, FindReplaceOptions options)

Parameters:

ParameterTypeDescription
inputStreamjava.io.InputStream
outputStreamjava.io.OutputStream
saveFormatint
patternjava.lang.String
replacementjava.lang.String
optionsFindReplaceOptions

Returns: int

replace(InputStream inputStream, OutputStream outputStream, int saveFormat, Pattern pattern, String replacement)

public static int replace(InputStream inputStream, OutputStream outputStream, int saveFormat, Pattern pattern, String replacement)

Parameters:

ParameterTypeDescription
inputStreamjava.io.InputStream
outputStreamjava.io.OutputStream
saveFormatint
patternjava.util.regex.Pattern
replacementjava.lang.String

Returns: int

replace(InputStream inputStream, OutputStream outputStream, int saveFormat, Pattern pattern, String replacement, FindReplaceOptions options)

public static int replace(InputStream inputStream, OutputStream outputStream, int saveFormat, Pattern pattern, String replacement, FindReplaceOptions options)

Parameters:

ParameterTypeDescription
inputStreamjava.io.InputStream
outputStreamjava.io.OutputStream
saveFormatint
patternjava.util.regex.Pattern
replacementjava.lang.String
optionsFindReplaceOptions

Returns: int

replace(String inputFileName, String outputFileName, int saveFormat, String pattern, String replacement)

public static int replace(String inputFileName, String outputFileName, int saveFormat, String pattern, String replacement)

Parameters:

ParameterTypeDescription
inputFileNamejava.lang.String
outputFileNamejava.lang.String
saveFormatint
patternjava.lang.String
replacementjava.lang.String

Returns: int

replace(String inputFileName, String outputFileName, int saveFormat, String pattern, String replacement, FindReplaceOptions options)

public static int replace(String inputFileName, String outputFileName, int saveFormat, String pattern, String replacement, FindReplaceOptions options)

Parameters:

ParameterTypeDescription
inputFileNamejava.lang.String
outputFileNamejava.lang.String
saveFormatint
patternjava.lang.String
replacementjava.lang.String
optionsFindReplaceOptions

Returns: int

replace(String inputFileName, String outputFileName, int saveFormat, Pattern pattern, String replacement)

public static int replace(String inputFileName, String outputFileName, int saveFormat, Pattern pattern, String replacement)

Parameters:

ParameterTypeDescription
inputFileNamejava.lang.String
outputFileNamejava.lang.String
saveFormatint
patternjava.util.regex.Pattern
replacementjava.lang.String

Returns: int

replace(String inputFileName, String outputFileName, int saveFormat, Pattern pattern, String replacement, FindReplaceOptions options)

public static int replace(String inputFileName, String outputFileName, int saveFormat, Pattern pattern, String replacement, FindReplaceOptions options)

Parameters:

ParameterTypeDescription
inputFileNamejava.lang.String
outputFileNamejava.lang.String
saveFormatint
patternjava.util.regex.Pattern
replacementjava.lang.String
optionsFindReplaceOptions

Returns: int

replace(String inputFileName, String outputFileName, String pattern, String replacement)

public static int replace(String inputFileName, String outputFileName, String pattern, String replacement)

Replaces all occurrences of a specified character string pattern with a replacement string in the input file.

Examples:

Shows how to replace string in the document.


 // There is a several ways to replace string in the document:
 String doc = getMyDir() + "Footer.docx";
 String pattern = "(C)2006 Aspose Pty Ltd.";
 String replacement = "Copyright (C) 2024 by Aspose Pty Ltd.";

 Replacer.replace(doc, getArtifactsDir() + "LowCode.Replace.1.docx", pattern, replacement);
 Replacer.replace(doc, getArtifactsDir() + "LowCode.Replace.2.docx", SaveFormat.DOCX, pattern, replacement);
 FindReplaceOptions options = new FindReplaceOptions();
 options.setFindWholeWordsOnly(false);
 Replacer.replace(doc, getArtifactsDir() + "LowCode.Replace.3.docx", SaveFormat.DOCX, pattern, replacement, options);
 

Parameters:

ParameterTypeDescription
inputFileNamejava.lang.StringThe input file name.
outputFileNamejava.lang.StringThe output file name.
patternjava.lang.StringA string to be replaced.
replacementjava.lang.StringA string to replace all occurrences of pattern.

Returns: int - The number of replacements made.

replace(String inputFileName, String outputFileName, Pattern pattern, String replacement)

public static int replace(String inputFileName, String outputFileName, Pattern pattern, String replacement)

Replaces all occurrences of a specified character string pattern with a replacement string in the input file using a regular expression.

Examples:

Shows how to replace string with regex in the document.


 // There is a several ways to replace string with regex in the document:
 String doc = getMyDir() + "Footer.docx";
 String pattern = "gr(a|e)y";
 String replacement = "lavender";

 Replacer.replace(doc, getArtifactsDir() + "LowCode.ReplaceRegex.1.docx", pattern, replacement);
 Replacer.replace(doc, getArtifactsDir() + "LowCode.ReplaceRegex.2.docx", SaveFormat.DOCX, pattern, replacement);
 FindReplaceOptions options = new FindReplaceOptions();
 options.setFindWholeWordsOnly(false);
 Replacer.replace(doc, getArtifactsDir() + "LowCode.ReplaceRegex.3.docx", SaveFormat.DOCX, pattern, replacement, options);
 

Parameters:

ParameterTypeDescription
inputFileNamejava.lang.StringThe input file name.
outputFileNamejava.lang.StringThe output file name.
patternjava.util.regex.PatternA regular expression pattern used to find matches.
replacementjava.lang.StringA string to replace all occurrences of pattern.

Returns: int - The number of replacements made.