Skip to content

fix(OdfFile): start a new page at every master page switch in ODT -> DOCX - #336

Merged
chrip merged 6 commits into
mainfrom
fix/odt-docx-master-page-break
Nov 27, 2026
Merged

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

Conversation

@chrip

@chrip chrip commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

In ODF, a paragraph or table whose style names a master page (style:master-page-name) starts a new page, also when that master page is the one already in use. The ODT -> DOCX conversion only wrote a section break when both the master page name and its page layout changed. If the page style stayed the same, or a different page style had the same layout, the page break was lost and the following text ran on on the same page, so the converted document had fewer pages than in LibreOffice.

The fix writes a page break before every paragraph or table in the body that names a master page. Like LibreOffice, it:

  • skips the first paragraph of the document, hidden sections and anything inside a table, where ODF ignores the attribute;
  • takes the master page from the nearest parent style that sets one, and stops at an empty value, which overrides the parent;
  • also checks paragraphs inside tables of contents and other indexes;
  • ignores the name of a master page that does not exist.

The section handling for real page style changes stays as it was.

Changes:

  1. Writes the page break at every master page switch in the body.
  2. Follows inherited master pages and checks paragraphs in indexes.
  3. Keeps empty master page names when the styles are read. Before, they were dropped while parsing, so an empty value could not stop the inheritance. master_page_name_by_name() still returns no master page for an empty name, so existing callers behave as before.
  4. Writes the break once and only where it applies. A paragraph whose style already sets fo:break-before="page" gets no second break (that added an empty page). A table style with fo:break-before="auto" no longer cancels the break of the table's master page. Missing master pages and master page names inside table cells no longer break the page.

For a table, the break is written as w:pageBreakBefore on the first paragraph of the table's first cell. This holds whatever the style of that paragraph. The same applies to a table with fo:break-before="page", which before got a page break run in its first cell (ignored by LibreOffice) unless the paragraph had an automatic style with paragraph properties.

Tests: four test_odf cases with two small ODT fixtures. OdtToDocxMasterPageBreak.TableBreakIsPageBreakBeforeInFirstCell checks w:pageBreakBefore and no page break run for a table whose first cell paragraph has a named style, an automatic style without and with paragraph properties. OdtToDocxMasterPageBreak.PageLayoutChangeGivesNoEmptyPage checks that a master page with another page layout gives w:pageBreakBefore and no page break run. OdtToDocxMasterPageBreak.MasterPageNameStartsNewPage checks the break for the same master page on a later paragraph, a master page inherited from the parent style and a paragraph inside a table of contents, and checks that no break is written on the first paragraph, inside a hidden section or after an empty override. OdtToDocxMasterPageBreak.MasterPageBreakOnceAndOnlyOnPages checks the single break for a style with fo:break-before="page" (also through an automatic style), a missing master page, a table with fo:break-before="auto" and a master page name inside a table cell. Each case fails without the part of the change that handles it, and both tests pass with the whole change. All ctest suites and the conversion test pass.

Merge order: please merge #335 first. Today the editor saves text sections in ODT as master page switches, which that PR fixes; without it, this change would show those switches as page breaks after a save.

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

🤖 Generated with Claude Code

…DOCX

A paragraph or table whose style sets style:master-page-name starts a
new page (ODF 1.3 part 3, 19.501), also when the named master page is
the one already in use or has the same page layout. The DOCX writer only
started a new section when the master page name and its page layout both
changed, so in all other cases the page break was lost and the text ran
on on the same page.

Write a page break before every body paragraph or table that names a
master page, except before the first paragraph of the body, inside
hidden sections and inside tables. As in LibreOffice, take the master
page from the nearest parent style that sets it, stop at an empty value,
check paragraphs inside tables of contents and other indexes, and ignore
names of master pages that do not exist. The reader now keeps empty
master page names so that they can stop the inheritance;
master_page_name_by_name() still returns no master page for them.

No second break is written when the paragraph style already breaks the
page with fo:break-before="page", and fo:break-before="auto" on a table
style no longer cancels the break of the table's master page. The
existing section handling is unchanged.

Tested with two new test_odf cases and a fixture that covers each of
these cases; they fail without this change and pass 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>

