Find the text and replace it with table using C#

Hi,
I want to create the table as it is in doc2 and i need to insert the same table in input.doc in place of

Please any one can help me in this task. Please find the attached documents.

Hi Sreelakshmi,

Thanks for your query. Please use the following code snippet to find text “ bw_raw_rpt ” and replace it with table. Please read the following documentation links for your kind reference.

Document doc = new Document("D:\\Input.docx");

DocumentBuilder builder = new DocumentBuilder(doc);

Pattern regex = Pattern.compile("bw_raw_rpt", Pattern.CASE_INSENSITIVE);

firstDoc.getRange().replace(regex, new MyReplaceEvaluatorTable(), false);

firstDoc.save("D:\\Output.docx");

private class MyReplaceEvaluatorTable implements IReplacingCallback

{

/**

* This is called during a replace operation each time a match is found.

* This method appends a number to the match string and returns it as a replacement string.

*/

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

// This array is used to store all nodes of the match for further highlighting.

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

}

Document doc = (Document)e.getMatchNode().getParentNode().getDocument();

DocumentBuilder builder = new DocumentBuilder(doc);

builder.moveTo(currentNode);

// We call this method to start building the table.

builder.startTable();

builder.insertCell();

builder.write("Row 1, Cell 1 Content.");

// Build the second cell

builder.insertCell();

builder.write("Row 1, Cell 2 Content.");

// Call the following method to end the row and start a new row.

builder.endRow();

// Build the first cell of the second row.

builder.insertCell();

builder.write("Row 2, Cell 1 Content");

// Build the second cell.

builder.insertCell();

builder.write("Row 2, Cell 2 Content.");

builder.endRow();

// Signal that we have finished building the table.

builder.endTable();

// Now remove all runs in the sequence.

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;

}

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;

}

}