We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Replace {place holders} with text in word (v17.04-java)

How can we replace {place holders} with the text / html content in the given word doc template…?

It seems Range().replace() method is deprecated in the Aspose-word v17.04 (java). The examples would be helpful. Thanks.

Hi there,


Thanks for your inquiry. Please refer to the following article:
Find and Replace

The ReplaceAction, IReplacingCallback and ReplacingArgs had been moved to Aspose.Words.Replacing namespace. We introduced FindReplaceOptions class in Aspose.Words 16.7. Please refer to the release notes:
Aspose.Words for Java 16.7.0 Release Notes

If you want to replace {place holders} with HTML, we suggest you following solution.

1. Please implement IReplacingCallback interface.
2. In IReplacingCallback.Replacing, move the cursor to the matched node.
3. Insert the HTML using DocumentBuilder.InsertHtml

Please check the code example shared in following link.
How to Find and Highlight Text
I am new to this API.
Thanks a lot for quick reply. It works pretty well.!

Noticed few issues with HTML content replacement.

1) <> converting to t;> rather than <>
2) It shows 2b ; Expected content 2a

( actual html content #2c)

#2a

Examples:
• Apple
• Samsung
• Windows

#2b

Examples:
ull; Apple
ull; Samsung
ull; Windows

#2c

Examples:
Apple
•Samsung
Windows

Hi there,


Thanks for your inquiry. Please use following code example to achieve your requirement.

If you still face problem, please share the following detail here for our reference. We will then provide you code example according to your requirement.
  • Your input document.
  • Your input Html.
  • The place holder text.

Document doc = new Document(MyDir + “TestFile.doc”);

FindReplaceOptions options =
new FindReplaceOptions();

options.
ReplacingCallback = new FindandInsertHtml("
• Apple
•Samsung
•Windows"
);

Pattern regex = Pattern.compile(
“place holder text”, Pattern.CASE_INSENSITIVE);

doc.getRange().replace(regex,
“”, options);

doc.save(
MyDir + “TestFile_out.doc”);<o:p></o:p>
class FindandInsertHtml implements IReplacingCallback {
String
html;
public FindandInsertHtml(String htmlstring)
{
html = htmlstring;
}
public int replacing(ReplacingArgs e) throws Exception {
// This is a Run node that contains either the beginning or the complete match.
Node currentNode = e.getMatchNode();
// The first (and may be the only) run can contain text before the match,
// in this case it is necessary to split the run.
if (e.getMatchOffset() > 0)
currentNode = splitRun((Run) currentNode, e.getMatchOffset());

ArrayList runs =
new ArrayList();
// Find all runs that contain parts of the match string.
int remainingLength = e.getMatch().group().length();
while ((remainingLength > 0) && (currentNode != null) && (currentNode.getText().length() <= remainingLength)) {
runs.add(currentNode);
remainingLength = remainingLength - currentNode.getText().length();
// Select the next Run node. // Have to loop because there could be other nodes such as BookmarkStart etc.
do {
currentNode = currentNode.getNextSibling();
}
while ((currentNode != null) && (currentNode.getNodeType() != NodeType.RUN));
}
// Split the last run that contains the match if there is any text left.
if ((currentNode != null) && (remainingLength > 0)) {
splitRun((Run) currentNode, remainingLength);
runs.add(currentNode);
}

DocumentBuilder builder =
new DocumentBuilder((Document)e.getMatchNode().getDocument());
builder.moveTo((Run)runs.get(
0));
builder.insertHtml(
html);
// Remove matched text
for (Run run : (Iterable) runs)
run.remove();
// Signal to the replace engine to do nothing because we have already done all what we wanted.
return ReplaceAction.SKIP;
}
/**
* Splits text of the specified run into two runs. Inserts the new run just
* after the specified run.
*/
private Run splitRun(Run run, int position) throws Exception {
Run afterRun = (Run) run.deepClone(
true);
afterRun.setText(run.getText().substring(position));
run.setText(run.getText().substring((
0), (0) + (position)));
run.getParentNode().insertAfter(afterRun, run);
return afterRun;
}
}