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

Free Support Forum - aspose.com

Losing section breaks

I am using the atached function to insert a document (attached) into another document at a specified bookmark. All works fine except, if the document that I am inserting has a section break between two paragraphs, I am not picking it up. I tried adding my own section break at a specific node but the section breaks were ending up at the end of the document instead of the specified node. Any help would be appreciated.

Hi Howard,

Thanks for you inquiry. The InsertDocument method inserts contents of one document at a specified location in another document without page orientation. In your case, I suggest you please use following method to achieve your requirements. Hope this helps you. Please let us know if you have any more queries.

Public Sub InsertDocumentWithSectionFormatting(ByVal insertAfterNode As Node, ByVal srcDoc As Document)
' Make sure that the node is either a pargraph 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
' Document to insert srcDoc into.
Dim dstDoc As Document = DirectCast(insertAfterNode.Document, Document)
' To retain section formatting, split the current section into two at the marker node and then import the content from srcDoc as whole sections.
' The section of the node which the insert marker node belongs to
Dim currentSection As Section = DirectCast(insertAfterNode.GetAncestor(NodeType.Section), Section)
' Don't clone the content inside the section, we just want the properties of the section retained.
Dim cloneSection As Section = DirectCast(currentSection.Clone(False), Section)
' However make sure the clone section has a body, but no empty first paragraph.
cloneSection.EnsureMinimum()
cloneSection.Body.FirstParagraph.Remove()
' Insert the cloned section into the document after the original section.
insertAfterNode.Document.InsertAfter(cloneSection, currentSection)
' Append all nodes after the marker node to the new section. This will split the content at the section level at
' the marker so the sections from the other document can be inserted directly.
Dim currentNode As Node = insertAfterNode.NextSibling
While currentNode IsNot Nothing
Dim nextNode As Node = currentNode.NextSibling
cloneSection.Body.AppendChild(currentNode)
currentNode = nextNode
End While
' This object will be translating styles and lists during the import.
Dim importer As New NodeImporter(srcDoc, dstDoc, ImportFormatMode.KeepSourceFormatting)
' Loop through all sections in the source document.
For Each srcSection As Section In srcDoc.Sections
Dim newNode As Node = importer.ImportNode(srcSection, True)
' Append each section to the destination document. Start by inserting it after the split section.
dstDoc.InsertAfter(newNode, currentSection)
currentSection = DirectCast(newNode, Section)
Next
End Sub

Thanks for the fast reply.

This does what I need except that it is adding an extra section break before and after each section. Is there a way to prevent that?

Thank You!

Hi Howard,

Thanks for you inquiry. Could you please share the following detail here for testing? We will then provide you more information on this along with code.

  • Please supply us with the input documents
  • Please supply us with the output document showing the undesired behaviour
  • Please supply us with the expected document showing the desired behaviour (You can create this document using Microsoft Word).

I’ve attached 5 documents, they are as follows:

Destination.doc - this where I will insert the source documents. They will be inserted at a bookmark called “starthere”

Source1.doc - First source document to be inserted.

Source2.doc - Second source document to be inserted

WrongResult.doc - after inserting the 2 documents, there is an extra section break before and after the inserted document.

CorrectResult.doc - this is the expected result without the extra section breaks.

I appreciate all the help that you have been giving me.

Thank You!

Hi Howard,

Thanks for sharing the documents. In your case, I suggest you please use the InsertDocument method shared here. Please use the following code snippet to achieve the same output explained in CorrectResult.doc. Hope this helps you.

Dim doc1 As New Document(MyDir & "Source1.doc")
Dim builder1 As New DocumentBuilder(doc1)
builder1.MoveToDocumentEnd()
builder1.StartBookmark("bm1")
builder1.EndBookmark("bm1")
Dim doc2 As New Document(MyDir & "Source2.doc")
Dim builder2 As New DocumentBuilder(doc2)
builder2.MoveToDocumentEnd()
builder1.StartBookmark("bm2")
builder1.EndBookmark("bm2")
Dim doc As New Document(MyDir & "Destination.doc")
Dim builder As New DocumentBuilder(doc)
builder.MoveToBookmark("starthere")
Dim para As Paragraph = builder.CurrentParagraph
InsertDocument(para, doc1)
'Move cursor to bookmark bm1 
'and insert column break to add second document in next column
builder.MoveToBookmark("bm1")
builder.InsertBreak(BreakType.ColumnBreak)
para = builder.CurrentParagraph
InsertDocument(para, doc2)
doc.Save(MyDir & "Out.docx")

