java.lang.Object
com.aspose.words.FindReplaceOptions
public class FindReplaceOptions
Example:
// Open the document.
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);
// Replace the text in the document.
doc.getRange().replace("_CustomerName_", "James Bond", options);
// Save the modified document.
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 | getMatchCase() | |
void | setMatchCase(boolean value) | |
| True indicates case-sensitive comparison, false indicates case-insensitive comparison. | ||
boolean | getPreserveMetaCharacters() | |
void | setPreserveMetaCharacters(boolean value) | |
| True indicates that meta-characters beginning with "&" are preserved. Default value is false. | ||
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. | ||
| 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(count, 4);
doc.save(getArtifactsDir() + "Range.ReplaceNumbersAsHex.docx");
}
/// <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(count, 4);
doc.save(getArtifactsDir() + "Range.ReplaceNumbersAsHex.docx");
}
/// <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.
// Open the document.
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);
// Replace the text in the document.
doc.getRange().replace("_CustomerName_", "James Bond", options);
// Save the modified document.
doc.save(getArtifactsDir() + "Range.ReplaceSimple.docx");getMatchCase/setMatchCase | |
public boolean getMatchCase() / public void setMatchCase(boolean value) | |
Example:
Simple find and replace operation.
// Open the document.
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);
// Replace the text in the document.
doc.getRange().replace("_CustomerName_", "James Bond", options);
// Save the modified document.
doc.save(getArtifactsDir() + "Range.ReplaceSimple.docx");getPreserveMetaCharacters/setPreserveMetaCharacters | |
public boolean getPreserveMetaCharacters() / public void setPreserveMetaCharacters(boolean value) | |
Example:
Shows how to preserved meta-characters that beginning with "&".
Document doc = new Document(getMyDir() + "Range.FindAndReplaceWithPreserveMetaCharacters.docx");
FindReplaceOptions options = new FindReplaceOptions();
options.setFindWholeWordsOnly(true);
options.setPreserveMetaCharacters(true);
doc.getRange().replace("sad", "“ some text ”", options);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(count, 4);
doc.save(getArtifactsDir() + "Range.ReplaceNumbersAsHex.docx");
}
/// <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) | |