Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue May 22, 2018 6:25 pm

Hi,

I am attempting to merge multiple docx files. The issue I am having is that each file goes on a new page.

I am using the code below to merge the attached docx 3 times. The file contains 1 line. I expect to get a result file with 1 page with 3 lines. Instead, I get a result file with 3 pages with 1 line each.

How can I merge files to the same page?

Thanks

Code: Select all
    Private Shared Sub testMerge1(file1 As String, merge1 As String)
        Dim wordDocument As New Spire.Doc.Document()

        For i = 1 To 3
            wordDocument.InsertTextFromFile(file1, FileFormat.Docx)
        Next

        wordDocument.SaveToFile(merge1)
        wordDocument.Close()
    End Sub

msadovskiy
 
Posts: 7
Joined: Tue May 17, 2016 4:35 pm

Wed May 23, 2018 7:48 am

Hello,

Please refer to the below code.
Code: Select all
Dim file1 As String
file1 = "file1.docx"
Dim file2 As String
file2 = "file1.docx"
'Create new word document and load the document
Dim destDocument As New Document(file1)
Dim document As New Document(file2)
'Add the contents of the document1 to destDocument1
For i = 1 To 3
    For Each section As Section In document.Sections
        For Each obj As DocumentObject In section.Body.ChildObjects()
            destDocument.LastSection.Body.ChildObjects.Add(obj.Clone())
        Next obj
    Next section
Next
destDocument.SaveToFile("MergeDocument.docx")


Sincerely,
Wade
E-iceblue support team
User avatar

Wade.shao
 
Posts: 32
Joined: Thu Mar 22, 2018 8:23 am

Wed May 23, 2018 4:30 pm

It worked. Thanks

msadovskiy
 
Posts: 7
Joined: Tue May 17, 2016 4:35 pm

Thu May 24, 2018 1:11 am

Hello,

Just feel free to contact us if you need any assistance.

Sincerely,
Wade
E-iceblue support team
User avatar

Wade.shao
 
Posts: 32
Joined: Thu Mar 22, 2018 8:23 am

Return to Spire.Doc