Hey bro, I have a question.
How do I clone anything between the two words “table2” and “table3” into a new document?
a.docx (16.2 KB)
@quanghieumylo, you can use the following example:
Document doc = new Document("a.docx");
SearchOnlyCallback searchCallback = new SearchOnlyCallback();
FindReplaceOptions opts = new FindReplaceOptions()
{
ReplacingCallback = searchCallback
};
// Find the node containing Table1.
doc.Range.Replace("Table1", "", opts);
Node table1Node = searchCallback.foundNode;
searchCallback.foundNode = null;
// Find the node containing Table3.
doc.Range.Replace("Table3", "" , opts);
Node table3Node = searchCallback.foundNode;
// Extract nodes between Table1 and Table3.
var nodes = ExtractContentHelper.ExtractContent(table1Node, table3Node, false);
// Add the found nodes to a new document.
Document newDoc = (Document)doc.Clone(false);
newDoc.RemoveAllChildren();
newDoc.EnsureMinimum();
NodeImporter importer = new NodeImporter(doc, newDoc, ImportFormatMode.KeepSourceFormatting);
foreach(Node node in nodes)
newDoc.LastSection.Body.AppendChild(importer.ImportNode(node, true));
newDoc.Save("new.docx");
internal class SearchOnlyCallback : IReplacingCallback
{
public ReplaceAction Replacing(ReplacingArgs args)
{
foundNode = args.MatchNode;
return ReplaceAction.Stop;
}
public Node? foundNode = null;
}
The code for the ExtractContentHelper class can be found here.
1 Like
I tried the code you sent and had success with the original file.
However, when I use that code with the new file, the data is still in a table.
a1.docx (16.1 KB)
a1_result.docx (11.1 KB)
@quanghieumylo, to make the example work with a.docx and a1.docx, please replace the ExtractContentHelper.GetAncestorInBody method with the following code:
private static Node GetAncestorInBody(Node startNode)
{
while (startNode.ParentNode.NodeType != NodeType.Cell &&
startNode.ParentNode.NodeType != NodeType.Body)
startNode = startNode.ParentNode;
return startNode;
}
1 Like