Hi, I have two word documents, namely “word1.docx” and “word2.docx”, and I want to replace the placeholder in “word1.docx” with “word2.docx” content,this placeholder is between text of the file of “word1”,as you can see from the document i attach.How can i achieve this?
word2.docx (145.6 KB)
word1.docx (10.6 KB)
@ryota9527
Can you please specify the exact placeholder text you want to replace in ‘word1.docx’ and clarify how you would like the content from ‘word2.docx’ to be inserted?
1.placeholder is [PLACEHOLDER] 2.Insert without changing the format ,thanks
@ryota9527 You can use IReplacingCallback to achieve this. For example see the following code:
Document doc = new Document("C:\\Temp\\word1.docx");
FindReplaceOptions opt = new FindReplaceOptions();
opt.setReplacingCallback(new ReplaceWithDocumentCallback());
doc.getRange().replace("[PLACEHOLDER]", "C:\\Temp\\word2.docx", opt);
doc.save("C:\\Temp\\out.docx");
public class ReplaceWithDocumentCallback implements IReplacingCallback {
/**
* This method is called by the Aspose.Words find and replace engine for each match.
*/
@Override
public int replacing(ReplacingArgs e) throws Exception {
Document doc = (Document)e.getMatchNode().getDocument();
// 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());
// This array is used to store all nodes of the match for further deleting.
ArrayList<Run> runs = new ArrayList<Run>();
// 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((Run)currentNode);
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((Run)currentNode);
}
// Create DocumentBuilder to insert HTML.
DocumentBuilder builder = new DocumentBuilder(doc);
// Move builder to the first run.
builder.moveTo(runs.get(0));
builder.insertDocument(new Document(e.getReplacement()), ImportFormatMode.KEEP_SOURCE_FORMATTING);
Paragraph currentParagraph = builder.getCurrentParagraph();
// Delete matched runs
for (Run run : runs)
run.remove();
// Remove current paragraph if it is empty.
if(!currentParagraph.hasChildNodes())
currentParagraph.remove();
// Signal to the replace engine to do nothing because we have already done all what we wanted.
return ReplaceAction.SKIP;
}
private static Run splitRun(Run run, int position)
{
Run afterRun = (Run)run.deepClone(true);
run.getParentNode().insertAfter(afterRun, run);
afterRun.setText(run.getText().substring(position));
run.setText(run.getText().substring(0, position));
return afterRun;
}
}
thank you for your reply,its worked!
1 Like