Hi Team,
I am working on merging multiple Word documents.
I have set the section start to Aspose.Words.SectionStart.Continuous
, but the sections are still not appearing as continuous.
Could you please help me resolve this issue in the code?
Code Snippet :
List<string> documentPathList = new List<string>(); //here is the path of all documents
Aspose.Words.Document mainDoc = null;
for (int i = 0; i < documentPathList.Count; i++)
{
var bytes = File.ReadAllBytes(documentPathList[i]);
Aspose.Words.Document doc = new Aspose.Words.Document(new MemoryStream(bytes));
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);
Aspose.Words.Bookmark bookmarkStart = doc.Range.Bookmarks.Where(a => a.Name.Contains("Section") && a.Name.Contains("Start")).FirstOrDefault();
Aspose.Words.Bookmark bookmarkEnd = doc.Range.Bookmarks.Where(a => a.Name.Contains("Section") && a.Name.Contains("End")).FirstOrDefault();
if (bookmarkStart != null)
{
bookmarkStart.Remove();
}
if (bookmarkEnd != null)
{
bookmarkEnd.Remove();
}
builder.MoveToDocumentStart();
builder.MoveToDocumentEnd();
Aspose.Words.SectionStart sectionStart = Aspose.Words.SectionStart.Continuous;
doc.FirstSection.PageSetup.SectionStart = sectionStart;
if (mainDoc == null)
mainDoc = doc;
else
{
if (doc.LastSection.Body.LastChild.NodeType == Aspose.Words.NodeType.Paragraph)
{
var para = new Aspose.Words.Paragraph(doc);
doc.LastSection.Body.AppendChild(para);
}
mainDoc.AppendDocument(doc, Aspose.Words.ImportFormatMode.KeepSourceFormatting);
}
}
mainDoc.Cleanup();
mainDoc.WebExtensionTaskPanes.Clear();
mainDoc.RemoveExternalSchemaReferences();
mainDoc.RemoveMacros();
mainDoc.CompatibilityOptions.OptimizeFor(Aspose.Words.Settings.MsWordVersion.Word2019);
mainDoc.Save(@"Output.docx", Aspose.Words.SaveFormat.Docx);
Output :
Output.docx (31.1 KB)
Documents :
Note 5. REVENUE_Sample.docx (14.1 KB)
Note 4. PROPERTY AND EQUIPMENT_Sample.docx (14.9 KB)
Note 3. INVENTORY_Sample.docx (14.0 KB)