Description:
We are encountering a critical issue where section breaks are being lost or corrupted when using Document.Save() to overwrite DOCX files. This results in improper page layout—multi-page documents collapse into fewer pages, disrupting intended formatting and content flow.
Attachments:
- Correct formatting.docx – Demonstrates expected behavior with section breaks correctly pushing content to the next page, if I skip the doc.save line.
- Wrong formatting.docx – Shows the issue on page 7, where the header image and content appear on the same page due to the missing section break. The section break (Next Page) was present before the save operation but removed afterward.
Code Pattern:
Document doc = new Document(documentFileInfo.FullName);
Something(doc); 
Everything(doc, doc); 
Sample(doc); 
**doc.Save(documentFileInfo.FullName);**
Issue Details:
- The section break is intact before calling doc.Save().
- Our code does not modify section properties.
- The issue consistently occurs after the save operation.
- The expected section break (Next Page) is replaced or removed, causing layout collapse.
Expected Behavior:
Section breaks should remain intact during document processing and saving. Page separation between sections—especially for repetitive variable document or content blocks—must be preserved to maintain document integrity.
Environment:
- Aspose.Words Version: 25.6.0
- Document Format: DOCX
We would appreciate guidance on resolving this issue or confirmation if this is a known bug in the current release.
Wrong formatting.docx (3.8 MB)
Correct formatting.docx (3.8 MB)
             
            
              
              
              
            
            
                
                
              
                
           
          
            
            
              @santhosh.kumaresan
It appears you are encountering an issue where section breaks are lost or corrupted when saving a DOCX document using Aspose.Words version 25.6.0. This can lead to improper page layouts, as you’ve described.
Here are several steps you can take to troubleshoot and potentially resolve this issue:
- 
Check for Modifications: Ensure that none of the methods you are calling (like Something(doc),Everything(doc, doc), orSample(doc)) are inadvertently modifying the section properties or breaks. Review these methods to confirm they do not alter the document structure.
 
- 
Update Aspose.Words: If possible, consider updating to a newer version of Aspose.Words. There may have been bug fixes or improvements related to section handling in later releases. 
- 
Save with Different Options: Try saving the document with different options. For example, you can specify the format explicitly using SaveFormat:
 doc.Save(documentFileInfo.FullName, SaveFormat.Docx);
 
- 
Inspect Section Properties: Before and after the save operation, inspect the section properties to see if they change unexpectedly. Logging the section details can help identify any discrepancies. 
- 
Contact Support: If the issue persists, consider reaching out to Aspose support or checking their forums for similar issues. It may be a known bug, and they could provide a workaround or a fix. 
If you need further assistance, please provide more details about the methods you are using or any specific error messages you encounter.
             
            
              
              
              
            
            
                
                
              
           
          
            
            
              @santhosh.kumaresan The problem is not reproducible using the following simple code:
Document doc = new Document(@"C:\Temp\in.docx");
doc.Save(@"C:\Temp\out.docx");
Most likely, your code modifies the document that causes the problem. Could you please create a simple code that will allow us to reproduce the problem?
             
            
              
              
              
            
            
                
                
              
           
          
            
            
              @alexey.noskov
This is the code. Let me know if you need anything further. When I loop through the code the document is doing as expected but only section break modification is made after it passes through doc.save line.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using Aspose.Words;
using Aspose.Words.Saving;
using Aspose.Words.Tables;
namespace DocumentProcessing.Utility 
{
    public class DocumentProcessor 
    {
        private readonly FileInfo documentFileInfo;
        private bool tableFound;
        
        public DocumentProcessor(string documentPath, bool hasTable = false) 
        {
            this.documentFileInfo = new FileInfo(documentPath);
            tableFound = hasTable;
            
            if (!this.documentFileInfo.Exists) {
                throw new FileNotFoundException($"Document not found: {documentPath}");
            }
        }
      
        internal void ProcessDocument() 
        {
            Document doc = new Document(documentFileInfo.FullName);
            RemoveAnchorParagraphs(doc);
            FixParagraphStyles(doc, doc);
            RemoveDuplicateStyles(doc);
            doc.Save(documentFileInfo.FullName); // Section breaks lost here
        }
        
