Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files from any .NET platform (C#, VB.NET, ASP.NET, .NET Core) and Java applications (J2SE and J2EE) with fast and high quality performance.

Wed Sep 12, 2012 12:19 pm

I would like to add a section at any point in the document, not just at the end.

For Example: I have a document with 9 sections. I want it so open to make changes dynamically from code. I position it in the exact recovering the desired section with

Code: Select all
 Section section = document.Sections [2];

At this point I would like to insert a new section between the section number 2 and number 3 (since then a total of 10 sections), but if I try to do so

Code: Select all
Section section = document.AddSection ();

rightly I add the section at the bottom of the document.

What is the correct way to do what I want?

Posts: 11
Joined: Sun Sep 18, 2011 9:32 pm

Thu Sep 13, 2012 9:03 am

Dear emailregistrazioni,

Thanks for your inquiry.

About your problem, we provide a sample demo. Hope it can help you.
Code: Select all
            Document doc = new Document();
            doc.LoadFromFile(@"sample.docx", FileFormat.Docx);

            Section sec = new Section(doc);
            sec.AddParagraph().AppendText("This is a section inserted");

            doc.Sections.Insert(2, sec);

            doc.SaveToFile(@"result.docx", FileFormat.Docx);

If you have any problems, please feel free to contact us.

Best wishes,
e-iceblue support
User avatar

Posts: 2444
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.Doc