I have multiple word file when i merge them I found some data movement .
While merging document new docuement merging from same page i want to merge it from new page.
Now while we merge document sometime page number is not continuous it take the same page number of their document.
Aspose.Words.License licWord = new Aspose.Words.License();
string strLicenscePath = "D:\\DemoCodes\\ConsoleApp\\MergingWordDocument\\License\\Aspose.Words.NET.lic";
licWord.SetLicense(strLicenscePath);
Aspose.Words.Document mainDoc = new Aspose.Words.Document();
mainDoc.RemoveAllChildren();
byte[] bytesectionData;
for (int i = 1; i < 6; i++)
{
bytesectionData = File.ReadAllBytes(@"C:\\Users\\Abc\\Desktop\\Orbis\\Section" + i + ".docx");
Aspose.Words.Document doc = new Aspose.Words.Document(new MemoryStream(bytesectionData));
mainDoc.AppendDocument(doc, ImportFormatMode.KeepSourceFormatting);
}
mainDoc.Save("C:\\Users\\Abc\\Desktop\\Output.docx");
section1.docx (1.8 MB)
section2.docx (65.7 KB)
section3.docx (1.8 MB)
section4.docx (1.8 MB)
section5.docx (77.8 KB)
Output.docx (92.7 KB)

@Jayshiv1408 To fix the page numbering please insert the following code after the for
loop that you are using to append the sections:
...
foreach (Section sec in mainDoc.Sections)
{
sec.PageSetup.PageStartingNumber = 1;
sec.PageSetup.RestartPageNumbering = false;
}
...
Regarding the position of the second section document, please note that the AppendDocument
method inserts a section break continuous in the document. Therefore, the new document will be inserted in the last page of the target document, and this is the same behavior for all the documents that you are appending. To fix this, specifically for the section2
document, you should modify the for
loop that you are using to append the sections:
...
DocumentBuilder builder = new DocumentBuilder(mainDoc);
for(int i = 1; i < 6; i++)
{
Aspose.Words.Document section = new Aspose.Words.Document($"C:\\Temp\\section{i}.docx");
mainDoc.AppendDocument(section, ImportFormatMode.KeepSourceFormatting);
if (i == 1)
{
builder.MoveTo(mainDoc.LastSection.Body.LastParagraph);
builder.InsertBreak(BreakType.PageBreak);
mainDoc.FirstSection.Remove();
}
}
...
@eduardo.canal Thanks For your response.
Yes I got your point so is there any way so that every new document will merge from New Page.The Solution is working proper only for section 2 as you mention above.
@Jayshiv1408 To achieve this you should simply set doc.FirstSection.PageSetup.SectionStart
to SectionStart.NewPage
for every document you append. In this case the appended document will start from new page. See the following code for example:
string[] docs = new string[] { @"C:\Temp\in1.docx", @"C:\Temp\in2.docx", @"C:\Temp\in3.docx", @"C:\Temp\in4.docx" };
Document result = null;
foreach (string path in docs)
{
Document doc = new Document(path);
// Configure the first section of the source document to start from a new page,
// so each appended document starts from a new page.
doc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
if (result == null)
result = doc;
else
result.AppendDocument(doc, ImportFormatMode.KeepSourceFormatting);
}
result.Save(@"C:\Temp\out.docx");
FYI @eduardo.canal
1 Like