Skip to content

fix(OdfFile): keep the page at continuous section breaks in DOCX -> ODT - #335

Merged
chrip merged 6 commits into
mainfrom
fix/docx-odt-continuous-section-master-page
Nov 26, 2026
Merged

chrip merged 6 commits into
mainfrom
fix/docx-odt-continuous-section-master-page

Conversation

@chrip

@chrip chrip commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

A continuous section break in a DOCX does not start a new page in Word, and LibreOffice imports it as a text section without a page break; only a change of orientation makes it a new page. The DOCX -> ODT writer handled a section with the same column count as the previous one as a page section and gave it a new master page, also when the break was continuous. In ODF, a paragraph that names a master page starts a new page, so each such section started on a new page.

This also affects ODT files that are opened and saved again: the saved text sections come back as continuous section breaks, and LibreOffice then shows an extra page per section or index (a one-page document with a user-defined index can come out with several pages).

The fix keeps the current master page at a continuous section break when the column count and the orientation stay the same and the section has no headers, footers or title page of its own. The first section and all other section breaks are converted as before.

The second commit keeps line numbering: the skipped page conversion also writes the line numbering settings, so a section keeps the page only if it has no line numbering or the previous section has it too.

Tests: two new test_odf cases. DocxToOdt.ContinuousSectionKeepsPage converts a DOCX with four continuous sections (the third switches to landscape) and checks that only the third section starts a new master page. DocxToOdt.ContinuousSectionKeepsLineNumbering converts three continuous sections with line numbering only on the middle one and checks that the line numbering settings are written. Each fails without its commit and passes with it.

Merge order: please merge this before #336.

Assisted-by: ClaudeCode:claude-opus-5-5

🤖 Generated with Claude Code

@DmySyz DmySyz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A kept page also drops everything else in that section's settings. The early return skips the whole conversion, including:

  • w:pgMar: left and right margins;
  • w:pgNumType: page-number restart and format;
  • w:pgBorders, plus footnote and endnote settings, vertical alignment and text direction.

When these match the previous section, as they usually do, nothing is lost.

When they differ, the most common case is a continuous section with different left and right margins, used to indent a block:

  • Before: the block started on a new page (wrong) but had the right margins.
  • Now: it stays on the same page (right) but takes the previous margins (wrong).

LibreOffice keeps the section's properties on the text section it creates (ApplySectionProperties, PropertyMap.cxx:1755).

Options:

  1. Also fall back to the old behaviour when w:pgMar left/right or w:pgNumType differ. Simple, but those sections get the extra page again.
  2. Better: write differing left and right margins as the indents of a text:section.

chrip added a commit that referenced this pull request Nov 18, 2026
A DOCX section becomes a master page in ODT, and the first paragraph of
the section names it. The DOCX to ODT conversion filled that master page
and let the paragraph name it only when the section had a title page, or
when the header and footer references collected so far lacked a header
or lacked a footer. A section for which both a header and a footer were
in force got an empty master page that no paragraph named.

Such a document lost the headers and footers of all sections but the
last, their page size and margins, and the page breaks between the
sections. A document with two sections on separate pages, each with its
own header and footer, was saved as one run of text with the header and
footer of the second section on every page.

The conversion now fills the master page and names it also when the
section starts on a new page. Naming a master page starts a new page in
ODF, so a continuous section gets its own only where a page starts
anyway: at the start of the document, where the page orientation changes
(Word starts a new page there), or, when it has a header or footer
reference of its own, when its first paragraph has a page break before
it. LibreOffice follows the same idea when it imports a DOCX: it applies
the page style of a continuous section at a paragraph with a page break
and never adds a break for it.

Any other continuous section is converted as before. While a header and
a footer are in force and the section has no title page, that means it
stays on the master page before it and does not start a new page; a
header or footer of its own is then not shown, because ODF cannot change
the page style in the middle of a page. The older conditions are
unchanged: a continuous section with a title page, and any section while
the document so far has no header or no footer, still names its master
page.

Not covered: a page break that comes from the paragraph style or from a
break in a run (in the paragraph before the section or at the start of
its first paragraph), and a continuous section that changes the page
size or margins without changing the orientation and has no header or
footer reference of its own.

The condition that is extended here dates from 46947ea.

Two related fixes are part of this, because later sections are now kept:
- The title page setting (`w:titlePg`) is taken from each section. It
  was read from the first converted section for every later one, so a
  later section would have shown the first section's first-page header
  on its own first page, and a later section with its own title page
  lost its first-page header when the first section had none.
