private CompositeNode SplitCompositeAtNode(CompositeNode baseNode, Node targetNode)
{
CompositeNode cloneNode = (CompositeNode)baseNode.Clone(false);
Node node = targetNode;
int currentPageNum = mPageNumberFinder.GetPage(baseNode);
// Move all nodes found on the next page into the copied node. Handle row nodes separately.
if (baseNode.NodeType != NodeType.Row)
{
CompositeNode composite = cloneNode;
if (baseNode.NodeType == NodeType.Section)
{
cloneNode = (CompositeNode)baseNode.Clone(true);
Section section = (Section)cloneNode;
section.Body.RemoveAllChildren();
composite = section.Body;
}
while (node != null)
{
Node nextNode = node.NextSibling;
composite.AppendChild(node);
node = nextNode;
}
}
else
{
// If we are dealing with a row then we need to add in dummy cells for the cloned row.
int targetPageNum = mPageNumberFinder.GetPage(targetNode);
Node[] childNodes = baseNode.ChildNodes.ToArray();
foreach (Node childNode in childNodes)
{
int pageNum = mPageNumberFinder.GetPage(childNode);
if (pageNum == targetPageNum)
{
cloneNode.LastChild.Remove();
cloneNode.AppendChild(childNode);
}
else if (pageNum == currentPageNum)
{
cloneNode.AppendChild(childNode.Clone(false));
if (cloneNode.LastChild.NodeType != NodeType.Cell)
((CompositeNode)cloneNode.LastChild).AppendChild(((CompositeNode)childNode).FirstChild.Clone(false));
}
}
}
// Insert the split node after the original.
baseNode.ParentNode.InsertAfter(cloneNode, baseNode);
// Update the new page numbers of the base node and the clone node including its descendents.
// This will only be a single page as the cloned composite is split to be on one page.
int currentEndPageNum = mPageNumberFinder.GetPageEnd(baseNode);
mPageNumberFinder.AddPageNumbersForNode(baseNode, currentPageNum, currentEndPageNum - 1);
mPageNumberFinder.AddPageNumbersForNode(cloneNode, currentEndPageNum, currentEndPageNum);
foreach (Node childNode in cloneNode.GetChildNodes(NodeType.Any, true))
mPageNumberFinder.AddPageNumbersForNode(childNode, currentEndPageNum, currentEndPageNum);
return cloneNode;
}