public Document ExtractContentFromBookmark(Document srcDoc, string bookmarkName)
{
Bookmark bookmark = srcDoc.Range.Bookmarks[bookmarkName];
if (bookmark == null)
throw new Exception(“There is no bookmark with specified name”);
Paragraph startNode = (Paragraph)bookmark.BookmarkStart.GetAncestor(NodeType.Paragraph);
Paragraph endNode = (Paragraph)bookmark.BookmarkEnd.GetAncestor(NodeType.Paragraph);
// Make sure that start and end nodes are children of the same story.
if (!startNode.ParentNode.Equals(endNode.ParentNode))
throw new Exception(“Start and end nodes should be children of the same story”);
// Clone the original document, this is needed to preserve styles of the original document
Document dstDoc = srcDoc.Clone();
dstDoc.RemoveAllChildren();
// The destination document should contain at lean one section.
dstDoc.EnsureMinimum();
dstDoc.FirstSection.Body.RemoveAllChildren();
// We will use NodeImporter to import nodes from one document to another.
NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.UseDestinationStyles);
Node currNode = startNode;
Node dstNode;
// Copy content
while (currNode != null && !currNode.Equals(endNode))
{
// Import node
dstNode = importer.ImportNode(currNode, true);
dstDoc.LastSection.Body.AppendChild(dstNode);
// Move to the next node
Node nextNode = currNode.NextSibling;
//Move to the next section
if (nextNode == null && currNode.ParentNode.NodeType == NodeType.Body)
{
Node sect = currNode.GetAncestor(NodeType.Section);
if (sect.NextSibling != null)
{
dstNode = importer.ImportNode(sect.NextSibling, true);
dstDoc.AppendChild(dstNode);
dstDoc.LastSection.Body.RemoveAllChildren();
nextNode = ((Section)sect.NextSibling).Body.FirstChild;
}
}
currNode = nextNode;
}
// If the bookmark end is the last child of the last paragraph we need to copy it as well.
if (endNode.LastChild.Equals(bookmark.BookmarkEnd))
{
dstNode = dstDoc.ImportNode(endNode, true, ImportFormatMode.UseDestinationStyles);
dstDoc.LastSection.Body.AppendChild(dstNode);
}
dstDoc.Save(“C:\Temp\Temp.doc”);
return dstDoc;
}
public void InsertDocumentAtBookmark(string bookmarkName, Document dstDoc, Document srcDoc)
{
NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.UseDestinationStyles);
//Create DocumentBuilder
DocumentBuilder builder = new DocumentBuilder(dstDoc);
//Move cursor to bookmark and insert paragraph break
builder.MoveToBookmark(bookmarkName);
builder.Writeln();
// If current paragraph is a list item, we should clear its formating.
if (builder.CurrentParagraph.IsListItem)
builder.ListFormat.RemoveNumbers();
//Content of srcdoc will be inserted after this node
Node insertAfterNode = builder.CurrentParagraph.PreviousSibling;
//Content of first paragraph of srcDoc will be apended to this parafraph
Paragraph insertAfterParagraph = null;
if (insertAfterNode.NodeType == NodeType.Paragraph)
insertAfterParagraph = (Paragraph)insertAfterNode;
//Content of last paragraph of srcDoc will be apended to this parafraph
Paragraph insertBeforeParagraph = builder.CurrentParagraph;
//We will be inserting into the parent of the destination paragraph.
CompositeNode dstStory = insertAfterNode.ParentNode;
//Remove empty paragraphs from the end of document
while (!((CompositeNode)srcDoc.LastSection.Body.LastChild).HasChildNodes)
{
srcDoc.LastSection.Body.LastParagraph.Remove();
if (srcDoc.LastSection.Body.LastChild == null)
break;
}
//Loop through all sections in the source document.
foreach (Section srcSection in srcDoc.Sections)
{
//Loop through all block level nodes (paragraphs and tables) in the body of the section.
for (int nodeIdx = 0; nodeIdx < srcSection.Body.ChildNodes.Count; nodeIdx++)
{
Node srcNode = srcSection.Body.ChildNodes[nodeIdx];
//Do not insert node if it is a last empty paragarph in the section.
Paragraph para = null;
if (srcNode.NodeType == NodeType.Paragraph)
para = (Paragraph)srcNode;
if ((para != null) && para.IsEndOfSection && (!para.HasChildNodes))
break;
//If current paragraph is first paragraph of srcDoc
//then appent its content to insertAfterParagraph
bool nodeInserted = false;
if (para != null && para.Equals(srcDoc.FirstSection.Body.FirstChild))
{
nodeInserted = true; // set this flag to know that we already processed this node.
for (int i = 0; i < para.ChildNodes.Count; i++)
{
Node node = para.ChildNodes[i];
Node dstNode = importer.ImportNode(node, true);
insertAfterParagraph.AppendChild(dstNode);
}
//If subdocument contains only one paragraph
//then copy content of insertBeforeParagraph to insertAfterParagraph
//and remove insertBeforeParagraph
if (srcDoc.FirstSection.Body.FirstParagraph.Equals(srcDoc.LastSection.Body.LastChild))
{
while (insertBeforeParagraph.HasChildNodes)
insertAfterParagraph.AppendChild(insertBeforeParagraph.FirstChild);
insertBeforeParagraph.Remove();
}
}
//If current paragraph is last paragraph of srcDoc
//then appent its content to insertBeforeParagraph
if (para != null && para.Equals(srcDoc.LastSection.Body.LastChild))
{
nodeInserted = true; // set this flag to know that we already processed this node.
Node previouseNode = null;
for (int i = 0; i < para.ChildNodes.Count; i++)
{
Node node = para.ChildNodes[i];
Node dstNode = importer.ImportNode(node, true);
if (previouseNode == null)
insertBeforeParagraph.InsertBefore(dstNode, insertBeforeParagraph.FirstChild);
else
insertBeforeParagraph.InsertAfter(dstNode, previouseNode);
previouseNode = dstNode;
}
}
if (!nodeInserted)
{
//This creates a clone of the node, suitable for insertion into the destination document.
Node newNode = dstDoc.ImportNode(srcNode, true);
//Insert new node after the reference node.
dstStory.InsertAfter(newNode, insertAfterNode);
insertAfterNode = newNode;
}
}
}
}
But i got several problems.
1. Numbering is wrong.
2. Last line lost its bullets.