- Whether a section starts on a new page is read from its first
  paragraph. It was read from the properties the conversion was called
  with, which for a paragraph with a tracked change of its properties
  are the old ones.

Tests: new `docx2odt_section_header_and_footer.cpp` in test_odf with
eleven tests and eleven small fixtures. Ten of them fail without the
change: `SectionsOnNewPagesKeepHeaderAndFooter`,
`SectionAfterASectionWithOnlyAHeader`,
`SectionOnANewPageInheritsAndKeepsItsMargins`,
`TitlePageOfTheFirstSectionIsNotInherited`,
`TitlePageOfALaterSectionIsKept`,
`ContinuousSectionsAtPageBreaksKeepHeaderAndFooter`,
`ContinuousSectionWithAnotherOrientationGetsItsMasterPage`,
`ContinuousSectionWithOwnReferencesStaysOnThePage`,
`ContinuousSectionWithoutReferencesOrPageBreakNamesNoMasterPage` and
`TrackedChangeOfTheFirstParagraphDoesNotDecideThePageStart`.
`ContinuousSectionWithoutReferencesStaysOnThePage` passes with and
without it and guards the case that must stay as it is.

The full ctest run and the conversion test pass.

Merge order: after #327, which converts each section's headers and
footers once; without it, a document with many sections that have their
own header and footer converts the header and footer parts of all
earlier sections again for every section. Merge with or after
"fix(OdfFile): do not take a body table for a table around a header
drawing", which fixes the position of header drawings in sections that
start with a table; this change gives more sections their own master
page, so more of them would show that problem. It touches
`DocxConverter.cpp`/`.h` next to #335; when both are in, keep both
changes.

Assisted-by: ClaudeCode:claude-fable-5-2
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Christoph Schaefer <christoph.schaefer@nextcloud.com>
@chrip

chrip commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, good catch. I pushed two commits on top, e3da9f6 (the fix) and 967bcdb (one more test). The first two commits are unchanged.

Left and right margins (w:pgMar). I went with your option 2. When a section keeps the page and its left or right margin differs from the page's, the converter now opens a text:section whose section style carries the difference as fo:margin-left / fo:margin-right (DocxConverter.cpp:1964-1986, new odt_conversion_context::set_section_margins at odt_conversion_context.cpp:1167). The next section closes the indents: a continuous one with the page's margins gets a new text:section without indents, and a section that starts a new page does too (DocxConverter.cpp:2344). The indents are measured against the margins of the section that set up the current page, not the one right before, so a run of continuous sections stays correct. One case keeps the old behaviour: if the text is already inside a text:section with columns, the section still starts a new page with its own margins, because a new text:section would drop the columns.

When a section's margin is smaller than the page's, I keep the negative difference instead of clamping it to 0. ODF allows negative values for fo:margin-left/right, Word also lets such a block run into the page margin, and LibreOffice's DOC import doesn't clamp the same difference either.

The fix also adds a flush_section() call at the start of add_section (odt_conversion_context.cpp:1100). Without it, a table whose first paragraph opens a new text:section was moved to the document root, in front of all earlier sections. This could already happen with column sections, and the indents make it more likely. The table now keeps its place in the document, but it ends up at the end of the previous text:section, not in the one it starts, so it takes that section's columns or indents. 967bcdb adds a test for this and corrects the comment.

Page numbers (w:pgNumType). I went with option 1 here. ODF only restarts page numbering when a paragraph also starts a master page (style:page-number), and the number format belongs to the page layout. So a continuous section with w:start, or with a different w:fmt than the previous section, starts a new page again, as it did before the PR (keeps_page, DocxConverter.cpp:143-146).

Borders, footnote/endnote settings, vertical alignment, text direction. For the last three, the converter never read them from a section: the page conversion that the early return skips only has them as commented-out fields (DocxConverter.cpp:2194-2196 and 2334-2335 on this head). So the early return loses nothing there. Page borders, page size and top/bottom margins are page properties, and ODF can only switch them with a new master page, which means a page break. I left those as they are, which matches what LibreOffice's DOCX import does.

LibreOffice. I don't think LibreOffice's DOCX import keeps these properties on the text section. In writerfilter, ApplySectionProperties only sets the writing mode and the endnote setting, and for a continuous section without its own headers, InheritOrFinalizePageStyles reuses the previous page style (sw/source/writerfilter/dmapper/PropertyMap.cxx). So it keeps neither the margins nor a page number restart for such a section. The indents follow Word, which indents such a block on the same page, and LibreOffice's DOC import, which turns the margin difference into section indents (wwSectionManager::InsertSection in sw/source/filter/ww8/ww8par6.cxx).

