Incorrect page numbering when saving as .PDF

Hi Mirek,

You can follow the code below to restart the page count for each new appended document. For example:

Dim doc As New Document(dataDir & “Document+1.docx”)

Dim src As New Document(dataDir & “Document+2.docx”)

doc.AppendDocument(src, ImportFormatMode.KeepSourceFormatting)

Dim builder As New DocumentBuilder(doc)

’ Set first section page numbering.

Dim section As Section = doc.Sections(0)

section.PageSetup.RestartPageNumbering = True

section.PageSetup.PageStartingNumber = 1

’ Set second section page numbering.

section = doc.Sections(1)

section.PageSetup.PageStartingNumber = 1

section.PageSetup.RestartPageNumbering = True

doc.Save(dataDir & “Document Out.pdf”)

When a document is appended, Aspose.Words automatically inserts it into a new section. So if you are merging more than one document you can simply run this code on each new section.

Please note, for the total page number to work for each of the new sections, you need to replace the NUMPAGES field with SECTIONPAGES in each of your template documents by editing that field in the header. If you don’t then it will still display the total number of pages in the merged document.

Thanks,

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.

Thanks guys, looks like exactly what I need.

The issues you have found earlier (filed as 13239) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(24)