        private void RemoveAnchorParagraphs(CompositeNode node) 
        {
            List<Node> nodesToRemove = new List<Node>();
            NodeCollection childNodes = node.GetChildNodes(NodeType.Any, false);
            
            for (int i = 0; i < childNodes.Count; i++) 
            {
                Node subNode = childNodes[i];
                if (subNode is Paragraph para) 
                {
                    if (para.ParagraphFormat.PageBreakBefore) {
                        continue;
                    }
                    
                    
                    if (subNode.GetText().Contains("COMPANY_ANCHOR_TEXT")) {
                        nodesToRemove.Add(subNode);
                    }
                    continue;
                }
                if (subNode is CompositeNode compositeNode) {
                    RemoveAnchorParagraphs(compositeNode);
                }
            }
            
            foreach (Node nodeToRemove in nodesToRemove) 
            {
                Paragraph node1 = nodeToRemove.NextSibling as Paragraph;
                node.RemoveChild(nodeToRemove);
                if (node1?.Count == 0) 
                {
                    if (tableFound) 
                    {
                        Paragraph node2 = node1.NextSibling as Paragraph;
                        if (node2?.Count == 0) {
                            node.RemoveChild(node2);
                        }
                    }
                    node.RemoveChild(node1);
                }
            }
        }
        public static void FixParagraphStyles(Document document, CompositeNode node) 
        {
            NodeCollection childNodes = node.GetChildNodes(NodeType.Any, false);
            for (int i = 0; i < childNodes.Count; i++) 
            {
                Node subNode = childNodes[i];
                if (subNode is Paragraph paragraph) 
                {
                    
                    if (paragraph.HasChildNodes) {
                        NodeCollection paraChildNodes = paragraph.GetChildNodes(NodeType.Any, false);
                        for (int j = 0; j < paraChildNodes.Count; j++) {
                            if (paraChildNodes[j] is CompositeNode compNode && compNode.HasChildNodes) {
                                FixParagraphStyles(document, compNode);
                            }
                        }
                    }
                    
                   
                    foreach (Style style in document.Styles) 
                    {
                        string paraText = paragraph.GetText().ToUpper();
                        string styleMarker = "STYLE_" + style.Name.ToUpper() + "\r";
                        
                        if (paraText == styleMarker) 
                        {
                            Paragraph nextParagraph = paragraph.NextSibling as Paragraph;
                            if (nextParagraph != null) 
                            {
                                if (style.Type == StyleType.Character) {
                                    foreach (Run run in nextParagraph.Runs) {
                                        run.Font.Style = style;
                                    }
                                } else if (style.Type == StyleType.Paragraph) {
                                    nextParagraph.ParagraphFormat.Style = style;
                                    nextParagraph.ParagraphFormat.StyleName = style.Name;
                                }
                                node.RemoveChild(paragraph);
                            }
                        }
                    }
                    continue;
                }
                
                if (subNode is CompositeNode compositeNode) {
                    FixParagraphStyles(document, compositeNode);
                }
            }
        }
        private static void RemoveDuplicateStyles(Document document) 
        {
            List<string> stylesToRemove = new List<string>();
            foreach (Style style in document.Styles) {
                if (style.Name.Contains("Normal_"))
                    stylesToRemove.Add(style.Name);
            }
            
            foreach (var styleString in stylesToRemove) {
                foreach (Style style in document.Styles) {
                    if (styleString == style.Name) {
                        style.Remove();
                        break;
                    }
                }
            }
        }
    }
}
             
            
              
              
              
            
            
                
                
              
           
          
            
            
              @santhosh.kumaresan Thank you for additional information. Unfortunately, the problem is still not reproducible on my side. I tested with both hasTable=true/false values.
             
            
              
              
              
            
            
                
                
              
           
          
            
            
              Can we make this forum public, please. My co-workers want to access to this Forum.
Thanks in Advance.
             
            
              
              
              
            
            
                
                
              
           
          
            
            
              By the way, which version of Aspose this was tested?
             
            
              
              
              
            
            
                
                
              
           
          
            
            
              @santhosh.kumaresan I made the forum topic public.
I tested the scenario with the latest 25.9 version of Aspose.Words.
             
            
              
              
              
            
            
                
                
              
           
          
            
            
              @alexey.noskov
Should_Reproduce.zip (31.4 KB)
Can you please try with this “Should_Reproduce.docx” to reproduce the issue? It’s only an issue when it has altchunks and that the Should_Reproduce.docx file has the afchunk2.docx in it.
Thanks
             
            
              
              
              
            
            
                
                
              
           
          
            
            
              @santhosh.kumaresan
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): WORDSNET-28681
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.