Tests. I added three test_odf cases:

  • DocxToOdt.ContinuousSectionMarginsBecomeIndents converts five sections (indented, back to the page's margins, indented again, then a next-page break) and checks the master pages and the section indents.
  • DocxToOdt.ContinuousSectionPageNumbersStartPage checks that a restart and a format change start a new page and that a plain continuous section does not.
  • DocxToOdt.SectionStartingWithTableKeepsOrder converts a two-column section followed by a one-column section that starts with a table, and checks that the table stays after the first section's text.

Assisted-by: ClaudeCode:claude-opus-5-5

chrip and others added 4 commits November 19, 2026 14:16
A continuous section break does not start a new page in Word, and
LibreOffice imports it as a text section without a page break; only a
change of orientation turns it into a new page. The ODT writer treated a
section with the same column count as its predecessor as a page section
and gave it a new master page, also when the break was continuous. In ODF
a paragraph that names a master page starts a new page, so every such
section began on a new page. This also hits every ODT that is opened and
saved again, because the saved text sections come back as continuous
section breaks: LibreOffice then shows one extra page per section or
index.

Keep the current master page for a continuous section break when the
column count and the orientation stay the same and the section brings no
headers, footers or title page of its own. The first section and all
other section breaks are converted as before.

Tested with a new test_odf case (DOCX with four continuous sections,
orientation change before the third): it fails without the fix and
passes with it. All ctest suites and the conversion test pass.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Christoph Schaefer <christoph.schaefer@nextcloud.com>
The previous commit skips the page conversion of a continuous section
that keeps the page. That conversion also writes the line numbering
settings, so a document whose line numbering is set only on such a
section lost it. Keep the page only when the section has no line
numbering or the previous section has it too; in that case the previous
section has already written the settings.

Tested with a new test_odf case (three continuous sections, line
numbering only on the middle one): it fails without this change and
passes with it.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Christoph Schaefer <christoph.schaefer@nextcloud.com>
A continuous section that keeps the page skipped its whole page
conversion, so its own left and right margins, page number restart and
page number format were lost when they differed from the previous
section. Before, such a section started a new page but had the right
margins and numbering.

Left and right margins that differ from the page's now become the
indents of a text section (fo:margin-left/right of its section style),
as LibreOffice's DOC import does. The following section starts a new
text section without indents, also when it starts a new page. A
section inside a text section with columns still gets a new page with
its margins, as before.

ODF restarts page numbers and changes their format only with a new
master page, so a continuous section with w:pgNumType w:start or
another number format starts a new page again, as before.

A new text section now first moves elements that are still open (a
table whose first paragraph starts the section) to the end of the
previous section instead of the document root, so they keep their
order; before, they ended up in front of all sections.

Tested with two new test_odf cases, one for the indents and one for the
page numbers: both fail without this change and pass with it.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Christoph Schaefer <christoph.schaefer@nextcloud.com>
…a section

A section that starts with a table opens its text section from the
table's first paragraph, while the table is still open. The previous
commit moves such a table to the end of the previous section instead of
the document root, where it ended up in front of all sections. Add a
test for that and correct the comment in add_section(): the table goes
to the previous section, not to the one it starts.

Tested with a new test_odf case (a two-column section, then a
one-column section that starts with a table): it fails without the
flush_section() call in add_section() and passes with it.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Christoph Schaefer <christoph.schaefer@nextcloud.com>
@chrip

chrip commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main. The only conflicts were in OdfFile/Test/test_odf/CMakeLists.txt and REUSE.toml; both sides' entries are kept. No code changes.

Assisted-by: ClaudeCode:claude-opus-5-5

@chrip
chrip force-pushed the fix/docx-odt-continuous-section-master-page branch from 967bcdb to 41832e5 Compare November 19, 2026 13:44
chrip added a commit that referenced this pull request Nov 24, 2026
)

A DOCX section becomes a master page in ODT, and the first paragraph of
the section names it. The DOCX to ODT conversion filled that master page
and let the paragraph name it only when the section had a title page, or
when the header and footer references collected so far lacked a header
or lacked a footer. A section for which both a header and a footer were
in force got an empty master page that no paragraph named.

Such a document lost the headers and footers of all sections but the
last, their page size and margins, and the page breaks between the
sections. A document with two sections on separate pages, each with its
own header and footer, was saved as one run of text with the header and
footer of the second section on every page.

