Skip to content

Commit be052f5

Browse files
knilecrackclaude
andcommitted
Fix SplitOnSections to properly handle body-level w:sectPr as section boundary
Previously, body-level w:sectPr elements were incorrectly included as block-level content when computing section ranges. This caused the final section's headers/footers/page settings to be merged into the previous section when the last section was defined only by body-level section properties. The fix filters out w:sectPr elements from the body's child elements before processing, since they are section properties rather than block-level content. This ensures proper section boundary detection and prevents sectPr elements from being counted in element ranges. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 5bd381a commit be052f5

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

OpenXmlPowerTools/DocumentBuilder.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,16 @@ public static IEnumerable<WmlDocument> SplitOnSections(WmlDocument doc)
242242
using OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(doc);
243243
using WordprocessingDocument document = streamDoc.GetWordprocessingDocument();
244244
XDocument mainDocument = document.MainDocumentPart.GetXDocument();
245-
var divs = mainDocument
245+
// Filter out body-level w:sectPr elements as they are section properties, not block-level content
246+
// A body-level sectPr marks a section boundary and should not be included in element counts
247+
var bodyElements = mainDocument
246248
.Root
247249
.Element(W.body)
248250
.Elements()
251+
.Where(e => e.Name != W.sectPr)
252+
.ToList();
253+
254+
var divs = bodyElements
249255
.Select((p, i) => new Atbi
250256
{
251257
BlockLevelContent = p,

0 commit comments

Comments
 (0)