Hello,
I manipulate bookmarks in Word document.
I want to get emcompassed bookmarks into an other one.
But in the document : Etiquettes_148x105.docx (23.2 KB), the bookmark ‘Enrobantpicto2’ is not concidered like a encompassing Bookmark (IsComposite)
I have following functions for extract bookmark and know a list of encompassed bookmarks.
public static IList<Node> ExtractContent(Node startNode, Node endNode, bool isInclusive)
{
IList<Node> nodes = new List<Node>();
Node originalStartNode = startNode;
Node originalEndNode = endNode;
while (!new[] { NodeType.Body, NodeType.Cell, NodeType.HeaderFooter }.Contains(startNode.ParentNode.NodeType))
startNode = startNode.ParentNode;
while (endNode.ParentNode.NodeType != startNode.ParentNode.NodeType)
endNode = endNode.ParentNode;
bool isExtracting = true;
bool isStartingNode = true;
bool isEndingNode;
// The current node we are extracting from the document.
Node currNode = startNode;
while (isExtracting)
{
Node cloneNode = currNode.Clone(true);
isEndingNode = currNode.Equals(endNode);
if ((isStartingNode || isEndingNode) && cloneNode.IsComposite)
{
if (isStartingNode)
{
ProcessMarker((CompositeNode)cloneNode, nodes, originalStartNode, isInclusive, isStartingNode, isEndingNode);
isStartingNode = false;
}
if (isEndingNode)
{
ProcessMarker((CompositeNode)cloneNode, nodes, originalEndNode, isInclusive, isStartingNode, isEndingNode);
isExtracting = false;
}
}
else
nodes.Add(cloneNode);
// Move to the next node and extract it. If next node is null that means the rest of the content is found in a different section.
if (currNode.NextSibling == null && isExtracting)
{
Section nextSection = (Section)currNode.GetAncestor(NodeType.Section).NextSibling;
currNode = nextSection.Body.FirstChild;
}
else
{
currNode = currNode.NextSibling;
}
}
return nodes;
}
private static void ProcessMarker(CompositeNode cloneNode, IList<Node> nodes, Node node, bool isInclusive, bool isStartMarker, bool isEndMarker)
{
if (!IsInline(node))
{
if (!(isStartMarker && isEndMarker))
{
if (isInclusive)
nodes.Add(cloneNode);
}
return;
}
if (node.NodeType == NodeType.FieldStart)
{
if ((isStartMarker && !isInclusive) || (!isStartMarker && isInclusive))
{
while (node.NextSibling != null && node.NodeType != NodeType.FieldEnd)
node = node.NextSibling;
}
}
if (node.NodeType == NodeType.CommentRangeEnd)
{
while (node.NextSibling != null && node.NodeType != NodeType.Comment)
node = node.NextSibling;
}
int indexDiff = node.ParentNode.ChildNodes.Count - cloneNode.ChildNodes.Count;
if (indexDiff == 0)
node = cloneNode.ChildNodes[node.ParentNode.IndexOf(node)];
else
node = cloneNode.ChildNodes[node.ParentNode.IndexOf(node) - indexDiff];
bool isProcessing = true;
bool isRemoving = isStartMarker;
Node nextNode = cloneNode.FirstChild;
while (isProcessing && nextNode != null)
{
Node currentNode = nextNode;
isSkip = false;
if (currentNode.Equals(node))
{
if (isStartMarker)
{
isProcessing = false;
if (isInclusive)
isRemoving = false;
}
else
{
isRemoving = true;
if (isInclusive)
isSkip = true;
}
}
nextNode = nextNode.NextSibling;
if (isRemoving && !isSkip)
currentNode.Remove();
}
if (!(isStartMarker && isEndMarker))
{
if (cloneNode.HasChildNodes)
nodes.Add(cloneNode);
}
}
The Bookmark 'Enrobantpicto2 is not considered like IsComposite.
So the list return is empty.
Thanks by advanced.