Hi,
Can anyone please give the proper solution for my below requirement. I need to create one word doc with four sections. After that i need to get that same four sections by loading the saved document.Is it possible by using Aspose.Words for .NET.
Hi there,
Thanks for your inquiry. Yes, you can create Word document with multiple sections by using Aspose.Words and load the same created document in Aspose.Words DOM. Please check the following code examples and documentation links for your kind reference. Hope this helps you.
https://reference.aspose.com/words/net/aspose.words/section/
https://docs.aspose.com/words/net/working-with-sections/
// Create an "empty" document. Note that like in Microsoft Word,
// the empty document has one section, body and one paragraph in it.
Document doc = new Document();
// This truly makes the document empty. No sections (not possible in Microsoft Word).
doc.RemoveAllChildren();
// Create a new section node.
// Note that the section has not yet been added to the document,
// but we have to specify the parent document.
Section section = new Section(doc);
// Append the section to the document.
doc.AppendChild(section);
// Lets set some properties for the section.
section.PageSetup.SectionStart = SectionStart.NewPage;
section.PageSetup.PaperSize = PaperSize.Letter;
// The section that we created is empty, lets populate it. The section needs at least the Body node.
Body body = new Body(doc);
section.AppendChild(body);
// The body needs to have at least one paragraph.
// Note that the paragraph has not yet been added to the document,
// but we have to specify the parent document.
// The parent document is needed so the paragraph can correctly work
// with styles and other document-wide information.
Paragraph para = new Paragraph(doc);
body.AppendChild(para);
// We can set some formatting for the paragraph
para.ParagraphFormat.StyleName = "Heading 1";
para.ParagraphFormat.Alignment = ParagraphAlignment.Center;
// So far we have one empty paragraph in the document.
// The document is valid and can be saved, but lets add some text before saving.
// Create a new run of text and add it to our paragraph.
Run run = new Run(doc);
run.Text = "Hello World!";
run.Font.Color = System.Drawing.Color.Red;
para.AppendChild(run);
// As a matter of interest, you can retrieve text of the whole document and
// see that \x000c is automatically appended. \x000c is the end of section character.
Console.WriteLine("Hello World!\x000c", doc.GetText());
// Save the document.
doc.Save(MyDir + "Section.CreateFromScratch Out.doc");
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("Section 1");
builder.InsertBreak(BreakType.SectionBreakNewPage);
builder.Writeln("Section 2");
builder.InsertBreak(BreakType.SectionBreakNewPage);
builder.Writeln("Section 3");
builder.InsertBreak(BreakType.SectionBreakNewPage);
builder.Writeln("Section 4");
builder.InsertBreak(BreakType.SectionBreakNewPage);
doc.Save(MyDir + "Out.docx", SaveFormat.Docx);
Document doc1 = new Document(MyDir + "Out.docx");
doc1.Save(MyDir + "Out.pdf", SaveFormat.Pdf);
Moreover, Section can have one Body and maximum one HeaderFooter of each HeaderFooterType. Body and HeaderFooter nodes can be in any order inside Section.
A minimal valid section needs to have Body with one Paragraph. Each section has its own set of properties that specify page size, orientation, margins etc.
You can create a copy of a section using Clone. The copy can be inserted into the same or different document. To add, insert or remove a whole section including section break and section properties use methods of the Sections object.
To copy and insert just content of the section excluding the section break and section properties use AppendContent and PrependContent methods.
If you still face any issue, please manually create your expected Word document using Microsoft Word and attach it here for our reference. We will investigate, how you want your final Word output be generated like. We will then provide you more information on this along with code.