Problems with deleting complete paragraph / section from word

Hello

I have a template rtf document. (I have attached a sample template document)
Inside this doc there is a “special language” for replacements, loops and ifs.

For example there is <?:expression> … Text, conditions, table, anything …
If this expression is true the <?:...> and tags are remove from the paragraph text.
This works.

My problem is, if the condition would be false.
Then everything (text, colored line, tables, and so on) between my tags should be removed.

At first I search every paragraph of the document for my conditions. (works)
Then I have the start Node (StartTag) and the end Node (EndTag) and try to remove every Node between this start and end Node. Therefor I use node.remove.
The Text is removed, but the Tables and colored lines are still part of the document.

Any ideas in which way my problem could be solved?

Thanks for your help.

Best regards
Randolf Kastenmaier

Hi
Thanks for your request. In your case, you should get nodes between which you need to remove content and then use code like the following to remove it:

/**
* Remove all nodes between start and end nodes, except start and end nodes
* @param start The start node
* @param end The end node
* @throws Exception
*/
public static void RemoveSequence(Node start, Node end) throws Exception
{
    Node curNode = start.nextPreOrder(start.getDocument());
    while (curNode != null && !curNode.equals(end))
    {
        // Move to next node
        Node nextNode = curNode.nextPreOrder(start.getDocument());
        // Check whether current contains end node
        if (curNode.isComposite())
        {
            CompositeNode curComposite = (CompositeNode) curNode;
            if (!curComposite.getChildNodes(NodeType.ANY, true).contains(end) &&
                !curComposite.getChildNodes(NodeType.ANY, true).contains(start))
            {
                nextNode = curNode.getNextSibling();
                curNode.remove();
            }
        }
        else
        {
            curNode.remove();
        }
        curNode = nextNode;
    }
}

Hope this helps. Please let us know in case of any issues, we will be glad to help you.
Best regards,

Hello Alexey

Thanks for your help.
Your solution works great.

Best regards
Randolf Kastenmaier