java.lang.Object
com.aspose.words.FindReplaceOptions
public class FindReplaceOptions
Example:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Hello _CustomerName_,");
// Check the document contains what we are about to test
System.out.println(doc.getFirstSection().getBody().getParagraphs().get(0).getText());
FindReplaceOptions options = new FindReplaceOptions();
options.setMatchCase(false);
options.setFindWholeWordsOnly(false);
doc.getRange().replace("_CustomerName_", "James Bond", options);
doc.save(getArtifactsDir() + "Range.ReplaceSimple.docx");
| Constructor Summary |
|---|
FindReplaceOptions()
|
FindReplaceOptions(int direction)
|
FindReplaceOptions(IReplacingCallback replacingCallback)
|
FindReplaceOptions(int direction, IReplacingCallback replacingCallback)
|
| Property Getters/Setters Summary | ||
|---|---|---|
Font | getApplyFont() | |
| Text formatting applied to new content. | ||
ParagraphFormat | getApplyParagraphFormat() | |
| Paragraph formatting applied to new content. | ||
int | getDirection() | |
void | setDirection(int value) | |
|
Selects direction for replace. Default value is |
||
boolean | getFindWholeWordsOnly() | |
void | setFindWholeWordsOnly(boolean value) | |
| True indicates the oldValue must be a standalone word. | ||
boolean | getIgnoreDeleted() | |
void | setIgnoreDeleted(boolean value) | |
Gets or sets a boolean value indicating either to ignore text inside delete revisions.
The default value is false.
|
||
boolean | getIgnoreFields() | |
void | setIgnoreFields(boolean value) | |
Gets or sets a boolean value indicating either to ignore text inside fields.
The default value is false.
|
||
boolean | getIgnoreInserted() | |
void | setIgnoreInserted(boolean value) | |
Gets or sets a boolean value indicating either to ignore text inside insert revisions.
The default value is false.
|
||
boolean | getLegacyMode() | |
void | setLegacyMode(boolean value) | |
| Gets or sets a boolean value indicating that old find/replace algorithm is used. | ||
boolean | getMatchCase() | |
void | setMatchCase(boolean value) | |
| True indicates case-sensitive comparison, false indicates case-insensitive comparison. | ||
IReplacingCallback | getReplacingCallback() | |
void | setReplacingCallback(IReplacingCallback value) | |
| The user-defined method which is called before every replace occurrence. | ||
boolean | getUseLegacyOrder() | |
void | setUseLegacyOrder(boolean value) | |
| True indicates that a text search is performed sequentially from top to bottom considering the text boxes. Default value is false. | ||
boolean | getUseSubstitutions() | |
void | setUseSubstitutions(boolean value) | |
Gets or sets a boolean value indicating whether to recognize and use substitutions within replacement patterns.
The default value is false.
|
||
| Constructor Detail |
|---|
public FindReplaceOptions()
public FindReplaceOptions(int direction)
direction - A FindReplaceDirection value.public FindReplaceOptions(IReplacingCallback replacingCallback)
public FindReplaceOptions(int direction, IReplacingCallback replacingCallback)
direction - A FindReplaceDirection value.| Property Getters/Setters Detail |
|---|
getApplyFont | |
public Font getApplyFont() | |
Example:
Shows how to apply a different font to new content via FindReplaceOptions.
public void replaceNumbersAsHex() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getFont().setName("Arial");
builder.write("There are few numbers that should be converted to HEX and highlighted: 123, 456, 789 and 17379.");
FindReplaceOptions options = new FindReplaceOptions();
// Highlight newly inserted content with a color
options.getApplyFont().setHighlightColor(new Color(255, 140, 0));
// Apply an IReplacingCallback to make the replacement to convert integers into hex equivalents
// and also to count replacements in the order they take place
options.setReplacingCallback(new NumberHexer());
// By default, text is searched for replacements front to back, but we can change it to go the other way
options.setDirection(FindReplaceDirection.BACKWARD);
int count = doc.getRange().replace(Pattern.compile("[0-9]+"), "", options);
Assert.assertEquals(4, count);
Assert.assertEquals("There are few numbers that should be converted to HEX and highlighted:" +
" 0x7b (replacement #4), 0x1c8 (replacement #3), 0x315 (replacement #2) and 0x43e3 (replacement #1).",
doc.getText().trim());
}
/// <summary>
/// Replaces arabic numbers with hexadecimal equivalents and appends the number of each replacement.
/// </summary>
private static class NumberHexer implements IReplacingCallback {
public int replacing(ReplacingArgs args) {
mCurrentReplacementNumber++;
// Parse numbers
String numberStr = args.getMatch().group();
numberStr = numberStr.trim();
// Java throws NumberFormatException both for overflow and bad format
int number = Integer.parseInt(numberStr);
// And write it as HEX
args.setReplacement(MessageFormat.format("0x{0} (replacement #{1})", Integer.toHexString(number), mCurrentReplacementNumber));
System.out.println(MessageFormat.format("Match #{0}", mCurrentReplacementNumber));
System.out.println(MessageFormat.format("\tOriginal value:\t{0}", args.getMatch().group()));
System.out.println(MessageFormat.format("\tReplacement:\t{0}", args.getReplacement()));
System.out.println(MessageFormat.format("\tOffset in parent {0} node:\t{1}", args.getMatchNode().getNodeType(), args.getMatchOffset()));
return ReplaceAction.REPLACE;
}
private int mCurrentReplacementNumber;
}getApplyParagraphFormat | |
public ParagraphFormat getApplyParagraphFormat() | |
Example:
Shows how to affect the format of paragraphs with successful replacements.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Every paragraph that ends with a full stop like this one will be right aligned.");
builder.writeln("This one will not!");
builder.writeln("And this one will.");
FindReplaceOptions options = new FindReplaceOptions();
options.getApplyParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);
int count = doc.getRange().replace(".&p", "!&p", options);
Assert.assertEquals(count, 2);
doc.save(getArtifactsDir() + "Range.ApplyParagraphFormat.docx");getDirection/setDirection | |
public int getDirection() / public void setDirection(int value) | |
Example:
Shows how to apply a different font to new content via FindReplaceOptions.
public void replaceNumbersAsHex() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getFont().setName("Arial");
builder.write("There are few numbers that should be converted to HEX and highlighted: 123, 456, 789 and 17379.");
FindReplaceOptions options = new FindReplaceOptions();
// Highlight newly inserted content with a color
options.getApplyFont().setHighlightColor(new Color(255, 140, 0));
// Apply an IReplacingCallback to make the replacement to convert integers into hex equivalents
// and also to count replacements in the order they take place
options.setReplacingCallback(new NumberHexer());
// By default, text is searched for replacements front to back, but we can change it to go the other way
options.setDirection(FindReplaceDirection.BACKWARD);
int count = doc.getRange().replace(Pattern.compile("[0-9]+"), "", options);
Assert.assertEquals(4, count);
Assert.assertEquals("There are few numbers that should be converted to HEX and highlighted:" +
" 0x7b (replacement #4), 0x1c8 (replacement #3), 0x315 (replacement #2) and 0x43e3 (replacement #1).",
doc.getText().trim());
}
/// <summary>
/// Replaces arabic numbers with hexadecimal equivalents and appends the number of each replacement.
/// </summary>
private static class NumberHexer implements IReplacingCallback {
public int replacing(ReplacingArgs args) {
mCurrentReplacementNumber++;
// Parse numbers
String numberStr = args.getMatch().group();
numberStr = numberStr.trim();
// Java throws NumberFormatException both for overflow and bad format
int number = Integer.parseInt(numberStr);
// And write it as HEX
args.setReplacement(MessageFormat.format("0x{0} (replacement #{1})", Integer.toHexString(number), mCurrentReplacementNumber));
System.out.println(MessageFormat.format("Match #{0}", mCurrentReplacementNumber));
System.out.println(MessageFormat.format("\tOriginal value:\t{0}", args.getMatch().group()));
System.out.println(MessageFormat.format("\tReplacement:\t{0}", args.getReplacement()));
System.out.println(MessageFormat.format("\tOffset in parent {0} node:\t{1}", args.getMatchNode().getNodeType(), args.getMatchOffset()));
return ReplaceAction.REPLACE;
}
private int mCurrentReplacementNumber;
}getFindWholeWordsOnly/setFindWholeWordsOnly | |
public boolean getFindWholeWordsOnly() / public void setFindWholeWordsOnly(boolean value) | |
Example:
Simple find and replace operation.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Hello _CustomerName_,");
// Check the document contains what we are about to test
System.out.println(doc.getFirstSection().getBody().getParagraphs().get(0).getText());
FindReplaceOptions options = new FindReplaceOptions();
options.setMatchCase(false);
options.setFindWholeWordsOnly(false);
doc.getRange().replace("_CustomerName_", "James Bond", options);
doc.save(getArtifactsDir() + "Range.ReplaceSimple.docx");getIgnoreDeleted/setIgnoreDeleted | |
public boolean getIgnoreDeleted() / public void setIgnoreDeleted(boolean value) | |
false.
Example:
Shows how to ignore text inside delete revisions.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert non-revised text
builder.writeln("Deleted");
builder.write("Text");
// Remove first paragraph with tracking revisions
doc.startTrackRevisions("John Doe", new Date());
doc.getFirstSection().getBody().getFirstParagraph().remove();
doc.stopTrackRevisions();
FindReplaceOptions options = new FindReplaceOptions();
// Replace 'e' in document ignoring deleted text
options.setIgnoreDeleted(true);
doc.getRange().replace("e", "*", options);
Assert.assertEquals(doc.getText(), "Deleted\rT*xt\f");
// Replace 'e' in document NOT ignoring deleted text
options.setIgnoreDeleted(false);
doc.getRange().replace("e", "*", options);
Assert.assertEquals(doc.getText(), "D*l*t*d\rT*xt\f");getIgnoreFields/setIgnoreFields | |
public boolean getIgnoreFields() / public void setIgnoreFields(boolean value) | |
false.
Example:
Shows how to ignore text inside fields.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert field with text inside
builder.insertField("INCLUDETEXT", "Text in field");
FindReplaceOptions options = new FindReplaceOptions();
// Replace 'e' in document ignoring text inside field
options.setIgnoreFields(true);
doc.getRange().replace(Pattern.compile("e"), "*", options);
Assert.assertEquals(doc.getText(), "\u0013INCLUDETEXT\u0014Text in field\u0015\f");
// Replace 'e' in document NOT ignoring text inside field
options.setIgnoreFields(false);
doc.getRange().replace(Pattern.compile("e"), "*", options);
Assert.assertEquals(doc.getText(), "\u0013INCLUDETEXT\u0014T*xt in fi*ld\u0015\f");getIgnoreInserted/setIgnoreInserted | |
public boolean getIgnoreInserted() / public void setIgnoreInserted(boolean value) | |
false.
Example:
Shows how to ignore text inside insert revisions.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert text with tracking revisions
doc.startTrackRevisions("John Doe", new Date());
builder.writeln("Inserted");
doc.stopTrackRevisions();
// Insert non-revised text
builder.write("Text");
FindReplaceOptions options = new FindReplaceOptions();
// Replace 'e' in document ignoring inserted text
options.setIgnoreInserted(true);
doc.getRange().replace("e", "*", options);
Assert.assertEquals(doc.getText(), "Inserted\rT*xt\f");
// Replace 'e' in document NOT ignoring inserted text
options.setIgnoreInserted(false);
doc.getRange().replace("e", "*", options);
Assert.assertEquals(doc.getText(), "Ins*rt*d\rT*xt\f");getLegacyMode/setLegacyMode | |
public boolean getLegacyMode() / public void setLegacyMode(boolean value) | |
getMatchCase/setMatchCase | |
public boolean getMatchCase() / public void setMatchCase(boolean value) | |
Example:
Simple find and replace operation.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Hello _CustomerName_,");
// Check the document contains what we are about to test
System.out.println(doc.getFirstSection().getBody().getParagraphs().get(0).getText());
FindReplaceOptions options = new FindReplaceOptions();
options.setMatchCase(false);
options.setFindWholeWordsOnly(false);
doc.getRange().replace("_CustomerName_", "James Bond", options);
doc.save(getArtifactsDir() + "Range.ReplaceSimple.docx");getReplacingCallback/setReplacingCallback | |
public IReplacingCallback getReplacingCallback() / public void setReplacingCallback(IReplacingCallback value) | |
Example:
Shows how to apply a different font to new content via FindReplaceOptions.
public void replaceNumbersAsHex() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getFont().setName("Arial");
builder.write("There are few numbers that should be converted to HEX and highlighted: 123, 456, 789 and 17379.");
FindReplaceOptions options = new FindReplaceOptions();
// Highlight newly inserted content with a color
options.getApplyFont().setHighlightColor(new Color(255, 140, 0));
// Apply an IReplacingCallback to make the replacement to convert integers into hex equivalents
// and also to count replacements in the order they take place
options.setReplacingCallback(new NumberHexer());
// By default, text is searched for replacements front to back, but we can change it to go the other way
options.setDirection(FindReplaceDirection.BACKWARD);
int count = doc.getRange().replace(Pattern.compile("[0-9]+"), "", options);
Assert.assertEquals(4, count);
Assert.assertEquals("There are few numbers that should be converted to HEX and highlighted:" +
" 0x7b (replacement #4), 0x1c8 (replacement #3), 0x315 (replacement #2) and 0x43e3 (replacement #1).",
doc.getText().trim());
}
/// <summary>
/// Replaces arabic numbers with hexadecimal equivalents and appends the number of each replacement.
/// </summary>
private static class NumberHexer implements IReplacingCallback {
public int replacing(ReplacingArgs args) {
mCurrentReplacementNumber++;
// Parse numbers
String numberStr = args.getMatch().group();
numberStr = numberStr.trim();
// Java throws NumberFormatException both for overflow and bad format
int number = Integer.parseInt(numberStr);
// And write it as HEX
args.setReplacement(MessageFormat.format("0x{0} (replacement #{1})", Integer.toHexString(number), mCurrentReplacementNumber));
System.out.println(MessageFormat.format("Match #{0}", mCurrentReplacementNumber));
System.out.println(MessageFormat.format("\tOriginal value:\t{0}", args.getMatch().group()));
System.out.println(MessageFormat.format("\tReplacement:\t{0}", args.getReplacement()));
System.out.println(MessageFormat.format("\tOffset in parent {0} node:\t{1}", args.getMatchNode().getNodeType(), args.getMatchOffset()));
return ReplaceAction.REPLACE;
}
private int mCurrentReplacementNumber;
}getUseLegacyOrder/setUseLegacyOrder | |
public boolean getUseLegacyOrder() / public void setUseLegacyOrder(boolean value) | |
getUseSubstitutions/setUseSubstitutions | |
public boolean getUseSubstitutions() / public void setUseSubstitutions(boolean value) | |
false.
Example:
Shows how to replace text with substitutions.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("John sold a car to Paul.");
builder.writeln("Jane sold a house to Joe.");
// Perform a find-and-replace operation on a range's text contents
// while preserving some elements from the replaced text using substitutions.
FindReplaceOptions options = new FindReplaceOptions();
options.setUseSubstitutions(true);
doc.getRange().replace("[a-zA-Z] sold a [a-zA-Z] to [a-zA-Z]", "$3 bought a $2 from $1", options);
Assert.assertEquals(doc.getText(), "Paul bought a car from John.\rJoe bought a house from Jane.\r\f");