From b968af22381040736bb05613e52d2ee054c4fc10 Mon Sep 17 00:00:00 2001 From: Christoph Schaefer Date: Mon, 16 Nov 2026 09:05:19 +0100 Subject: [PATCH 1/3] fix(OdfFile): start a new page at every master page switch in ODT -> 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 Signed-off-by: Christoph Schaefer --- .../Converter/docx_conversion_context.h | 1 + OdfFile/Reader/Format/odf_document_impl.cpp | 2 +- OdfFile/Reader/Format/odfcontext.cpp | 27 +++- OdfFile/Reader/Format/odfcontext.h | 3 + OdfFile/Reader/Format/office_text.cpp | 2 + .../Reader/Format/style_table_properties.cpp | 3 +- OdfFile/Reader/Format/styles.cpp | 8 - OdfFile/Reader/Format/text_elements.cpp | 41 +++++ OdfFile/Reader/Format/text_elements.h | 3 + OdfFile/Test/test_odf/CMakeLists.txt | 1 + .../ExampleFiles/master_page_break.odt | Bin 0 -> 1663 bytes .../test_odf/odt2docx_master_page_break.cpp | 145 ++++++++++++++++++ REUSE.toml | 1 + 13 files changed, 226 insertions(+), 11 deletions(-) create mode 100644 OdfFile/Test/test_odf/ExampleFiles/master_page_break.odt create mode 100644 OdfFile/Test/test_odf/odt2docx_master_page_break.cpp diff --git a/OdfFile/Reader/Converter/docx_conversion_context.h b/OdfFile/Reader/Converter/docx_conversion_context.h index 3e9802d523..bec17c4b20 100644 --- a/OdfFile/Reader/Converter/docx_conversion_context.h +++ b/OdfFile/Reader/Converter/docx_conversion_context.h @@ -997,6 +997,7 @@ class docx_conversion_context : boost::noncopyable styles_map styles_map_; std::unordered_map status_para{}; bool process_headers_footers_; + bool body_started_ = false; // a body paragraph has been written bool delayed_converting_; bool convert_delayed_enabled_; diff --git a/OdfFile/Reader/Format/odf_document_impl.cpp b/OdfFile/Reader/Format/odf_document_impl.cpp index 5b6be3bddc..d80800ecc5 100644 --- a/OdfFile/Reader/Format/odf_document_impl.cpp +++ b/OdfFile/Reader/Format/odf_document_impl.cpp @@ -980,7 +980,7 @@ void odf_document::Impl::parse_styles(office_element *element) continue; } - if (styleInst->style_master_page_name_ && !styleInst->style_master_page_name_->empty()) + if (styleInst->style_master_page_name_) context_->styleContainer().add_master_page_name(styleInst->style_name_, *styleInst->style_master_page_name_); context_->styleContainer().add_style(styleInst->style_name_, diff --git a/OdfFile/Reader/Format/odfcontext.cpp b/OdfFile/Reader/Format/odfcontext.cpp index b8d5e1bd40..dac22c3ccb 100644 --- a/OdfFile/Reader/Format/odfcontext.cpp +++ b/OdfFile/Reader/Format/odfcontext.cpp @@ -338,11 +338,36 @@ namespace cpdoccore { { _CP_OPT(std::wstring) master_page; + // An empty name only stops the inheritance (see master_page_name_inherited), it is no master page. map_wstring_wstring::const_iterator res = master_page_name_.find(StyleName); - if (res != master_page_name_.end()) + if (res != master_page_name_.end() && !res->second.empty()) master_page = res->second; return master_page; } + + _CP_OPT(std::wstring) styles_container::master_page_name_inherited(const std::wstring& StyleName, + odf_types::style_family::type Type) const + { + // LibreOffice keeps the master page as a paragraph attribute, so child styles inherit it. + // An empty value stops the inheritance. The depth limit guards against parent cycles. + std::wstring name = StyleName; + style_instance* inst = style_by_name(StyleName, Type, false); + + for (int depth = 0; depth < 32; depth++) + { + map_wstring_wstring::const_iterator res = master_page_name_.find(name); + if (res != master_page_name_.end()) + { + if (res->second.empty()) + return boost::none; + return res->second; + } + if (!inst || !(inst = inst->parent())) + break; + name = inst->name(); + } + return boost::none; + } void styles_container::set_new_name_master_page(std::wstring ws_name_layout, std::wstring ws_new_name) { diff --git a/OdfFile/Reader/Format/odfcontext.h b/OdfFile/Reader/Format/odfcontext.h index cbf467c633..373311a790 100644 --- a/OdfFile/Reader/Format/odfcontext.h +++ b/OdfFile/Reader/Format/odfcontext.h @@ -178,6 +178,9 @@ class styles_container style_instance * style_default_by_type (odf_types::style_family::type Type) const; const _CP_OPT(std::wstring) master_page_name_by_name(const std::wstring & StyleName) const; + // style:master-page-name of the style or the nearest parent style that sets it; none when empty + _CP_OPT(std::wstring) master_page_name_inherited(const std::wstring & StyleName, + odf_types::style_family::type Type) const; instances_array & instances() { return instances_; } diff --git a/OdfFile/Reader/Format/office_text.cpp b/OdfFile/Reader/Format/office_text.cpp index 6ac2124fef..51daaaa9ee 100644 --- a/OdfFile/Reader/Format/office_text.cpp +++ b/OdfFile/Reader/Format/office_text.cpp @@ -32,6 +32,7 @@ #include "serialize_elements.h" #include "odfcontext.h" +#include "text_elements.h" #include "style_paragraph_properties.h" namespace cpdoccore { @@ -273,6 +274,7 @@ void office_text::docx_convert(oox::docx_conversion_context & Context) } } } + text::break_before_master_page(Context, content_[i]); content_[i]->docx_convert(Context); } Context.end_office_text(); diff --git a/OdfFile/Reader/Format/style_table_properties.cpp b/OdfFile/Reader/Format/style_table_properties.cpp index a2052ba9ba..9376b02ee8 100644 --- a/OdfFile/Reader/Format/style_table_properties.cpp +++ b/OdfFile/Reader/Format/style_table_properties.cpp @@ -104,7 +104,8 @@ void table_format_properties::docx_convert(oox::docx_conversion_context & Contex _tblPr << L""; } - if (common_break_attlist_.fo_break_before_) + // A master page switch has already asked for a new page (2); fo:break-before="auto" must not cancel it + if (common_break_attlist_.fo_break_before_ && Context.get_page_break_before() != 2) { Context.set_page_break_before(common_break_attlist_.fo_break_before_->get_type()); } diff --git a/OdfFile/Reader/Format/styles.cpp b/OdfFile/Reader/Format/styles.cpp index 00540e9e9b..d96ac1e545 100644 --- a/OdfFile/Reader/Format/styles.cpp +++ b/OdfFile/Reader/Format/styles.cpp @@ -637,14 +637,6 @@ void style::add_attributes( const xml::attributes_wc_ptr & Attributes ) else style_default_outline_level_= XmlUtils::GetInteger(*sTest); } - - if (style_master_page_name_) - { - if (style_master_page_name_->empty()) - { - style_master_page_name_.reset(); - } - } } void style::add_child_element( xml::sax * Reader, const std::wstring & Ns, const std::wstring & Name) diff --git a/OdfFile/Reader/Format/text_elements.cpp b/OdfFile/Reader/Format/text_elements.cpp index 35068e945c..ba197a795b 100644 --- a/OdfFile/Reader/Format/text_elements.cpp +++ b/OdfFile/Reader/Format/text_elements.cpp @@ -476,6 +476,10 @@ void paragraph::docx_convert(oox::docx_conversion_context & Context, _CP_OPT(std Context.end_paragraph_style(); Context.finish_paragraph(); + if (!Context.process_headers_footers_ && !in_drawing && + Context.get_process_note() == oox::docx_conversion_context::noNote) + Context.body_started_ = true; + if (next_masterPageName) { if (true == Context.set_master_page_name(*next_masterPageName)) @@ -802,6 +806,7 @@ void section::add_child_element( xml::sax * Reader, const std::wstring & Ns, con void section::docx_convert(oox::docx_conversion_context & Context) { bool bAddSection = false; + const bool bHidden = section_attr_.display_ && section_attr_.display_->get_type() == odf_types::text_display::None; if ( false == Context.get_drawing_state_content()) { std::wstring current_page_properties = Context.get_page_properties(); @@ -860,6 +865,8 @@ void section::docx_convert(oox::docx_conversion_context & Context) { Context.set_implicit_end(true); } + if (!bHidden && bAddSection) + break_before_master_page(Context, content_[i]); content_[i]->docx_convert(Context); } if (bAddSection) @@ -918,6 +925,38 @@ void index_body::add_child_element( xml::sax * Reader, const std::wstring & Ns, CP_CREATE_ELEMENT(content_); } +void break_before_master_page(oox::docx_conversion_context & Context, const office_element_ptr & elm) +{ + // style:master-page-name always starts a new page, also when the page style stays the same. + // It is ignored inside tables. + if (!elm || !elm->element_style_name || !Context.body_started_ || Context.process_headers_footers_ + || Context.get_table_context().in_table()) + return; + + const bool is_table = elm->get_type() == typeTableTable; + const odf_types::style_family::type family = is_table ? + odf_types::style_family::Table : odf_types::style_family::Paragraph; + + odf_reader::odf_read_context & odf_context = Context.root()->odf_context(); + _CP_OPT(std::wstring) master_page = + odf_context.styleContainer().master_page_name_inherited(*elm->element_style_name, family); + + // Like LibreOffice, ignore the name of a master page that does not exist + if (!master_page || !odf_context.pageLayoutContainer().master_page_by_name(*master_page)) + return; + + if (!is_table) + { + // A paragraph style that breaks the page already writes w:pageBreakBefore; a second break adds a page + const style_instance * style = + odf_context.styleContainer().style_by_name(*elm->element_style_name, family, false); + const paragraph_format_properties properties = calc_paragraph_properties_content(style); + if (properties.fo_break_before_ && properties.fo_break_before_->get_type() == odf_types::fo_break::Page) + return; + } + Context.set_page_break_before(2); +} + void index_body::docx_convert(oox::docx_conversion_context & Context) { if (index_title_) @@ -928,6 +967,7 @@ void index_body::docx_convert(oox::docx_conversion_context & Context) for (size_t i = 0; i < content_.size(); i++) { Context.start_index_element(); + break_before_master_page(Context, content_[i]); content_[i]->docx_convert(Context); Context.end_index_element(); } @@ -949,6 +989,7 @@ void index_title::docx_convert(oox::docx_conversion_context & Context) { for (size_t i = 0; i < content_.size(); i++) { + break_before_master_page(Context, content_[i]); content_[i]->docx_convert(Context); } } diff --git a/OdfFile/Reader/Format/text_elements.h b/OdfFile/Reader/Format/text_elements.h index d06d7fe928..2bba96d492 100644 --- a/OdfFile/Reader/Format/text_elements.h +++ b/OdfFile/Reader/Format/text_elements.h @@ -567,6 +567,9 @@ class index_body : public office_element_impl virtual void add_child_element( xml::sax * Reader, const std::wstring & Ns, const std::wstring & Name); }; CP_REGISTER_OFFICE_ELEMENT2(index_body); + +// Sets a page break before a body element whose style names a master page (not before the first one). +void break_before_master_page(oox::docx_conversion_context & Context, const office_element_ptr & elm); //--------------------------------------------------------------------------------------------------- class index_title : public office_element_impl { diff --git a/OdfFile/Test/test_odf/CMakeLists.txt b/OdfFile/Test/test_odf/CMakeLists.txt index 194fb4ae45..1eee1acbf1 100644 --- a/OdfFile/Test/test_odf/CMakeLists.txt +++ b/OdfFile/Test/test_odf/CMakeLists.txt @@ -66,6 +66,7 @@ add_core_gtest( pptx2odp_bullet_size.cpp chart_vary_colors.cpp odt2docx_first_body_element.cpp + odt2docx_master_page_break.cpp odt2docx_meta_template.cpp chart_subtitle.cpp math_property_values.cpp diff --git a/OdfFile/Test/test_odf/ExampleFiles/master_page_break.odt b/OdfFile/Test/test_odf/ExampleFiles/master_page_break.odt new file mode 100644 index 0000000000000000000000000000000000000000..8b44d1364216b5d6e04fb698ed4eddcd595affc9 GIT binary patch literal 1663 zcmWIWW@Zs#fB?mqxMM~<>Oc+%a{zH}W^QUpWkG6UK|xMta$-qlex80=UW#6RVsU1% zUVcGpUP^v)X>Mv>iC#%+MM(hEFpyTom`e6-hWml6coRwhYWby-dA@`{2g|*drIlO8m8KYOQ}&Amp7$7x_2-4!JLi-+gqOe z|7X9%>dxNN`8QOhJq~WWBnhgBmNbYKtq8Pfc%%37<}%*l*?Ky?ViW54O2E zla|=3opfvKxWaO|T4mb%-$i9nyFwdeuSEX)l)d(F(v|YV*}LD))n4>$HPgnXikvli z6&))aj=xxQ(Z=a$@2n!fuyxE+d&=29_Rr)zGP%g+Y4_VBBA0*Y=RG=@_)9Rlvp@dI zO=eUdhpt|*C6$SRA&85Cfg9-K`HWkgO>EBRcYrCnG?;`7buDhqTw07u4-%W6} zmWuCM@XuV}tV49(p3ptl{zaxto>s-VM6dogOI{9>{)5w|U&=S4;Go$~1g-_y** zzW!@kai8U2{V^t+ngUYzb4W zTIQs6VXAKL++q$FebHq(H=gtSlvxyR%e2f*@4F+Hr_s{iUCKow_8Iep+3tq;r3NR~ z^7R-m>b@f#Uarb+BYcj(X3a#FJjJL@lQ>p+{CoH2y~3|J^H&Uad?kHuc8${i!*_0WjR9Ksb!}vgjpmPcz2YlZeT%tp>e#Ko&|hm?+Y6Yw{5uZM zoG9~b`nzvEU#7kBT+e^f$K6zK0c(HP+ryRC+ve|B?_YZMV)>qPWwINM@Bb56bkiO+ zHU63Cv#bl4_qtga7=Q_qp}3?nC$$)q7?)1*^*d}J(E8oh^~v7oD6xjCON2aP7%UE$ z6bg0UNWK}Ik>CHh`}T&8BWkPWJwGd5KRK=5;1c%I;qL zPi5h4_-vtVkPN#~BJlEA%} zOrx{jcO{#jG}blUzBN3~LeKwQS8{o$_uBOPAA~CVZZWM~dA336Q2qH$p+=Y9DY7fx z{J#C&JkS5Td%bUZ^G;im+#Jyqc2+f2OeTac$5t-m!@{2E6)NxN=Cdqg2;XpaQbW4> z!_W4bHzycW?kHuPqAVJ>Sm@|R-UsfHizeqj_i8)%ZFa^Qu`|!6o$286Yq45z_9su2 zxi7Q0&Bd!{*W7ap_BAyxE>*w3YJpYi?%Z`ZRjvhvmR5W&^MBnQCU0da`cLjfNJQO? z<#$)Py7P&YhViZoY7@ToK;{|__*Jtl{;DNi%C}R!`HDTjn~_O`8Fv8;j5aW60LCMtI7aBgUeZGJFfcSQ y`U06)%UyIM(2FjF5%xfj#KAYIQ2;sF3FAF@0E literal 0 HcmV?d00001 diff --git a/OdfFile/Test/test_odf/odt2docx_master_page_break.cpp b/OdfFile/Test/test_odf/odt2docx_master_page_break.cpp new file mode 100644 index 0000000000..c42de81dad --- /dev/null +++ b/OdfFile/Test/test_odf/odt2docx_master_page_break.cpp @@ -0,0 +1,145 @@ +// SPDX-FileCopyrightText: 2026 Euro-Office contributors +// SPDX-License-Identifier: AGPL-3.0-only + +// ODT -> DOCX: a paragraph whose style names a master page starts a new page. + +#include "gtest/gtest.h" + +#include "common.h" + +#include + +namespace +{ +// The w:p element that contains the given text, or an empty string. +std::wstring paragraph_with_text(const std::wstring& xml, const std::wstring& text) +{ + const size_t pos = xml.find(text); + if (pos == std::wstring::npos) + return L""; + + size_t start = xml.rfind(L"", pos); + const size_t start_attr = xml.rfind(L" start)) + start = start_attr; + if (start == std::wstring::npos) + return L""; + + return xml.substr(start, pos - start); +} + +bool has_page_break(const std::wstring& paragraph) +{ + return paragraph.find(L"") != std::wstring::npos || + paragraph.find(L"") != std::wstring::npos; +} + +// The w:style element with the given w:name, or an empty string. +std::wstring style_with_name(const std::wstring& xml, const std::wstring& name) +{ + const size_t pos = xml.find(L""); + if (pos == std::wstring::npos) + return L""; + + const size_t start = xml.rfind(L"", pos); + if (start == std::wstring::npos || end == std::wstring::npos) + return L""; + + return xml.substr(start, end - start); +} +} // namespace + +// style:master-page-name starts a new page even when the master page stays the same, +// also when inherited from a parent style or inside an index, but not on the first paragraph +// of the body, not inside a hidden section and not when an empty value overrides the parent. +TEST(OdtToDocxMasterPageBreak, MasterPageNameStartsNewPage) +{ + const std::wstring dir = convert_odf_to_ooxml_dir(L"ExampleFiles/master_page_break.odt"); + ASSERT_FALSE(dir.empty()) << "ODT -> DOCX conversion failed"; + + const std::wstring xml = read_part(dir, L"word/document.xml"); + ASSERT_FALSE(xml.empty()) << "word/document.xml is missing"; + EXPECT_TRUE(is_well_formed(xml)); + + const std::wstring first = paragraph_with_text(xml, L"First paragraph"); + ASSERT_FALSE(first.empty()); + EXPECT_FALSE(has_page_break(first)); + + const std::wstring plain = paragraph_with_text(xml, L"Plain paragraph"); + ASSERT_FALSE(plain.empty()); + EXPECT_FALSE(has_page_break(plain)); + + const std::wstring same = paragraph_with_text(xml, L"Same page style paragraph"); + ASSERT_FALSE(same.empty()); + EXPECT_TRUE(has_page_break(same)); + + const std::wstring hidden = paragraph_with_text(xml, L"Hidden paragraph"); + if (!hidden.empty()) + EXPECT_FALSE(has_page_break(hidden)); + + // the master page comes from the parent style + const std::wstring inherited = paragraph_with_text(xml, L"Inherited paragraph"); + ASSERT_FALSE(inherited.empty()); + EXPECT_TRUE(has_page_break(inherited)); + + // an empty style:master-page-name overrides the parent style: no new page + const std::wstring empty_master = paragraph_with_text(xml, L"Empty master paragraph"); + ASSERT_FALSE(empty_master.empty()); + EXPECT_FALSE(has_page_break(empty_master)); + + // a paragraph inside a table of contents + const std::wstring index = paragraph_with_text(xml, L"Index paragraph"); + ASSERT_FALSE(index.empty()); + EXPECT_TRUE(has_page_break(index)); + + const std::wstring last = paragraph_with_text(xml, L"Last paragraph"); + ASSERT_FALSE(last.empty()); + EXPECT_FALSE(has_page_break(last)); +} + +// The page break is written once, never for a master page that does not exist, not cancelled by +// fo:break-before="auto" on a table, and not inside a table. +TEST(OdtToDocxMasterPageBreak, MasterPageBreakOnceAndOnlyOnPages) +{ + const std::wstring dir = convert_odf_to_ooxml_dir(L"ExampleFiles/master_page_break.odt"); + ASSERT_FALSE(dir.empty()) << "ODT -> DOCX conversion failed"; + + const std::wstring xml = read_part(dir, L"word/document.xml"); + ASSERT_FALSE(xml.empty()) << "word/document.xml is missing"; + const std::wstring styles = read_part(dir, L"word/styles.xml"); + ASSERT_FALSE(styles.empty()) << "word/styles.xml is missing"; + + // the paragraph style breaks the page itself: no second break in the paragraph + const std::wstring break_style = style_with_name(styles, L"ChapterBreak"); + ASSERT_FALSE(break_style.empty()); + EXPECT_NE(break_style.find(L""), std::wstring::npos); + + const std::wstring break_paragraph = paragraph_with_text(xml, L"Break style paragraph"); + ASSERT_FALSE(break_paragraph.empty()); + EXPECT_FALSE(has_page_break(break_paragraph)); + + // the same for an automatic style that inherits both from that style + const std::wstring break_child = paragraph_with_text(xml, L"Break child paragraph"); + ASSERT_FALSE(break_child.empty()); + EXPECT_FALSE(has_page_break(break_child)); + + // a master page that does not exist is ignored + const std::wstring missing = paragraph_with_text(xml, L"Missing master paragraph"); + ASSERT_FALSE(missing.empty()); + EXPECT_FALSE(has_page_break(missing)); + + // a table that names a master page starts a new page also with fo:break-before="auto" + const std::wstring table = paragraph_with_text(xml, L"Table with master"); + ASSERT_FALSE(table.empty()); + EXPECT_TRUE(has_page_break(table)); + + // a master page name inside a table cell does not break the page + const std::wstring cell = paragraph_with_text(xml, L"Cell section paragraph"); + ASSERT_FALSE(cell.empty()); + EXPECT_FALSE(has_page_break(cell)); + + const std::wstring last = paragraph_with_text(xml, L"Last paragraph"); + ASSERT_FALSE(last.empty()); + EXPECT_FALSE(has_page_break(last)); +} diff --git a/REUSE.toml b/REUSE.toml index 38c3b5af4d..af92a72a50 100644 --- a/REUSE.toml +++ b/REUSE.toml @@ -183,6 +183,7 @@ SPDX-License-Identifier = "AGPL-3.0-only" path = [ "OdfFile/Test/test_odf/ExampleFiles/numbered_paragraph_first.odt", "OdfFile/Test/test_odf/ExampleFiles/page_sequence_first.odt", + "OdfFile/Test/test_odf/ExampleFiles/master_page_break.odt", ] SPDX-FileCopyrightText = "2026 Euro-Office contributors" SPDX-License-Identifier = "AGPL-3.0-only" From 9a4194d36682532d1b6db5d8d4be639a7af9de80 Mon Sep 17 00:00:00 2001 From: Christoph Schaefer Date: Wed, 18 Nov 2026 07:51:53 +0100 Subject: [PATCH 2/3] fix(OdfFile): write the master page break as w:pageBreakBefore in ODT -> 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 Signed-off-by: Christoph Schaefer --- .../Converter/docx_conversion_context.cpp | 28 ++++++++- .../ExampleFiles/master_page_layout_break.odt | Bin 0 -> 1403 bytes .../test_odf/odt2docx_master_page_break.cpp | 53 ++++++++++++++++++ REUSE.toml | 1 + 4 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 OdfFile/Test/test_odf/ExampleFiles/master_page_layout_break.odt diff --git a/OdfFile/Reader/Converter/docx_conversion_context.cpp b/OdfFile/Reader/Converter/docx_conversion_context.cpp index 0dfc875196..35f34b64de 100644 --- a/OdfFile/Reader/Converter/docx_conversion_context.cpp +++ b/OdfFile/Reader/Converter/docx_conversion_context.cpp @@ -1527,6 +1527,19 @@ int twips(double points) return (int)(points * 20 + (points < 0 ? -0.5 : 0.5)); } +// True, and the request cleared, if a page break (2) is pending for a body paragraph outside tables. A master page +// switch asks for it. Written as w:pageBreakBefore it adds no empty page when a section break has already started a +// new page; a page break run at the start of the paragraph would. +bool take_page_break_before(docx_conversion_context & Context, bool in_drawing) +{ + if (Context.get_page_break_before() != 2 || in_drawing || Context.get_table_context().in_table() + || Context.get_process_note() != docx_conversion_context::noNote) + return false; + + Context.set_page_break_before(0); + return true; +} + // Text width in points of a page layout (page width less the page margins), 0 if unknown. // Section margins are left out: a section of its own gets them neither in its w:pgMar. double text_width(odf_reader::page_layout_container & layouts, const std::wstring & page_layout_name) @@ -1769,7 +1782,10 @@ void docx_conversion_context::serialize_paragraph_style(std::wostream & strm, co // start_paragraph(); //} - if (!paragraph_style.str().empty() || !ParentId.empty()) + // an automatic paragraph style without paragraph properties has not written the page break yet + const bool page_break_before = !in_styles && in_automatic_style_ && take_page_break_before(*this, in_drawing); + + if (!paragraph_style.str().empty() || !ParentId.empty() || page_break_before) { CP_XML_NODE(L"w:pPr") { @@ -1785,6 +1801,13 @@ void docx_conversion_context::serialize_paragraph_style(std::wostream & strm, co CP_XML_ATTR(L"w:val", ParentId); } } + if (page_break_before) + { + CP_XML_NODE(L"w:pageBreakBefore") + { + CP_XML_ATTR(L"w:val", L"true"); + } + } CP_XML_STREAM() << paragraph_style.str(); serialize_list_properties(CP_XML_STREAM()); @@ -2306,6 +2329,9 @@ int docx_conversion_context::process_paragraph_attr(odf_reader::text::paragraph_ } output_stream() << L""; + if (take_page_break_before(*this, in_drawing)) + output_stream() << L""; + if (!get_text_tracked_context().dumpPPr_.empty()) { output_stream() << get_text_tracked_context().dumpPPr_; diff --git a/OdfFile/Test/test_odf/ExampleFiles/master_page_layout_break.odt b/OdfFile/Test/test_odf/ExampleFiles/master_page_layout_break.odt new file mode 100644 index 0000000000000000000000000000000000000000..f076557af0fbcac18a8e1d105bb0c070ea5dc9ba GIT binary patch literal 1403 zcmWIWW@Zs#fB?mqxMM~<>Oc+%a{zH}W^QUpWkG6UK|xMta$-qlex80=UW#6RVsU1% zUVcGpUP^v)X>Mv>iC#%+MM(hEFpyTom`e6-hWml6coRwhYWby-dA@`{2g|*drIlO8m8KYOQ}&Amp7$7x_2-4!JLi-+gqOe z|7X9%>dxNN`8QOhJq~WWBnhgBmNbYKtq8Pfc%%37<}%*l*?Ky?ViW54O2E zla|=3opfvKxWaO|T4mb%-$i9nyFwdeuSEX)l)d(F(v|YV*}LD))n4>$HPgnXikvli z6&))aj=xxQ(Z=a$@2n!fuyxE+d&=29_Rr)zGP%g+Y4_VBBA0*Y=RG=@_)9Rlvp@dI zO=eUde}DJvS1QoAbu0`F+&~{E=jWAx!UpKits$}5hZRI>->(n!(imaf-jlS$lB(scn&?1y^!3S9-)+MZo5YG>igTk&!wo2m`(94YND9g9Ba z&3bpr+HTIS)el!z_VGQESjYN(Z*flYI#;<`cPU}^aIvO4y+Un?X?*Jpq8D5=uwYr7 zWUubIt>1mc8Q~@Br{4cPDq0u&Xknhi$#)@%+=`7K*Ag-(z|q71P^& z`VRm96MJ_xR!gt6W!E;4?ob!lsK@r8Nkq<6E!AzoPAEO&OzK97^z3eL5j{B`jB%5!!8PtVV1kFG(BjXAeV?c0E{@dp?i zT)@~UE~(5(Ee1u#-jLII%?1K{o@-Bd&(|tdO>+6;1+x#~g7`=ZeR%cG$zrM1~ zA@riRKF8U2Z{G1J^Bv#3rrAGl?gcHyi~ssqVp6TdSNWVc{+qj2<#E<|RM$TKE6GV}^$a$3nP7#T$ukUdcSw2W+&jA~)b9i%+Yh3u;H$(-t_+K4$V=&UK{V)wR(2gVOR5t z;<*zAJ+IqdiFiNJ^`UXVi+!I|i`EDUxzAJ)jwlQa`}g^X|L=A&d9}!g#!J?I`1h+i z>T%Du495ibsR`Z-HL@PhJiLR^I`fNCSixEDn8vajZ!a3ywrGpa{&Kwjs=Y?6rNG}+ z`{(U@_+QFk>C^Uto}H3H+qur_beb>QyHK3jZ2psTJ-d%--7$DM>rK4d(d`@KW2_%` z*N5-=%@*Lz$RxsyyYK-<85lGG;}B64A#`CcWgvPO7#bLTflRDr4!RNO*%o1hJ DOCX conversion failed"; + + const std::wstring xml = read_part(dir, L"word/document.xml"); + ASSERT_FALSE(xml.empty()) << "word/document.xml is missing"; + EXPECT_TRUE(is_well_formed(xml)); + + const std::wstring page_break_run = L""; + const std::wstring page_break_before = L""; + const std::wstring section = L" Date: Wed, 18 Nov 2026 16:18:05 +0100 Subject: [PATCH 3/3] fix(OdfFile): break before a table with w:pageBreakBefore in its first 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 Signed-off-by: Christoph Schaefer --- .../Converter/docx_conversion_context.cpp | 23 +++++++--- .../ExampleFiles/master_page_break.odt | Bin 1663 -> 1823 bytes .../test_odf/odt2docx_master_page_break.cpp | 40 +++++++++++++++++- 3 files changed, 55 insertions(+), 8 deletions(-) diff --git a/OdfFile/Reader/Converter/docx_conversion_context.cpp b/OdfFile/Reader/Converter/docx_conversion_context.cpp index 5600d128c8..dadc133133 100644 --- a/OdfFile/Reader/Converter/docx_conversion_context.cpp +++ b/OdfFile/Reader/Converter/docx_conversion_context.cpp @@ -1529,12 +1529,14 @@ int twips(double points) return (int)(points * 20 + (points < 0 ? -0.5 : 0.5)); } -// True, and the request cleared, if a page break (2) is pending for a body paragraph outside tables. A master page -// switch asks for it. Written as w:pageBreakBefore it adds no empty page when a section break has already started a -// new page; a page break run at the start of the paragraph would. +// True, and the request cleared, if a page break (2) is pending for a body paragraph. A master page switch asks for +// it. Written as w:pageBreakBefore it adds no empty page when a section break has already started a new page; a page +// break run at the start of the paragraph would. Inside a table, a pending break comes from the table itself and goes +// to the first paragraph of its first cell: Word and LibreOffice start the table on a new page only for +// w:pageBreakBefore there. bool take_page_break_before(docx_conversion_context & Context, bool in_drawing) { - if (Context.get_page_break_before() != 2 || in_drawing || Context.get_table_context().in_table() + if (Context.get_page_break_before() != 2 || in_drawing || Context.process_headers_footers_ || Context.get_process_note() != docx_conversion_context::noNote) return false; @@ -2430,6 +2432,8 @@ int docx_conversion_context::process_paragraph_attr(odf_reader::text::paragraph_ // distance between a header/footer and the page body is all it gets. header_footer_edge_spacing edge_spacing(*this, Attr, NULL); const std::wstring edge_attr = header_footer_context_.edge_attr(); + // or the page break of a table that starts a new page, for the first paragraph of its first cell + const bool page_break_before = take_page_break_before(*this, in_drawing); if (!get_section_context().dump_.empty() && !get_table_context().in_table() @@ -2442,12 +2446,19 @@ int docx_conversion_context::process_paragraph_attr(odf_reader::text::paragraph_ output_stream() << get_section_context().dump_; get_section_context().dump_.clear(); //TODO find out if headers are real without styles and properties + if (page_break_before) + output_stream() << L""; output_stream() << L""; return 3; } - if (!edge_attr.empty()) + if (!edge_attr.empty() || page_break_before) { - output_stream() << L""; + output_stream() << L""; + if (page_break_before) + output_stream() << L""; + if (!edge_attr.empty()) + output_stream() << L""; + output_stream() << L""; return 3; } diff --git a/OdfFile/Test/test_odf/ExampleFiles/master_page_break.odt b/OdfFile/Test/test_odf/ExampleFiles/master_page_break.odt index 8b44d1364216b5d6e04fb698ed4eddcd595affc9..eeb0699bf2c6a0feeb9cf61ed08041d00c093577 100644 GIT binary patch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1g)&vjKHgvXBRgcP zJ(ho^%B3hUKWS-*d%85zC&6sI=nqqNaz-F{<%Iz)y`Ws6m1Z=FxLR~kz0x&XYVV3; zH_0G^(bdhY-bPbR zH8*p0C*P`|I%T_UuGu8kQdM<$^L9_`hgKgtITijM=u}vKn5W&UMMn-4byf|oQieJc z+Ta}84cZYYTw*3SvZR^lhU}pJVv24N!}^2Gh;3Ndj9IkED`wL=lR39vs+&0dCSuZW zJ|)@e2a_KLi3(M#ptNNI002@7ld=Xc9Q=_?riuXo0E+|w015yA00000000000Du9_ P1C#v*90q>{000009Gnw6 diff --git a/OdfFile/Test/test_odf/odt2docx_master_page_break.cpp b/OdfFile/Test/test_odf/odt2docx_master_page_break.cpp index 8da23518a2..cf3c870148 100644 --- a/OdfFile/Test/test_odf/odt2docx_master_page_break.cpp +++ b/OdfFile/Test/test_odf/odt2docx_master_page_break.cpp @@ -129,10 +129,12 @@ TEST(OdtToDocxMasterPageBreak, MasterPageBreakOnceAndOnlyOnPages) ASSERT_FALSE(missing.empty()); EXPECT_FALSE(has_page_break(missing)); - // a table that names a master page starts a new page also with fo:break-before="auto" + // a table that names a master page starts a new page also with fo:break-before="auto"; the break is + // w:pageBreakBefore on the first paragraph of the first cell, here one without a style const std::wstring table = paragraph_with_text(xml, L"Table with master"); ASSERT_FALSE(table.empty()); - EXPECT_TRUE(has_page_break(table)); + EXPECT_NE(table.find(L""), std::wstring::npos); + EXPECT_EQ(table.find(L""), std::wstring::npos); // a master page name inside a table cell does not break the page const std::wstring cell = paragraph_with_text(xml, L"Cell section paragraph"); @@ -196,3 +198,37 @@ TEST(OdtToDocxMasterPageBreak, PageLayoutChangeGivesNoEmptyPage) EXPECT_EQ(sections, 4u); EXPECT_EQ(xml.find(page_break_run), std::wstring::npos); } + +// A table that names a master page starts a new page through w:pageBreakBefore on the first paragraph of its first +// cell, whatever the style of that paragraph. Word and LibreOffice ignore a page break run there, so the table would +// stay on the previous page. +TEST(OdtToDocxMasterPageBreak, TableBreakIsPageBreakBeforeInFirstCell) +{ + const std::wstring dir = convert_odf_to_ooxml_dir(L"ExampleFiles/master_page_break.odt"); + ASSERT_FALSE(dir.empty()) << "ODT -> DOCX conversion failed"; + + const std::wstring xml = read_part(dir, L"word/document.xml"); + ASSERT_FALSE(xml.empty()) << "word/document.xml is missing"; + EXPECT_TRUE(is_well_formed(xml)); + + const std::wstring page_break_run = L""; + const std::wstring page_break_before = L""; + + // named paragraph style, automatic style without paragraph properties, automatic style with them + for (const wchar_t* text : + {L"Table named style cell", L"Table plain automatic cell", L"Table formatted automatic cell"}) + { + const std::wstring cell = paragraph_with_text(xml, text); + ASSERT_FALSE(cell.empty()) << text; + EXPECT_NE(cell.find(page_break_before), std::wstring::npos) << text; + EXPECT_EQ(cell.find(page_break_run), std::wstring::npos) << text; + } + + // only the first paragraph of the cell breaks the page + const std::wstring second = paragraph_with_text(xml, L"Table second paragraph"); + ASSERT_FALSE(second.empty()); + EXPECT_FALSE(has_page_break(second)); + + // no page break run anywhere in the document + EXPECT_EQ(xml.find(page_break_run), std::wstring::npos); +}