We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Preserve section breaks when inserting document at a bookmark

Hello,

I have two documents with the following layout:

1. A target document that is setup in two column layout.
2. 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.
‘’’

‘’’ <param name=“insertAfterNode”>Node in the destination document after which the content
‘’’ should be inserted. This node should be a block level node (paragraph or table).
‘’’ <param name=“srcDoc”>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 <span class="code-keyword">import</span>.
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 <span class="code-keyword">if</span> 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 <span class="code-keyword">for</span> insertion into the destination document.
        Dim newNode As Node = importer.ImportNode(srcNode, True)

        ' Insert <span class="code-keyword">new</span> node after the reference node.
        dstStory.InsertAfter(newNode, insertAfterNode)
        insertAfterNode = newNode
    Next srcNode
Next srcSection

End Sub


Hi,

Thanks for your inquiry. Unfortunately, it is difficult to say what the problem is without your input (source/target documents), output (document showing the undesired behaviour) and expected Word documents. Please create a simple application (for example a Console Application Project) that helps us reproduce the same problem on our end and attach it here for testing. As soon as you get these pieces of information to us, we'll start our investigation into your issue and provide you more information. Thanks for your cooperation.

Best regards,

Attached is a zip file containing 4 sample documents.

1. target.doc - This is the main document where I want to insert other documents. It is in two-column page layout which contains the source2 bookmark.

2. source.doc - This is the document that I want to insert into the target document. The entire text is centered and enclosed in section breaks.

3. badTarget.doc - This is the actual output that I get when running the code in my first post which inserts a document in a bookmarked location. Notice that the section breaks that encloses the source.doc text are missing. I think if the section breaks are not getting lost from the source document after insertion, then the layout of the source document will be preserved in the target document.

4. goodTarget.doc - This is the expected output. I inserted the source document into the target manually via Insert > Object > Text from File menu in Word. Notice that the section breaks in the source are preserved.

Hope these samples helps and thanks for your prompt response.

Hi,


Thanks for the additional information. Please try run the following code snippet which achieves what you’re looking for.

Dim dstDoc As Document = New Document(“C:\Temp\target.doc”)

Dim srcDoc As Document = New Document("C:\Temp\source.doc")

Dim builder As DocumentBuilder = New DocumentBuilder(dstDoc)

builder.MoveToBookmark("source2")

builder.InsertBreak(BreakType.SectionBreakContinuous)

Dim insertAfterPara As Paragraph = builder.CurrentParagraph

builder.InsertBreak(BreakType.SectionBreakContinuous)

Dim ps As PageSetup = DirectCast(builder.CurrentSection.PreviousSibling, Section).PageSetup

ps.TextColumns.SetCount(1)

ps.TextColumns.EvenlySpaced = True

ps.TopMargin = 0.3 * 72

ps.BottomMargin = 0.3 * 72

ps.LeftMargin = 2.22 * 72

ps.RightMargin = 2.22 * 72

InsertDocument(insertAfterPara, srcDoc)

dstDoc.Save("C:\Temp\out.doc")


I hope, this helps.

Best regards,

Thank you! This code fixed our issue.