Hi
Thank you for your interest in Aspose.Words. Since Ms Word document can contain any number of sections, it is better to use code like the following:
Dim dataDir As String = “C:\Temp”
Dim doc As New Document(dataDir & “Document+1.docx”)
Dim src As New Document(dataDir & “Document+2.docx”)
’ Set RestartPageNumbering flag of the first section of the source document.
src.FirstSection.PageSetup.RestartPageNumbering = True
doc.AppendDocument(src, ImportFormatMode.KeepSourceFormatting)
doc.Save(dataDir & “Document Out.pdf”)
But anyways this code only partially resolves the issue. In your documents you have {PAGE} of {NUMPAGES}. The code above resolves the problem with {PAGE}. So in final document you will get numbering like 1 of 9, 2 of 9, 3 of 9, 1 of 9, 2 of 9 etc. But I suppose you expects to get numbering like 1 of 3, 2 of 3, 3 of 3, 1 of 6, 2 of 6 etc. Am I right? If so the only solution, I can suggest you at the moment is unlinking NUMPAGES fields in the documents before combining. Please see sample code below:
Dim doc As New Document(dataDir & “Document+1.docx”)
Dim src As New Document(dataDir & “Document+2.docx”)
’ unlink NUMPAGES fields in both documents.
UnlinkNumPages(doc)
UnlinkNumPages(src)
’ Set RestartPageNumbering flag of the first section of the source document.
src.FirstSection.PageSetup.RestartPageNumbering = True
doc.AppendDocument(src, ImportFormatMode.KeepSourceFormatting)
doc.Save(dataDir & “Document Out.pdf”)
=============================================================================
Private Sub UnlinkNumPages(ByVal doc As Document)
’ Get field strats.
Dim starts As Node() = doc.GetChildNodes(NodeType.FieldStart, True).ToArray()
’ Loop through al field starts and unlink PAGE fields.
For Each start As FieldStart In starts
If (start.FieldType = FieldType.FieldNumPages) Then
’ Remove FieldStart and field code.
Dim currentNode As Node = start
While (Not currentNode.NodeType = NodeType.FieldSeparator)
Dim nextNode As Node = currentNode.NextSibling
currentNode.Remove()
currentNode = nextNode
End While
’ Now we need to remove FieldSeparator and FieldEnd
Dim sep As Node = currentNode
’ Look for FieldEnd
While (Not currentNode.NodeType = NodeType.FieldEnd)
currentNode = currentNode.NextSibling
End While
’ Remove FieldEnd and FieldSeparator.
sep.Remove()
currentNode.Remove()
End If
Next
End Sub
Hope this helps.
Best regards.