I have two documents with same bookmark name and wanted to copy bookmark content from one document to another document.But Bookmark end throwing exception.
public void CopyBookmarkContentBetweenDocuments(List<string> bookmarkNames, string sourceFileName, string destinationFileName)
{
var sourceDocument = new Document(sourceFileName);
var destinationDocument = new Document(destinationFileName);
foreach (var bookmarkName in bookmarkNames)
{
Bookmark bookmarkSourceDocument = CommonFunction.FindBookmark(sourceDocument, bookmarkName);
if (bookmarkSourceDocument == null)
continue;
Bookmark bookmarkDestinationDocument = CommonFunction.FindBookmark(destinationDocument, bookmarkName);
if (bookmarkDestinationDocument == null)
continue;
bookmarkDestinationDocument.Text = ""; // Remove possibly nodes enclosed by bookmark in destination document
bookmarkDestinationDocument = CommonFunction.FindBookmark(destinationDocument, bookmarkName); // Get bookmark again. Necessary?
NodeImporter imp = new NodeImporter(sourceDocument, destinationDocument, ImportFormatMode.KeepSourceFormatting);
foreach (Node node in bookmarkSourceDocument.BookmarkStart.ParentNode)
{
Node impNode = imp.ImportNode(node, true);
bookmarkDestinationDocument.BookmarkStart.ParentNode.AppendChild(impNode);
}
}
destinationDocument.Save(destinationFileName);
}
26dbb9c8-59fc-4862-8630-6fd08d3a3669.zip (40.1 KB)
Saksfremlegg.Docx Source document and SaksprotokollElements.Docx destination document