java.lang.Object
com.aspose.words.ReplacingArgs
public class ReplacingArgs
Example: Example:
public void insertDocumentAtReplace() throws Exception
{
Document mainDoc = new Document(getMyDir() + "InsertDocument1.doc");
FindReplaceOptions options = new FindReplaceOptions();
options.setDirection(FindReplaceDirection.BACKWARD);
options.setReplacingCallback(new InsertDocumentAtReplaceHandler());
mainDoc.getRange().replace(Pattern.compile("\\[MY_DOCUMENT\\]"), "", options);
mainDoc.save(getMyDir() + "\\Artifacts\\InsertDocumentAtReplace.doc");
}
private class InsertDocumentAtReplaceHandler implements IReplacingCallback
{
public int replacing(ReplacingArgs args) throws Exception
{
Document subDoc = new Document(getMyDir() + "InsertDocument2.doc");
// Insert a document after the paragraph, containing the match text.
Paragraph para = (Paragraph) args.getMatchNode().getParentNode();
insertDocument(para, subDoc);
// Remove the paragraph with the match text.
para.remove();
return ReplaceAction.SKIP;
}
}
public void replaceWithInsertHtml() throws Exception
{
// Open the document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Hello <CustomerName>,");
FindReplaceOptions options = new FindReplaceOptions();
options.setReplacingCallback(new ReplaceWithHtmlEvaluator(options));
doc.getRange().replace(Pattern.compile(" <CustomerName>,"), "", options);
// Save the modified document.
doc.save(getMyDir() + "\\Artifacts\\Range.ReplaceWithInsertHtml.doc");
}
private class ReplaceWithHtmlEvaluator implements IReplacingCallback
{
ReplaceWithHtmlEvaluator(FindReplaceOptions options)
{
mOptions = options;
}
/**
* NOTE: This is a simplistic method that will only work well when the match
* starts at the beginning of a run.
*/
public int replacing(ReplacingArgs e) throws Exception
{
DocumentBuilder builder = new DocumentBuilder((Document) e.getMatchNode().getDocument());
builder.moveTo(e.getMatchNode());
// Replace '<CustomerName>' text with a red bold name.
builder.insertHtml("<b><font color='red'>James Bond, </font></b>");
e.setReplacement("");
return ReplaceAction.REPLACE;
}
private /*final*/ FindReplaceOptions mOptions;
}
| Property Getters/Setters Summary | ||
|---|---|---|
int | getGroupIndex() | |
void | setGroupIndex(int value) | |
|
Identifies, by index, a captured group in the |
||
java.util.regex.Matcher | getMatch() | |
|
The |
||
Node | getMatchNode() | |
| Gets the node that contains the beginning of the match. | ||
int | getMatchOffset() | |
| Gets the zero-based starting position of the match from the start of the node that contains the beginning of the match. | ||
java.lang.String | getReplacement() | |
void | setReplacement(java.lang.String value) | |
| Gets or sets the replacement string. | ||
| Property Getters/Setters Detail |
|---|
getGroupIndex/setGroupIndex | |
public int getGroupIndex() / public void setGroupIndex(int value) | |
Default is zero.
getMatch | |
public java.util.regex.Matcher getMatch() | |
Matcher.start() gets the zero-based starting position of the match from the start of the find and replace range.
getMatchNode | |
public Node getMatchNode() | |
Example:
Shows how to insert content of one document into another during a customized find and replace operation.
public void insertDocumentAtReplace() throws Exception
{
Document mainDoc = new Document(getMyDir() + "InsertDocument1.doc");
FindReplaceOptions options = new FindReplaceOptions();
options.setDirection(FindReplaceDirection.BACKWARD);
options.setReplacingCallback(new InsertDocumentAtReplaceHandler());
mainDoc.getRange().replace(Pattern.compile("\\[MY_DOCUMENT\\]"), "", options);
mainDoc.save(getMyDir() + "\\Artifacts\\InsertDocumentAtReplace.doc");
}
private class InsertDocumentAtReplaceHandler implements IReplacingCallback
{
public int replacing(ReplacingArgs args) throws Exception
{
Document subDoc = new Document(getMyDir() + "InsertDocument2.doc");
// Insert a document after the paragraph, containing the match text.
Paragraph para = (Paragraph) args.getMatchNode().getParentNode();
insertDocument(para, subDoc);
// Remove the paragraph with the match text.
para.remove();
return ReplaceAction.SKIP;
}
}getMatchOffset | |
public int getMatchOffset() | |
getReplacement/setReplacement | |
public java.lang.String getReplacement() / public void setReplacement(java.lang.String value) | |
Example:
Replaces text specified with regular expression with HTML.
public void replaceWithInsertHtml() throws Exception
{
// Open the document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Hello <CustomerName>,");
FindReplaceOptions options = new FindReplaceOptions();
options.setReplacingCallback(new ReplaceWithHtmlEvaluator(options));
doc.getRange().replace(Pattern.compile(" <CustomerName>,"), "", options);
// Save the modified document.
doc.save(getMyDir() + "\\Artifacts\\Range.ReplaceWithInsertHtml.doc");
}
private class ReplaceWithHtmlEvaluator implements IReplacingCallback
{
ReplaceWithHtmlEvaluator(FindReplaceOptions options)
{
mOptions = options;
}
/**
* NOTE: This is a simplistic method that will only work well when the match
* starts at the beginning of a run.
*/
public int replacing(ReplacingArgs e) throws Exception
{
DocumentBuilder builder = new DocumentBuilder((Document) e.getMatchNode().getDocument());
builder.moveTo(e.getMatchNode());
// Replace '<CustomerName>' text with a red bold name.
builder.insertHtml("<b><font color='red'>James Bond, </font></b>");
e.setReplacement("");
return ReplaceAction.REPLACE;
}
private /*final*/ FindReplaceOptions mOptions;
}