com.aspose.words

Class FindReplaceOptions

  • java.lang.Object
    • com.aspose.words.FindReplaceOptions
public class FindReplaceOptions 
extends java.lang.Object

Specifies options for find/replace operations.

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");

Constructor Summary
FindReplaceOptions(intdirection)
FindReplaceOptions(intdirection, IReplacingCallback replacingCallback)
 
Property Getters/Setters Summary
FontgetApplyFont()
Text formatting applied to new content.
ParagraphFormatgetApplyParagraphFormat()
Paragraph formatting applied to new content.
intgetDirection()
void
setDirection(intvalue)
           Selects direction for replace. Default value is FindReplaceDirection.FORWARD. The value of the property is FindReplaceDirection integer constant.
booleangetFindWholeWordsOnly()
void
setFindWholeWordsOnly(booleanvalue)
           True indicates the oldValue must be a standalone word.
booleangetIgnoreDeleted()
void
setIgnoreDeleted(booleanvalue)
           Gets or sets a boolean value indicating either to ignore text inside delete revisions. The default value is false.
booleangetIgnoreFields()
void
setIgnoreFields(booleanvalue)
           Gets or sets a boolean value indicating either to ignore text inside fields. The default value is false.
booleangetIgnoreInserted()
void
setIgnoreInserted(booleanvalue)
           Gets or sets a boolean value indicating either to ignore text inside insert revisions. The default value is false.
booleangetMatchCase()
void
setMatchCase(booleanvalue)
           True indicates case-sensitive comparison, false indicates case-insensitive comparison.
booleangetPreserveMetaCharacters()
void
           True indicates that meta-characters beginning with "&" are preserved. Default value is false.
IReplacingCallbackgetReplacingCallback()
void
           The user-defined method which is called before every replace occurrence.
booleangetUseLegacyOrder()
void
setUseLegacyOrder(booleanvalue)
           True indicates that a text search is performed sequentially from top to bottom considering the text boxes. Default value is false.
 

    • Constructor Detail

      • FindReplaceOptions

        public FindReplaceOptions()
      • FindReplaceOptions

        public FindReplaceOptions(int direction)
        Parameters:
        direction - A FindReplaceDirection value.
    • Property Getters/Setters Detail

      • getApplyFont

        public Font getApplyFont()
        
        Text formatting applied to new content.

        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()
        
        Paragraph formatting applied to new content.

        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)
        
        Selects direction for replace. Default value is FindReplaceDirection.FORWARD. The value of the property is FindReplaceDirection integer constant.

        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)
        
        True indicates the oldValue must be a standalone word.

        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");
      • getIgnoreDeleted/setIgnoreDeleted

        public boolean getIgnoreDeleted() / public void setIgnoreDeleted(boolean value)
        
        Gets or sets a boolean value indicating either to ignore text inside delete revisions. The default value is false.
      • getIgnoreFields/setIgnoreFields

        public boolean getIgnoreFields() / public void setIgnoreFields(boolean value)
        
        Gets or sets a boolean value indicating either to ignore text inside fields. The default value is false.
      • getIgnoreInserted/setIgnoreInserted

        public boolean getIgnoreInserted() / public void setIgnoreInserted(boolean value)
        
        Gets or sets a boolean value indicating either to ignore text inside insert revisions. The default value is false.
      • getMatchCase/setMatchCase

        public boolean getMatchCase() / public void setMatchCase(boolean value)
        
        True indicates case-sensitive comparison, false indicates case-insensitive comparison.

        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)
        
        True indicates that meta-characters beginning with "&" are preserved. Default value is false.

        Example:

        Shows how to preserved meta-characters that begin with "&".
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        builder.writeln("one");
        builder.writeln("two");
        builder.writeln("three");
        
        FindReplaceOptions options = new FindReplaceOptions();
        options.setFindWholeWordsOnly(true);
        options.setPreserveMetaCharacters(true);
        
        doc.getRange().replace("two", "&ldquo; four &rdquo;", options);
      • getReplacingCallback/setReplacingCallback

        public IReplacingCallback getReplacingCallback() / public void setReplacingCallback(IReplacingCallback value)
        
        The user-defined method which is called before every replace occurrence.

        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)
        
        True indicates that a text search is performed sequentially from top to bottom considering the text boxes. Default value is false.