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

Free Support Forum - aspose.com

How to delete text before MergeField?

image.png (531 Bytes)

If my tag does not set value, I want to delete the merge tag and “some texts”.

What should I do? thanks a lot.

@huawei,

You can build on the following code that removes all nodes before the merge field and the leftover merge fields too:

Document doc = new Document("E:\\temp\\input.docx");

Node[] fields =  doc.GetChildNodes(NodeType.FieldStart, true).ToArray();
foreach (FieldStart field in fields)
{
    if (field.FieldType == FieldType.FieldMergeField)
    {
        Node node = field.PreviousSibling;
        while (node != null) {
            Node temp = node;
            node = node.PreviousSibling;
            temp.Remove();
        }

        field.GetField().Remove();
    }
}

doc.Save("E:\\Temp\\18.12.docx");