Free Support Forum -

Keep original page numbers when combining documents

When combining 2 (or more) documents, each one with it's own page numbers, how can we keep the original page numbering?

For example, document A has 3 pages, document B has 2 pages. Currently, the page numbering is 1,2,3,4,5, but our users want to have 1,2,3,1,2 when the two documents are combined into one.

This is the code that I am using to combine documents:

    Private Sub cmd_CombineForms()
        Dim TL As cSelectTemplateList = CType(Session("SelectForms_TemplateList"), cSelectTemplateList)
        Dim GeneratedPath As String = CType(Session("GeneratedPath"), String)
        Dim FilesAdded As Boolean = False
        Dim FirstDoc As Boolean = True
    Dim doc As Aspose.Words.Document = Nothing

    For Each TI As cSelectTemplateList.cTemplateInfo In TL.TemplateList
        If TI.GeneratedFileName.Length() > 0 AndAlso System.IO.File.Exists(GeneratedPath & TI.GeneratedFileName) Then

            If FirstDoc Then
                doc = New Aspose.Words.Document(GeneratedPath & TI.GeneratedFileName)

                FirstDoc = False
                Dim append_doc As Aspose.Words.Document = New Aspose.Words.Document(GeneratedPath & TI.GeneratedFileName)

                For Each srcSection As Aspose.Words.Section In append_doc
                    Dim dstSection As Aspose.Words.Node = doc.ImportNode(srcSection, True, Aspose.Words.ImportFormatMode.KeepSourceFormatting)

                Next srcSection

                append_doc = Nothing
            End If

            FilesAdded = True
        End If

    If FilesAdded AndAlso doc IsNot Nothing Then
        Response.ContentType = "application/"

        Dim FileName As String = Split(Me.txtFileName.Text.Trim(), " ")(0) & ".docx"
        Response.AddHeader("content-disposition", "attachment;filename=" & Context.Server.HtmlEncode(FileName))

        doc.Save(Response.OutputStream, Aspose.Words.SaveFormat.Docx)

    End If

    doc = Nothing
End Sub

Hi Michael,

Thanks for your inquiry.

To restart the page numbering at the start of section the PageSetup.RestartPageNumbering property must be set to true. The number which this is restarted to is defined by the PageSetup.PageStartingNumber property. This property is set to “1” in Microsoft Word and Aspose.Words by default. For more details, I would suggest you please read the following article:

I hope, this helps.

Best regards,

I basically copied & pasted the code in the link you provided, and it works for the most part...

The problem is that for some documents I get "Page 3 of 2" or "Page 4 of 1"... it does not seem to calculate the total number of pages properly for some documents.

Hi Michael,

Thanks for the additional information. Could you please attach your problematic documents along with source code here for testing? I will investigate the issue on my side and provide you more information.

Best regards,

I found and fixed the problem.

When combining documents, if the first document has a header and footer with pager numbering, and the second document that gets appended, does not have a header and footer; the header and footer from the first document gets copied into the second document. When this happens, the page numbering in the second document that got appended is wrong.

So, by adding this line of code:
I fixed 2 problems :)

Hi Michael,

Thanks for the additional information. It’s great you were able to find what you were looking for. Please let us know any time you have any further queries.

Best regards,