Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue Feb 09, 2021 2:16 pm

Hi Team,

We have requirement where we need to remove all section breaks from the whole document (.docx) file and after that the parsing should start. Having multiple sections is increasing the loop count and impacting the performance. So kindly let us know if the section breaks can be removed and there should be only one section throughout the docx.

Preferred Language: Java

Thanks in advance!!

pr20080798
 
Posts: 148
Joined: Wed Jan 20, 2021 1:15 pm

Wed Feb 10, 2021 6:26 am

Hello,

Thanks for your inquiry!

Please refer to the following code to remove the section breaks and let the docx file keep only one section.
Code: Select all
   Document doc = new Document();
        doc.loadFromFile("Break.docx");
       
       
        Section sec = doc.getSections().get(0);
        int sections = doc.getSections().getCount() - 1;
       
        for (int i = 0 ; i <sections ; i++)
        {
           Section section = doc.getSections().get(1);
            for (int j = 0; j < section.getBody().getChildObjects().getCount(); j++)
            {
                sec.getBody().getChildObjects().add(section.getBody().getChildObjects().get(j).deepClone());
            }
            doc.getSections().remove(section);
        }
       
        doc.saveToFile("removeBreak.docx", FileFormat.Docx);

If the code does not meet your needs, please provide us with your input file and your expected results for further investigation.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Mon Feb 22, 2021 4:28 am

Thanks for the update. This code works fine.

pr20080798
 
Posts: 148
Joined: Wed Jan 20, 2021 1:15 pm

Mon Feb 22, 2021 9:12 am

Hello,

Thanks for your feedback!

Glad to hear that the issue has been solved.

If you encounter any issues related to our product in the future, just feel free to contact us.

Have a nice day!

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Return to Spire.Doc