public class ReplacerContext extends ProcessorContext
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();
}
}
| Constructor and Description |
|---|
ReplacerContext()
Initializes a new instance of this class.
|
| Modifier and Type | Method and Description |
|---|---|
FindReplaceOptions |
getFindReplaceOptions()
Find/replace options.
|
void |
setReplacement(java.util.regex.Pattern pattern,
java.lang.String replacement)
Sets pattern and replacement used by find/replace operation.
|
void |
setReplacement(java.lang.String pattern,
java.lang.String replacement)
Sets pattern and replacement used by find/replace operation.
|
getFontSettings, getLayoutOptions, getWarningCallback, setFontSettings, setWarningCallbackpublic ReplacerContext()
public FindReplaceOptions getFindReplaceOptions()
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();
}
}
FindReplaceOptions value.public void setReplacement(java.lang.String pattern,
java.lang.String replacement)
Remarks:
Using this method overrides previously set pattern and replacement.
Examples:
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 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();
}
}
public void setReplacement(java.util.regex.Pattern pattern,
java.lang.String replacement)
Remarks:
Using this method overrides previously set pattern and replacement.
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 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();
}
}