@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 note from Claude (if you think that it shouldn't block this PR - can be reconsidered):

Worth checking before merge

A real page-style change from a non-automatic paragraph style may give an empty page.

  • When the master page name and its layout both change, the existing code still writes a section break (w:sectPr, next page). This PR also sets the break value 2 on the same paragraph.
  • Automatic style: the break becomes w:pageBreakBefore on the first paragraph of the new section. Word and LibreOffice don't add a page for that, since the paragraph is already at the top of a page.
  • Named style (or an automatic style without paragraph properties): the properties aren't written inline, so the value survives to paragraph::docx_convert and becomes a <w:br w:type="page"/> run at the start of the paragraph (text_elements.cpp:394-402).
    • An explicit page break right after a next-page section break gives an empty page in Word.
    • LibreOffice lets named styles carry a page style ("Breaks → With page style", e.g. on a chapter heading style), so this can happen.
  • The tests don't combine a page-layout change with the new break; the PR says section handling is unchanged.
  • Suggestion: add a fixture where a named style switches to a master page with a different layout, and check there is no <w:br w:type="page"/>. Or skip setting the break when a section break is already pending for that paragraph.

Minor

  1. List paragraphs: break_before_master_page runs on top-level body elements, section children and index entries, but not on paragraphs inside a text:list. For a list, the element's style is the list style, so nothing is found. A list paragraph naming a master page (rare) still gets no break.
  2. Lookup ignores the style family: master_page_name_ is keyed by style name only. The new lookup passes the family only for walking parents. That's older behaviour and harmless as long as a table style and a paragraph style don't share a name.

… -> DOCX

When a master page switch also changes the page layout, the paragraph
before it still ends a section. A paragraph with an automatic style that
has paragraph properties got the new page break as w:pageBreakBefore,
which adds no page when the paragraph already starts one. A paragraph
with a named style, or with an automatic style without paragraph
properties, got a page break run at its start instead. After a section
break that starts a new page, for example one that changes the page
size or orientation, that run gives an empty page.

Write the break as w:pageBreakBefore for these paragraphs too, right
after w:pStyle. Paragraphs inside tables, drawings and notes keep the
old handling. The section handling is unchanged.

Tested with a new test_odf case and fixture: a named style and an
automatic style without paragraph properties switch to a landscape
master page and back. It fails without this change and passes with it;
an automatic style with paragraph properties guards the path that was
already right.

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 18, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, good catch. The main point holds, and I fixed it in 9a4194d.

Empty page after a page layout change

Confirmed. For a named style, or an automatic style without paragraph properties, process_paragraph_attr writes no paragraph properties inline, so the break value 2 survives to paragraph::docx_convert and becomes the <w:br w:type="page"/> run (text_elements.cpp:394-402), right after the section break in the previous paragraph.

One detail is different from what you describe: that section break is w:type="continuous", not next page. nextPage is only written when get_page_break() is set (styles.cpp:1646), and nothing ever calls set_page_break(true). Word starts a new page at such a break only when the page size or orientation changes. With a change that keeps the size (other margins, or just a different page layout: compare_page_properties treats any two page layouts as different), the text stays on the same page. So skipping the break whenever a section break is pending would lose the new page in those cases.

Instead, the break is now always written as w:pageBreakBefore, as the automatic-style path already did:

  • named style: right after w:pStyle in process_paragraph_attr (docx_conversion_context.cpp:2332)
  • automatic style without paragraph properties: in serialize_paragraph_style (docx_conversion_context.cpp:1786, 1804-1810)
  • shared check in take_page_break_before (docx_conversion_context.cpp:1533). Paragraphs in tables, drawings and notes are left alone.

As you said, w:pageBreakBefore adds no page when the paragraph is already at the top of one, so this gives a new page in both cases and never an empty one. The section handling is unchanged.

Test: OdtToDocxMasterPageBreak.PageLayoutChangeGivesNoEmptyPage with a new fixture, master_page_layout_break.odt. A named style switches to a landscape master page, an automatic style without properties switches back, and an automatic style with properties switches again. The test checks that there is no page break run anywhere, that each of these paragraphs has w:pageBreakBefore, and that there are four w:sectPr in the expected places. It fails without the fix.

Minor 1: list paragraphs

Confirmed. Strictly, text:list has no element_style_name at all (only text:p, text:h and table:table set it), so break_before_master_page returns at text_elements.cpp:932. Covering it means hooking into the list items, with extra guards for lists inside frames, notes and comments. That is more than I want to add to this PR for a rare case, so I'd leave it for a follow-up.

Minor 2: lookup ignores the style family

Agreed, and it predates this PR. On main, add_master_page_name stores by style name only (odfcontext.cpp:276-278) and master_page_name_by_name looks up by name only (odfcontext.cpp:337-344). The new lookup keeps that map and uses the family only to walk the parents. I left it as is.

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

@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.

Thanks for the fix. There may or may not be one more issue:

The description says "for a table, the break is written as w:pageBreakBefore on the first paragraph of the table's first cell". By reading the code, that only happens when that paragraph has an automatic style with paragraph properties.

How the value travels:

  • break_before_master_page sets the value 2 before the table starts.
  • take_page_break_before returns false inside a table.
  • A style-less cell paragraph (like text:p in the fixture's table T1) writes no w:pPr at all (process_paragraph_attr returns 0).
  • A named one, such as LibreOffice's default "Table Contents", takes the w:pStyle path.

In both cases the value reaches text_elements.cpp:394, which writes <w:br w:type="page"/> inside the first cell.

Why the test passes anyway: has_page_break() accepts a page-break run as well as w:pageBreakBefore, so MasterPageBreakOnceAndOnlyOnPages passes either way.

Why the form matters (hand-made DOCX, a paragraph followed by a table):

LibreOffice Euro-Office (sdkjs)
Page-break run ignored, table stays on page 1 ignored
w:pageBreakBefore table starts on a new page ignored

Word also uses w:pageBreakBefore on the first cell's paragraph for "table starts on a new page". The editor ignores both forms; that's a separate sdkjs issue.

Suggested fix:

  • Write w:pageBreakBefore for the first paragraph of the first cell whatever its style: let the pending table break through take_page_break_before there, and open a w:pPr for the style-less case.
  • Make the table assertion check for w:pageBreakBefore and the absence of a run.

This path already existed for tables with fo:break-before="page". But this PR sends master-page tables through it, and its description says otherwise.

Note: the table finding comes from reading the code. Running the existing test with the stricter assertion would confirm or refute it.

chrip and others added 2 commits November 18, 2026 16:17
…age-break

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>
…t cell

A table that starts a new page (a master page switch or fo:break-before="page")
leaves the break pending for the first paragraph of its first cell. Unless that
paragraph had an automatic style with paragraph properties, the break was
written as a page break run inside the cell. LibreOffice and Word ignore such a
run, so the table stayed on the previous page.

Let the pending break through take_page_break_before inside tables too, and
write w:pPr with w:pageBreakBefore for a paragraph without a style. Header
and footer paragraphs keep the old output.

The fixture gets tables whose first cell paragraph has a named style and an
automatic style without and with paragraph properties. The table checks now
require w:pageBreakBefore and no page break run.

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 18, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, you were right. I checked it with a build of the PR head: for a table after a master page switch, the first cell paragraph got <w:br w:type="page"/> when it had no style, a named style, or an automatic style without paragraph properties. Only an automatic style with paragraph properties got w:pageBreakBefore.

Fixed in 38c2588, as you suggested:

  • take_page_break_before now lets the pending break through inside a table (docx_conversion_context.cpp:1537). A pending break there can only come from the table itself. Headers and footers are left as they were.
  • A cell paragraph without a style now gets <w:pPr><w:pageBreakBefore w:val="true"/></w:pPr> (docx_conversion_context.cpp:2454). The named style and automatic style paths already write it once the break gets through.

This also fixes the same case for tables with fo:break-before="page", which used this path before.

Tests: the table check in MasterPageBreakOnceAndOnlyOnPages now requires w:pageBreakBefore and no page break run. The new TableBreakIsPageBreakBeforeInFirstCell covers a first cell paragraph with a named style ("Table Contents"), an automatic style without paragraph properties and one with them. It also checks that the second paragraph in the cell gets no break and that document.xml has no page break run at all. Both fail without the fix.

I also merged main into the branch (cbf22a2), because the fix touches the same lines as the header/footer spacing change on main. Both are kept.

Locally test_odf passes (342 tests) and the conversion test shows no snapshot changes.

Could you take another look?

Resolve two conflicts:
- office_text.cpp: main added the check for a page style change after a
  table (#369) before each body element is converted; this branch adds
  the master page break there. Keep both: the table check first, then
  the break, then the conversion.
- test_odf CMakeLists.txt: keep the entries of this branch and the ones
  added on main.

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 26, 2026

Copy link
Copy Markdown
Contributor Author

@DmySyz the table point from your second review is fixed in 38c2588 (details in my comment from 18 Nov): the first paragraph of the table's first cell gets w:pageBreakBefore whatever its style, and the test now fails on a page break run. On 25 Nov I merged main into the branch again (4109130), without conflicts. Could you take another look?

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

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 enabled auto-merge (squash) November 27, 2026 16:14
@chrip
chrip deployed to build-cache November 27, 2026 16:24 — with GitHub Actions Active
@chrip
chrip deployed to build-cache November 27, 2026 16:46 — with GitHub Actions Active
@chrip
chrip deployed to build-cache November 27, 2026 16:56 — with GitHub Actions Active
@chrip
chrip deployed to build-cache November 27, 2026 17:04 — with GitHub Actions Active
@chrip
chrip deployed to build-cache November 27, 2026 17:31 — with GitHub Actions Active
@chrip
chrip merged commit 417b55f into main Nov 27, 2026
25 checks passed
@chrip
chrip deleted the fix/odt-docx-master-page-break branch November 27, 2026 17:31
@chrip
chrip deployed to build-cache November 27, 2026 17:31 — with GitHub Actions Active

This branch was successfully deployed

1 active deployment
build-cache — df2cffde Deployed Nov 27, 2026 by chrip via prune #143
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