Hello,
I have two documents with the following layout:
- A target document that is setup in two column layout.
- A source document that is setup in single column layout, text is centered, and enclosed in section breaks.
I’m using the sample code below to insert the source document in a bookmarked location in the target document. However, upon inserting the source document, the section breaks are getting lost and therefore, the source document takes the page layout of the target document which is in two columns.
Is there a way to preserve the section breaks of the source document when inserting into the target document?
CODE:
'''
''' Inserts content of the external document after the specified node.
''' Section breaks and section formatting of the inserted document are ignored.
'''
''' Node in the destination document after which the content
''' should be inserted. This node should be a block level node (paragraph or table).
''' The document to insert.
Private Shared Sub InsertDocument(ByVal insertAfterNode As Node, ByVal srcDoc As Document)
' Make sure that the node is either a paragraph or table.
If ((Not insertAfterNode.NodeType.Equals(NodeType.Paragraph))) And ((Not insertAfterNode.NodeType.Equals(NodeType.Table))) Then
Throw New ArgumentException("The destination node should be either a paragraph or table.")
End If
' We will be inserting into the parent of the destination paragraph.
Dim dstStory As CompositeNode = insertAfterNode.ParentNode
' This object will be translating styles and lists during the import.
Dim importer As New NodeImporter(srcDoc, insertAfterNode.Document, ImportFormatMode.KeepSourceFormatting)
' Loop through all sections in the source document.
For Each srcSection As Section In srcDoc.Sections
' Loop through all block level nodes (paragraphs and tables) in the body of the section.
For Each srcNode As Node In srcSection.Body
' Let's skip the node if it is a last empty paragraph in a section.
If srcNode.NodeType.Equals(NodeType.Paragraph) Then
Dim para As Paragraph = CType(srcNode, Paragraph)
If para.IsEndOfSection AndAlso (Not para.HasChildNodes) Then
Continue For
End If
End If
' This creates a clone of the node, suitable for insertion into the destination document.
Dim newNode As Node = importer.ImportNode(srcNode, True)
' Insert new node after the reference node.
dstStory.InsertAfter(newNode, insertAfterNode)
insertAfterNode = newNode
Next srcNode
Next srcSection
End Sub