The conversion now fills the master page and names it also when the
section starts on a new page. Naming a master page starts a new page in
ODF, so a continuous section gets its own only where a page starts
anyway: at the start of the document, where the page orientation changes
(Word starts a new page there), or, when it has a header or footer
reference of its own, when its first paragraph has a page break before
it. LibreOffice follows the same idea when it imports a DOCX: it applies
the page style of a continuous section at a paragraph with a page break
and never adds a break for it.

Any other continuous section is converted as before. While a header and
a footer are in force and the section has no title page, that means it
stays on the master page before it and does not start a new page; a
header or footer of its own is then not shown, because ODF cannot change
the page style in the middle of a page. The older conditions are
unchanged: a continuous section with a title page, and any section while
the document so far has no header or no footer, still names its master
page.

Not covered: a page break that comes from the paragraph style or from a
break in a run (in the paragraph before the section or at the start of
its first paragraph), and a continuous section that changes the page
size or margins without changing the orientation and has no header or
footer reference of its own.

The condition that is extended here dates from 46947ea.

Two related fixes are part of this, because later sections are now kept:
- The title page setting (`w:titlePg`) is taken from each section. It
  was read from the first converted section for every later one, so a
  later section would have shown the first section's first-page header
  on its own first page, and a later section with its own title page
  lost its first-page header when the first section had none.
- Whether a section starts on a new page is read from its first
  paragraph. It was read from the properties the conversion was called
  with, which for a paragraph with a tracked change of its properties
  are the old ones.

Tests: new `docx2odt_section_header_and_footer.cpp` in test_odf with
eleven tests and eleven small fixtures. Ten of them fail without the
change: `SectionsOnNewPagesKeepHeaderAndFooter`,
`SectionAfterASectionWithOnlyAHeader`,
`SectionOnANewPageInheritsAndKeepsItsMargins`,
`TitlePageOfTheFirstSectionIsNotInherited`,
`TitlePageOfALaterSectionIsKept`,
`ContinuousSectionsAtPageBreaksKeepHeaderAndFooter`,
`ContinuousSectionWithAnotherOrientationGetsItsMasterPage`,
`ContinuousSectionWithOwnReferencesStaysOnThePage`,
`ContinuousSectionWithoutReferencesOrPageBreakNamesNoMasterPage` and
`TrackedChangeOfTheFirstParagraphDoesNotDecideThePageStart`.
`ContinuousSectionWithoutReferencesStaysOnThePage` passes with and
without it and guards the case that must stay as it is.

The full ctest run and the conversion test pass.

Merge order: after #327, which converts each section's headers and
footers once; without it, a document with many sections that have their
own header and footer converts the header and footer parts of all
earlier sections again for every section. Merge with or after
"fix(OdfFile): do not take a body table for a table around a header
drawing", which fixes the position of header drawings in sections that
start with a table; this change gives more sections their own master
page, so more of them would show that problem. It touches
`DocxConverter.cpp`/`.h` next to #335; when both are in, keep both
changes.

Assisted-by: ClaudeCode:claude-fable-5-2

Signed-off-by: Christoph Schaefer <christoph.schaefer@nextcloud.com>
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
chrip and others added 2 commits November 26, 2026 09:52
Resolve the conflicts with "keep DOCX sections with a header and a footer
in ODT" (#368), which changed the same section code:

- DocxConverter.h: a section now has bKeepPage (this branch) and bFirst
  and bTurnsPage (main).
- convert_document(): one loop sets all three. bKeepPage keeps its
  meaning (a continuous section after the first one that keeps_page()
  allows).
- convert(CSectionProperty*): main's signature with bPageStart, and the
  three flags read before current_section_properties is reset.

The two rules do not overlap: keeps_page() is false for the first
section, for a section with its own header, footer or title page and
for a change of orientation, which are the cases where main's
own_page_style names a master page for a continuous section.
The test_odf CMakeLists.txt and REUSE.toml keep the entries of both
sides.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Christoph Schaefer <christoph.schaefer@nextcloud.com>
Resolve the conflicts in DocxConverter.cpp with the clang-tidy cleanup on
main: keep this branch's section helpers and bKeepPage, and take main's
nullptr in the constructor and in convert(CSectionProperty*).

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Christoph Schaefer <christoph.schaefer@nextcloud.com>

@DmySyz DmySyz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Description needs an update, otherwise lgtm

@chrip
chrip merged commit c58deb3 into main Nov 26, 2026
25 checks passed
@chrip
chrip deleted the fix/docx-odt-continuous-section-master-page branch November 26, 2026 16:36

This branch was previously deployed

1 inactive deployment
build-cache — db57319d Deployed Nov 26, 2026 by chrip via prune #132
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants