PageNumber in output of merging multiple documents

How do we keep the same page numbers in output document when we merge multiple documents
for ex : first document has 3 pages (1,2,3)
second document has 3 pages (1,2,3)
output document has page numbers as (1,2,3,4,5,6).
the requirement is that the output document has (1,2,3,1,2,3).
I have tried setting restart page number for every section but for every section it is taking 4 pages , so for every 4 pages page number is restarted to 1 which is not correct. we want page number restarting as per document. can you help me how to do this?

@vbhasker please consider the following code

Document doc1 = new Document("Doc1.docx");
Document doc2 = new Document("Doc2.docx");
doc1.AppendDocument(doc2, ImportFormatMode.KeepSourceFormatting);
doc1.Sections[1].PageSetup.RestartPageNumbering = true;
doc1.Save("result.docx");

Doc1.docx (18.2 KB)
Doc2.docx (14.7 KB)

@Vadim.Saltykov

Thanks for your reply and code. it is working for documents. when I have tried the same for pdf documents it is not working as expected. my second pdf is getting appended fully.
here is my code

===================

// Open the document to join.
Document doc1 = new Document(MyDir + "Joining multiple documents - Aspose.Words_10.pdf");
// Append the source document at the end of the destination document.

Document doc2 = new Document(MyDir + "Joining multiple documents - Aspose.Words_20.pdf");
doc1.AppendDocument(doc2, ImportFormatMode.KeepSourceFormatting);

doc1.Sections[1].PageSetup.RestartPageNumbering = true;

my first pdf has 7 pages, second pdf has 7 pages. result shoudl contain 14 pages and number should follow as source format.(1-7, 1-7). but it is coming as (1-8).

Do we have any solution for thiJoining multiple documents - Aspose.Words_10.pdf (85.4 KB)
s?

Thanks
–VijayJoining multiple documents - Aspose.Words_10.pdf (85.4 KB)

@vbhasker “Joining multiple documents - Aspose.Words_10.pdf” is imported with several sections. So you need to remember the number of sections of the first document and set the PageSetup values to the first section of the second document.

Document doc1 = new Document("Joining multiple documents - Aspose.Words_10.pdf");
int sectCount = doc1.Sections.Count;
Document doc1 = new Document(...)

doc1.AppendDocument(doc2, ImportFormatMode.KeepSourceFormatting);
doc1.Sections[sectCount].PageSetup.RestartPageNumbering = true;
doc1.Sections[sectCount].PageSetup.PageStartingNumber = 1;

@Vadim.Saltykov , ok. thanks for your quick reply on this. below code is working as expected. what ever the sample code you provided is not appending second pdf some how.

// Open the document to join.
Document doc1 = new Document(MyDir + "Joining multiple documents - Aspose.Words_1.pdf");
// Append the source document at the end of the destination document.
int sectCount = doc1.Sections.Count;
Document doc2 = new Document(MyDir + "Joining multiple documents - Aspose.Words_2.pdf");
dstDoc.AppendDocument(doc1, ImportFormatMode.KeepSourceFormatting);
dstDoc.AppendDocument(doc2, ImportFormatMode.KeepSourceFormatting);
            
dstDoc.Sections[sectCount].PageSetup.RestartPageNumbering = true;
dstDoc.Sections[sectCount].PageSetup.PageStartingNumber = 1;
dstDoc.Save(ArtifactsDir + "Joining multiple documents - Aspose.Words.pdf", SaveFormat.Pdf);

Thanks
–Vijay

@vbhasker It is perfect that you managed to make it work as expected. Alternatively, you can set RestartPageNumbering flag for the first section of the appended document. Please see the following simple code:

Document doc1 = new Document(@"C:\Temp\in1.docx");
Document doc2 = new Document(@"C:\Temp\in1.docx");
doc2.FirstSection.PageSetup.RestartPageNumbering = true;
doc1.AppendDocument(doc2, ImportFormatMode.KeepSourceFormatting);
doc1.Save(@"C:\Temp\out.docx");