Using the AppendDocument method

Hi, I am trying to use the AppendDocument method to add a document to the end of another and IntelliSense in Visual Studio is telling me that AppendDocument is not a member of Aspose.Words.Document.
My code looks something like this:

Dim docMaster as new Aspose.Words.Document
Dim doc As Document = New Document()
docMaster.AppendDocument(doc,ImportFormatMode.KeepSourceFormatting)

At the top of the class, I have the following imports:

Imports Aspose.Words
Imports Aspose.Words.ComHelper
Imports Aspose.Words.Viewer.DocumentRenderer

Am I missing something or is there something else I should be doing? Any help you can give would be most appreciated!
Thanks in advance!
C.

Hi
Thanks for your inquiry. There is no AppendDocument method in Aspose.Words API. AppendDocument is a custom method that looks like the following.

private void AppendDocument(Document dstDoc, Document srcDoc)
{
    foreach (Section srcSection in srcDoc)
    {
        // Import section
        Section newSection = (Section)dstDoc.ImportNode(srcSection, true, ImportFormatMode.KeepSourceFormatting);
        // Append content of the src section to last section of dst document
        dstDoc.LastSection.AppendContent(newSection);
    }
}

Here is VB.NET version of this code:

Private Sub AppendDocument(ByVal dstDoc As Document, ByVal srcDoc As Document)
For Each srcSection As Section In srcDoc.Sections
'Import section
Dim newSection As Section = CType(dstDoc.ImportNode(srcSection, True, ImportFormatMode.KeepSourceFormatting), Section)
'Append content of the src section to last section of dst document
dstDoc.LastSection.AppendContent(newSection)
Next
End Sub

Hope this helps.
Best regards.

Hi Alexey,
Yes, thank you very much. That works, sort of. It does what it says in that it appends the source document to the destination document at the point where the destination document ends. This means that if the destination documents ends half way down a page, the source document that is being appended starts at that point. What I need to do is start the appended source document at the top of the next page. Can that be done?
Also, the original MS Word source documents have page section breaks in them so that the first page of the document automatically prints on letterhead and the subsequent pages print on blank paper. If I append one source document to the end of another how can I keep the section breaks intact?
I still need to have the first page of the first source document print on letterhead, the subsequent pages of the first source document print on plain, the first page of the appended second source document print on letterhead, the subsequent pages of the appended second source docuemtn print on plain, and so on.
Thanks again for all your help.
C.

Hello!
There is a sample in our online documentation that can help you join two documents preserving all sections. You can also set an option how the first section of the appended document should start.
Please follow this link:
https://docs.aspose.com/words/net/insert-and-append-documents/
Regards,

Thank you very much for the link. It was very helpful. I followed the example, but instead of coding:

srcDoc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous

as it is shown in the example, I coded:

srcDoc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage

because I want the appended source document to start on a new page of the destination document. I can’t seem to get it to work, though. The destination document still appears as if I had coded SectionStart.Continuous.
My code is exactly the same as the example in all other respects. Can you tell me what I may be doing incorrectly? Thanks very much.
C.

Hi
Hi thanks for your request. I tried this on my side and all works correctly. Could you please attach your documents for testing?
Best regards.

Thanks Alexey.
Attached are two sample documents, MA-050410-1[5].doc & MA-050410-1[6].doc.
In my code I create a new blank destination document called docMaster,

Dim docMaster As New Aspose.Words.Document

then I create documents for each of the source documents as follows:

Dim doc1 As Document = New Document("//myserver/mydirectory/MA-050410-1[5].doc")
Dim doc2 As Document = New Document("//myserver/mydirectory/MA-050410-1[6].doc")
then I append each source document to the destination document calling the AppendDocument subroutine as follows:
doc1.FirstSection.PageSetup.SectionStart = SectionStart.NewPage
AppendDocument(docMaster, doc1)
doc2.FirstSection.PageSetup.SectionStart = SectionStart.NewPage
AppendDocument(docMaster, doc2)

When I look at docMaster after the appends have been done I see that doc1 starts at the top of docMaster, but doc2 starts at the end of doc1 rather than the next page.
Thanks!

Hi again,
here is the other document file.
Thanks!

Hi
Thank you for additional information. I used the following code for testing and all works fine.

Sub Main()
Dim lic As Aspose.Words.License = New Aspose.Words.License()
lic.SetLicense("Aspose.Words.lic")
Dim docMaster As New Aspose.Words.Document
Dim doc1 As Document = New Document("MA-050410-1[5].doc")
Dim doc2 As Document = New Document("MA-050410-1[6].doc")
doc1.FirstSection.PageSetup.SectionStart = SectionStart.NewPage
AppendDoc(docMaster, doc1)
doc2.FirstSection.PageSetup.SectionStart = SectionStart.NewPage
AppendDoc(docMaster, doc2)
docMaster.Save("out.doc")
End Sub
Public Sub AppendDoc(ByVal dstDoc As Document, ByVal srcDoc As Document)
' Loop through all sections in the source document. 
For Each srcSection As Section In srcDoc
' Importing a node creates a copy of the original node
Dim dstSection As Node = dstDoc.ImportNode(srcSection, True, ImportFormatMode.KeepSourceFormatting)
' Now the new section node can be appended to the destination document.
dstDoc.AppendChild(dstSection)
Next srcSection
End Sub

Best regards.

Hi Alexey,
Thanks SO much for this!! It works beautifully. The AppendDoc subroutine you sent is slightly different than the one I was using, but once I started using your version, it works great!
Thanks again for your help.
Cheers!
Chris.