public class Replacer extends Processor
mResultDocument| Modifier and Type | Method and Description |
|---|---|
protected void |
checkArgumentsSet() |
static Replacer |
create(ReplacerContext context)
Creates new instance of the replacer processor.
|
protected void |
executeCore() |
static int |
replace(java.io.InputStream inputStream,
java.io.OutputStream outputStream,
int saveFormat,
java.util.regex.Pattern pattern,
java.lang.String replacement) |
static int |
replace(java.io.InputStream inputStream,
java.io.OutputStream outputStream,
int saveFormat,
java.util.regex.Pattern pattern,
java.lang.String replacement,
FindReplaceOptions options) |
static int |
replace(java.io.InputStream inputStream,
java.io.OutputStream outputStream,
int saveFormat,
java.lang.String pattern,
java.lang.String replacement) |
static int |
replace(java.io.InputStream inputStream,
java.io.OutputStream outputStream,
int saveFormat,
java.lang.String pattern,
java.lang.String replacement,
FindReplaceOptions options) |
static int |
replace(java.io.InputStream inputStream,
java.io.OutputStream outputStream,
SaveOptions saveOptions,
java.util.regex.Pattern pattern,
java.lang.String replacement) |
static int |
replace(java.io.InputStream inputStream,
java.io.OutputStream outputStream,
SaveOptions saveOptions,
java.util.regex.Pattern pattern,
java.lang.String replacement,
FindReplaceOptions options) |
static int |
replace(java.io.InputStream inputStream,
java.io.OutputStream outputStream,
SaveOptions saveOptions,
java.lang.String pattern,
java.lang.String replacement) |
static int |
replace(java.io.InputStream inputStream,
java.io.OutputStream outputStream,
SaveOptions saveOptions,
java.lang.String pattern,
java.lang.String replacement,
FindReplaceOptions options) |
static int |
replace(java.lang.String inputFileName,
java.lang.String outputFileName,
int saveFormat,
java.util.regex.Pattern pattern,
java.lang.String replacement) |
static int |
replace(java.lang.String inputFileName,
java.lang.String outputFileName,
int saveFormat,
java.util.regex.Pattern pattern,
java.lang.String replacement,
FindReplaceOptions options) |
static int |
replace(java.lang.String inputFileName,
java.lang.String outputFileName,
int saveFormat,
java.lang.String pattern,
java.lang.String replacement) |
static int |
replace(java.lang.String inputFileName,
java.lang.String outputFileName,
int saveFormat,
java.lang.String pattern,
java.lang.String replacement,
FindReplaceOptions options) |
static int |
replace(java.lang.String inputFileName,
java.lang.String outputFileName,
java.util.regex.Pattern pattern,
java.lang.String replacement)
Replaces all occurrences of a specified character string pattern with a replacement string in the input file using a regular expression.
|
static int |
replace(java.lang.String inputFileName,
java.lang.String outputFileName,
SaveOptions saveOptions,
java.util.regex.Pattern pattern,
java.lang.String replacement) |
static int |
replace(java.lang.String inputFileName,
java.lang.String outputFileName,
SaveOptions saveOptions,
java.util.regex.Pattern pattern,
java.lang.String replacement,
FindReplaceOptions options)
Replaces all occurrences of a specified character string pattern with a replacement string in the input file using a regular expression, with the specified save format and additional options.
|
static int |
replace(java.lang.String inputFileName,
java.lang.String outputFileName,
SaveOptions saveOptions,
java.lang.String pattern,
java.lang.String replacement) |
static int |
replace(java.lang.String inputFileName,
java.lang.String outputFileName,
SaveOptions saveOptions,
java.lang.String pattern,
java.lang.String replacement,
FindReplaceOptions options)
Replaces all occurrences of a specified character string pattern with a replacement string in the input file, with the specified save format and additional options.
|
static int |
replace(java.lang.String inputFileName,
java.lang.String outputFileName,
java.lang.String pattern,
java.lang.String replacement)
Replaces all occurrences of a specified character string pattern with a replacement string in the input file.
|
static java.io.OutputStream[] |
replaceToImages(java.io.InputStream inputStream,
ImageSaveOptions saveOptions,
java.util.regex.Pattern pattern,
java.lang.String replacement) |
static java.io.OutputStream[] |
replaceToImages(java.io.InputStream inputStream,
ImageSaveOptions saveOptions,
java.util.regex.Pattern pattern,
java.lang.String replacement,
FindReplaceOptions options)
Replaces all occurrences of a specified regular expression pattern with a replacement string in the input file.
|
static java.io.OutputStream[] |
replaceToImages(java.io.InputStream inputStream,
ImageSaveOptions saveOptions,
java.lang.String pattern,
java.lang.String replacement) |
static java.io.OutputStream[] |
replaceToImages(java.io.InputStream inputStream,
ImageSaveOptions saveOptions,
java.lang.String pattern,
java.lang.String replacement,
FindReplaceOptions options)
Replaces all occurrences of a specified character string pattern with a replacement string in the input file.
|
static java.io.OutputStream[] |
replaceToImages(java.lang.String inputFileName,
ImageSaveOptions saveOptions,
java.util.regex.Pattern pattern,
java.lang.String replacement) |
static java.io.OutputStream[] |
replaceToImages(java.lang.String inputFileName,
ImageSaveOptions saveOptions,
java.util.regex.Pattern pattern,
java.lang.String replacement,
FindReplaceOptions options)
Replaces all occurrences of a specified regular expression pattern with a replacement string in the input file.
|
static java.io.OutputStream[] |
replaceToImages(java.lang.String inputFileName,
ImageSaveOptions saveOptions,
java.lang.String pattern,
java.lang.String replacement) |
static java.io.OutputStream[] |
replaceToImages(java.lang.String inputFileName,
ImageSaveOptions saveOptions,
java.lang.String pattern,
java.lang.String replacement,
FindReplaceOptions options)
Replaces all occurrences of a specified character string pattern with a replacement string in the input file.
|
public static Replacer create(ReplacerContext context)
Examples:
Shows how to replace string with regex in the document using context.
// There is a several ways to replace string with regex in the document:
String doc = getMyDir() + "Footer.docx";
Pattern pattern = Pattern.compile("gr(a|e)y");
String replacement = "lavender";
ReplacerContext replacerContext = new ReplacerContext();
replacerContext.setReplacement(pattern, replacement);
replacerContext.getFindReplaceOptions().setFindWholeWordsOnly(false);
Replacer.create(replacerContext)
.from(doc)
.to(getArtifactsDir() + "LowCode.ReplaceContextRegex.docx")
.execute();
Shows how to replace string in the document using context.
// 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.";
ReplacerContext replacerContext = new ReplacerContext();
replacerContext.setReplacement(pattern, replacement);
replacerContext.getFindReplaceOptions().setFindWholeWordsOnly(false);
Replacer.create(replacerContext)
.from(doc)
.to(getArtifactsDir() + "LowCode.ReplaceContext.docx")
.execute();
Shows how to replace string with regex in the document using documents from the stream using context.
// There is a several ways to replace string with regex in the document using documents from the stream:
Pattern pattern = Pattern.compile("gr(a|e)y");
String replacement = "lavender";
try (FileInputStream streamIn = new FileInputStream(getMyDir() + "Replace regex.docx")) {
ReplacerContext replacerContext = new ReplacerContext();
replacerContext.setReplacement(pattern, replacement);
replacerContext.getFindReplaceOptions().setFindWholeWordsOnly(false);
try (FileOutputStream streamOut = new FileOutputStream(getArtifactsDir() + "LowCode.ReplaceContextStreamRegex.docx")) {
Replacer.create(replacerContext)
.from(streamIn)
.to(streamOut, SaveFormat.DOCX)
.execute();
}
}
Shows how to replace string in the document using documents from the stream using context.
// There is a several ways to replace string in the document using documents from the stream:
String pattern = "(C)2006 Aspose Pty Ltd.";
String replacement = "Copyright (C) 2024 by Aspose Pty Ltd.";
try (FileInputStream streamIn = new FileInputStream(getMyDir() + "Footer.docx")) {
ReplacerContext replacerContext = new ReplacerContext();
replacerContext.setReplacement(pattern, replacement);
replacerContext.getFindReplaceOptions().setFindWholeWordsOnly(false);
try (FileOutputStream streamOut = new FileOutputStream(getArtifactsDir() + "LowCode.ReplaceContextStream.docx")) {
Replacer.create(replacerContext)
.from(streamIn)
.to(streamOut, SaveFormat.DOCX)
.execute();
}
}
protected void executeCore()
throws java.lang.Exception
executeCore in class Processorjava.lang.Exceptionprotected void checkArgumentsSet()
checkArgumentsSet in class Processorpublic static int replace(java.lang.String inputFileName,
java.lang.String outputFileName,
java.lang.String pattern,
java.lang.String replacement)
throws java.lang.Exception
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 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.";
FindReplaceOptions options = new FindReplaceOptions();
options.setFindWholeWordsOnly(false);
Replacer.replace(doc, getArtifactsDir() + "LowCode.Replace.1.docx", pattern, replacement);
Replacer.replace(doc, getArtifactsDir() + "LowCode.Replace.2.docx", SaveFormat.DOCX, pattern, replacement);
Replacer.replace(doc, getArtifactsDir() + "LowCode.Replace.3.docx", SaveFormat.DOCX, pattern, replacement, options);
inputFileName - The input file name.outputFileName - The output file name.pattern - A string to be replaced.replacement - A string to replace all occurrences of pattern.java.lang.Exceptionpublic static int replace(java.lang.String inputFileName,
java.lang.String outputFileName,
int saveFormat,
java.lang.String pattern,
java.lang.String replacement)
throws java.lang.Exception
java.lang.Exceptionpublic static int replace(java.lang.String inputFileName,
java.lang.String outputFileName,
int saveFormat,
java.lang.String pattern,
java.lang.String replacement,
FindReplaceOptions options)
throws java.lang.Exception
java.lang.Exceptionpublic static int replace(java.lang.String inputFileName,
java.lang.String outputFileName,
SaveOptions saveOptions,
java.lang.String pattern,
java.lang.String replacement)
throws java.lang.Exception
java.lang.Exceptionpublic static int replace(java.lang.String inputFileName,
java.lang.String outputFileName,
SaveOptions saveOptions,
java.lang.String pattern,
java.lang.String replacement,
FindReplaceOptions options)
throws java.lang.Exception
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.
inputFileName - The input file name.outputFileName - The output file name.saveOptions - The save options.pattern - A string to be replaced.replacement - A string to replace all occurrences of pattern.options - FindReplaceOptions object to specify additional options.java.lang.Exceptionpublic static int replace(java.io.InputStream inputStream,
java.io.OutputStream outputStream,
int saveFormat,
java.lang.String pattern,
java.lang.String replacement)
throws java.lang.Exception
java.lang.Exceptionpublic static int replace(java.io.InputStream inputStream,
java.io.OutputStream outputStream,
int saveFormat,
java.lang.String pattern,
java.lang.String replacement,
FindReplaceOptions options)
throws java.lang.Exception
java.lang.Exceptionpublic static int replace(java.io.InputStream inputStream,
java.io.OutputStream outputStream,
SaveOptions saveOptions,
java.lang.String pattern,
java.lang.String replacement)
throws java.lang.Exception
java.lang.Exceptionpublic static int replace(java.io.InputStream inputStream,
java.io.OutputStream outputStream,
SaveOptions saveOptions,
java.lang.String pattern,
java.lang.String replacement,
FindReplaceOptions options)
throws java.lang.Exception
java.lang.Exceptionpublic static int replace(java.lang.String inputFileName,
java.lang.String outputFileName,
java.util.regex.Pattern pattern,
java.lang.String replacement)
throws java.lang.Exception
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 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);
inputFileName - The input file name.outputFileName - The output file name.pattern - A regular expression pattern used to find matches.replacement - A string to replace all occurrences of pattern.java.lang.Exceptionpublic static int replace(java.lang.String inputFileName,
java.lang.String outputFileName,
int saveFormat,
java.util.regex.Pattern pattern,
java.lang.String replacement)
throws java.lang.Exception
java.lang.Exceptionpublic static int replace(java.lang.String inputFileName,
java.lang.String outputFileName,
int saveFormat,
java.util.regex.Pattern pattern,
java.lang.String replacement,
FindReplaceOptions options)
throws java.lang.Exception
java.lang.Exceptionpublic static int replace(java.lang.String inputFileName,
java.lang.String outputFileName,
SaveOptions saveOptions,
java.util.regex.Pattern pattern,
java.lang.String replacement)
throws java.lang.Exception
java.lang.Exceptionpublic static int replace(java.lang.String inputFileName,
java.lang.String outputFileName,
SaveOptions saveOptions,
java.util.regex.Pattern pattern,
java.lang.String replacement,
FindReplaceOptions options)
throws java.lang.Exception
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.
inputFileName - The input file name.outputFileName - The output file name.saveOptions - The save options.pattern - A regular expression pattern used to find matches.replacement - A string to replace all occurrences of pattern.options - FindReplaceOptions object to specify additional options.java.lang.Exceptionpublic static int replace(java.io.InputStream inputStream,
java.io.OutputStream outputStream,
int saveFormat,
java.util.regex.Pattern pattern,
java.lang.String replacement)
throws java.lang.Exception
java.lang.Exceptionpublic static int replace(java.io.InputStream inputStream,
java.io.OutputStream outputStream,
int saveFormat,
java.util.regex.Pattern pattern,
java.lang.String replacement,
FindReplaceOptions options)
throws java.lang.Exception
java.lang.Exceptionpublic static int replace(java.io.InputStream inputStream,
java.io.OutputStream outputStream,
SaveOptions saveOptions,
java.util.regex.Pattern pattern,
java.lang.String replacement)
throws java.lang.Exception
java.lang.Exceptionpublic static int replace(java.io.InputStream inputStream,
java.io.OutputStream outputStream,
SaveOptions saveOptions,
java.util.regex.Pattern pattern,
java.lang.String replacement,
FindReplaceOptions options)
throws java.lang.Exception
java.lang.Exceptionpublic static java.io.OutputStream[] replaceToImages(java.lang.String inputFileName,
ImageSaveOptions saveOptions,
java.lang.String pattern,
java.lang.String replacement)
throws java.lang.Exception
java.lang.Exceptionpublic static java.io.OutputStream[] replaceToImages(java.lang.String inputFileName,
ImageSaveOptions saveOptions,
java.lang.String pattern,
java.lang.String replacement,
FindReplaceOptions options)
throws java.lang.Exception
Examples:
Shows how to replace string in the document and save result to images.
// 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.";
OutputStream[] images = Replacer.replaceToImages(doc, new ImageSaveOptions(SaveFormat.PNG), pattern, replacement);
FindReplaceOptions options = new FindReplaceOptions();
options.setFindWholeWordsOnly(false);
images = Replacer.replaceToImages(doc, new ImageSaveOptions(SaveFormat.PNG), pattern, replacement, options);
inputFileName - The input file name.saveOptions - The save options.pattern - A string to be replaced.replacement - A string to replace all occurrences of pattern.options - FindReplaceOptions object to specify additional options.java.lang.Exceptionpublic static java.io.OutputStream[] replaceToImages(java.io.InputStream inputStream,
ImageSaveOptions saveOptions,
java.lang.String pattern,
java.lang.String replacement)
throws java.lang.Exception
java.lang.Exceptionpublic static java.io.OutputStream[] replaceToImages(java.io.InputStream inputStream,
ImageSaveOptions saveOptions,
java.lang.String pattern,
java.lang.String replacement,
FindReplaceOptions options)
throws java.lang.Exception
Examples:
Shows how to replace string in the document using documents from the stream and save result to images.
// There is a several ways to replace string in the document using documents from the stream:
String pattern = "(C)2006 Aspose Pty Ltd.";
String replacement = "Copyright (C) 2024 by Aspose Pty Ltd.";
try (FileInputStream streamIn = new FileInputStream(getMyDir() + "Footer.docx")) {
OutputStream[] images = Replacer.replaceToImages(streamIn, new ImageSaveOptions(SaveFormat.PNG), pattern, replacement);
FindReplaceOptions options = new FindReplaceOptions();
options.setFindWholeWordsOnly(false);
images = Replacer.replaceToImages(streamIn, new ImageSaveOptions(SaveFormat.PNG), pattern, replacement, options);
}
inputStream - The input file stream.saveOptions - The save options.pattern - A string to be replaced.replacement - A string to replace all occurrences of pattern.options - FindReplaceOptions object to specify additional options.java.lang.Exceptionpublic static java.io.OutputStream[] replaceToImages(java.lang.String inputFileName,
ImageSaveOptions saveOptions,
java.util.regex.Pattern pattern,
java.lang.String replacement)
throws java.lang.Exception
java.lang.Exceptionpublic static java.io.OutputStream[] replaceToImages(java.lang.String inputFileName,
ImageSaveOptions saveOptions,
java.util.regex.Pattern pattern,
java.lang.String replacement,
FindReplaceOptions options)
throws java.lang.Exception
Examples:
Shows how to replace string with regex in the document and save result to images.
// There is a several ways to replace string with regex in the document:
String doc = getMyDir() + "Footer.docx";
Pattern pattern = Pattern.compile("gr(a|e)y");
String replacement = "lavender";
OutputStream[] images = Replacer.replaceToImages(doc, new ImageSaveOptions(SaveFormat.PNG), pattern, replacement);
FindReplaceOptions options = new FindReplaceOptions();
options.setFindWholeWordsOnly(false);
images = Replacer.replaceToImages(doc, new ImageSaveOptions(SaveFormat.PNG), pattern, replacement, options);
inputFileName - The input file name.saveOptions - The save options.pattern - A regular expression pattern used to find matches.replacement - A string to replace all occurrences of pattern.options - FindReplaceOptions object to specify additional options.java.lang.Exceptionpublic static java.io.OutputStream[] replaceToImages(java.io.InputStream inputStream,
ImageSaveOptions saveOptions,
java.util.regex.Pattern pattern,
java.lang.String replacement)
throws java.lang.Exception
java.lang.Exceptionpublic static java.io.OutputStream[] replaceToImages(java.io.InputStream inputStream,
ImageSaveOptions saveOptions,
java.util.regex.Pattern pattern,
java.lang.String replacement,
FindReplaceOptions options)
throws java.lang.Exception
Examples:
Shows how to replace string with regex in the document using documents from the stream and save result to images.
// There is a several ways to replace string with regex in the document using documents from the stream:
Pattern pattern = Pattern.compile("gr(a|e)y");
String replacement = "lavender";
try (FileInputStream streamIn = new FileInputStream(getMyDir() + "Replace regex.docx")) {
OutputStream[] images = Replacer.replaceToImages(streamIn, new ImageSaveOptions(SaveFormat.PNG), pattern, replacement);
FindReplaceOptions options = new FindReplaceOptions();
options.setFindWholeWordsOnly(false);
images = Replacer.replaceToImages(streamIn, new ImageSaveOptions(SaveFormat.PNG), pattern, replacement, options);
}
inputStream - The input file stream.saveOptions - The save options.pattern - A regular expression pattern used to find matches.replacement - A string to replace all occurrences of pattern.options - FindReplaceOptions object to specify additional options.java.lang.Exception