Tahir,

I don’t want to programmatically create a new column. The destination document should control whether there are 1 or 2 columns.

The last function you sent was working fine except it put in a n extra section break before and after the source. If we can eliminate those 2 extra section breaks, then we would be fine.

Hi Howard,

Thanks for you inquiry. Yes, you can remove and insert section breaks by using the Aspose.Words. There are already two section breaks before and after bookmark ‘starthere’ in Destination.doc. If you import the source1.doc and source2.doc by using InsertDocumentWithSectionFormatting, you can remove empty section by using following code snippet.

'Search for empty sections
Dim sectList As New ArrayList()
For Each sect As Section In doc.Sections
If (sect.Body.FirstParagraph Is Nothing) OrElse sect.Body.FirstParagraph.IsEndOfSection AndAlso String.IsNullOrEmpty(sect.Body.FirstParagraph.ToString(SaveFormat.Text).Trim()) Then
sectList.Add(sect)
End If
Next
'remove empty sections
For Each sect As Section In sectList
sect.Remove()
Next
doc.Save(MyDir & "Out.docx")

To get the output shared in CorrectResult.doc, you need to add bookmarks in each page columns. Once you have bookmark in each column, you can move cursor to bookmark and insert document by using InsertDocumentWithSectionFormatting method. Hope this helps you. Please let us know if you have any more queries.

Good Morning,

I don’t want to remove sections or touch anything that is in the destination document. This document is used as a template.

In post # 477954 you gave me a function which does what I want except that somewhere in that function two extra section breaks are getting generated. I do not want to touch the scetion breaks that are already in the destination or source documents.

The function InsertDocumentWithSectionFormatting that you sent me inserts the source documents into my destination document with it’s style intact. This is what I wanted. But I did not want the extra section breaks that were somehow generated by that function which were placed before and after the source. These section breaks were not part of the destination or source prior to the insertion.

I believe that the section breaks are getting generated somewhere in this area of the code:

’ Document to insert srcDoc into.

Dim dstDoc As Document = DirectCast(insertAfterNode.Document, Document)

’ To retain section formatting, split the current section into two at the marker node and then import the content from srcDoc as whole sections.

’ The section of the node which the insert marker node belongs to

Dim currentSection As Section = DirectCast(insertAfterNode.GetAncestor(NodeType.Section), Section)

’ Don’t clone the content inside the section, we just want the properties of the section retained.

Dim cloneSection As Section = DirectCast(currentSection.Clone(False), Section)

If you can get this routine working as is but not generate those section breaks, it would solve my problem.

Thank You.

Hi Howard,

Thanks for you inquiry. Unfortunately, I have not completely understood your query.

hchernick:

CorrectResult.doc - this is the expected result without the extra section breaks.

There are two section breaks (continues) in your Destination.doc before and after bookmark starthere. In your CorrectResult.doc, there is no new section breaks exists except the ones which already exists in Destination.doc. Please see the attached CorrectResult.png for detail. In this case, you can achieve the required output explain in CorrectResult.doc by using InsertDocument method.

hchernick:

All works fine except, if the document that I am inserting has a section break between two paragraphs, I am not picking it up.

Yes, InsertDocumentWithSectionFormatting method inserts extra section breaks. Please see the attached images for detail. Please manually create your expected Word document using Microsoft Word and attach it here for our reference. We will investigate, how you want your final Word output be generated like. We will then provide you more information on this along with code.

Hi,

I understand that the InsertDocument will give me the results without the extra section breaks. But, it loses the styles and formatting.

By using InsertDocumentWithSectionFormatting, it retains the styles but inserts the two section breaks. I need to keep the formatting and styles intact but without the two extra section breaks.

I’ve attached two screen shots. The wrong.jpg show the results after using InsertDocumentWithSectionFormatting. Nottice the yellow highlighted area. These are the extra section breaks that the function is inserting.

The correct.jpg shows a screenshot of what I would really like. The extra section breaks are not included.

Hi Howard,

Thanks for sharing the detail. Please note that when you insert a Section node in the middle of page (in your case, at bookmark starthere), the section break will be inserted in the document. Regarding your query about losing styles and formatting, perhaps you are using ImportFormatMode.UseDestinationStyles in InsertDocument method. Please use the ImportFormatMode.KeepSourceFormatting in InsertDocument method.

If you still face problem, please share detail about style and formatting issues along with document. We will then provide you more information on this along with code.