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 Dec 09, 2025 6:08 pm

Hi,
Please explain how to remove page break from document in case this is last element and no visible content after, even brak inside SDT or other object. Please see attached example.

Thanks.

andrei.chorin
 
Posts: 68
Joined: Sun Jan 08, 2023 2:36 pm

Wed Dec 10, 2025 3:05 am

Hello,

Thank you for your inquiry.
To delete the last page break in a document, please refer to the following code:
Code: Select all
                Document doc = new Document();
                doc.loadFromFile("test.docx");

                // Get the body of the first section
                Body body = doc.getSections().get(0).getBody();

                // Get the last child object in the body (e.g., a Paragraph or StructureDocumentTag)
                DocumentObject documentObject = body.getChildObjects().get(body.getChildObjects().getCount() - 1);

                // Get the child objects of this last container
                DocumentObjectCollection childObjects = documentObject.getChildObjects();

                // Traverse child objects in reverse order to find the last paragraph
                for (int j = childObjects.getCount() - 1; j >= 0; j--) {
                    DocumentObject sdtChild = childObjects.get(j);

                    // Check if the child is a Paragraph
                    if (sdtChild instanceof Paragraph) {
                        Paragraph paragraph = (Paragraph) sdtChild;
                        DocumentObjectCollection paraItems = paragraph.getChildObjects();

                        // If the paragraph contains items, check the last one
                        if (paraItems != null && paraItems.getCount() > 0) {
                            int lastIndex = paraItems.getCount() - 1;
                            DocumentObject lastParaItem = paraItems.get(lastIndex);

                            // If the last item is a page break, remove it
                            if (lastParaItem instanceof Break) {
                                paraItems.remove(lastParaItem);
                                System.out.println("Removed the page break at the end of the last paragraph.");
                                break; // Only process the first (i.e., last) paragraph found
                            }
                        }
                        break; // Exit after handling the first paragraph (which is the last one in reverse order)
                    }
                }
        doc.saveToFile("test_modified.docx");
Sincerely,
Talia
E-iceblue support team
User avatar

talia.liu
 
Posts: 341
Joined: Mon Apr 14, 2025 3:33 am

Fri Dec 12, 2025 9:40 am

Hello,

I hope this message finds you well.
Has your issue been resolved? Could you spare a moment to give me some feedback when you're available? Thank you very much.
Sincerely,
Talia
E-iceblue support team
User avatar

talia.liu
 
Posts: 341
Joined: Mon Apr 14, 2025 3:33 am

Return to Spire.Doc

cron