diff --git a/OdfFile/Reader/Converter/docx_conversion_context.cpp b/OdfFile/Reader/Converter/docx_conversion_context.cpp index e3b0d83ae6..776bbd080a 100644 --- a/OdfFile/Reader/Converter/docx_conversion_context.cpp +++ b/OdfFile/Reader/Converter/docx_conversion_context.cpp @@ -1452,6 +1452,21 @@ 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. 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.process_headers_footers_ + || 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) @@ -1700,7 +1715,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") { @@ -1716,6 +1734,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()); @@ -2278,6 +2303,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_; @@ -2327,6 +2355,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() @@ -2339,12 +2369,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/Reader/Converter/docx_conversion_context.h b/OdfFile/Reader/Converter/docx_conversion_context.h index a64d656786..da2831b98d 100644 --- a/OdfFile/Reader/Converter/docx_conversion_context.h +++ b/OdfFile/Reader/Converter/docx_conversion_context.h @@ -1028,6 +1028,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_; // the endnote page of the document (office_text::docx_convert): the paragraph that ends the diff --git a/OdfFile/Reader/Format/odf_document_impl.cpp b/OdfFile/Reader/Format/odf_document_impl.cpp index 5e1c1d7dfb..670c02f48a 100644 --- a/OdfFile/Reader/Format/odf_document_impl.cpp +++ b/OdfFile/Reader/Format/odf_document_impl.cpp @@ -1003,7 +1003,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 373737e516..8b8b6ec51e 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 ef9da82112..6367fac4ee 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 da359642b4..fe575beb53 100644 --- a/OdfFile/Reader/Format/office_text.cpp +++ b/OdfFile/Reader/Format/office_text.cpp @@ -35,6 +35,7 @@ #include "serialize_elements.h" #include "odfcontext.h" +#include "text_elements.h" #include "style_paragraph_properties.h" #include "styles.h" #include "text_elements.h" @@ -395,6 +396,7 @@ void office_text::docx_convert(oox::docx_conversion_context & Context) } } } + text::break_before_master_page(Context, content_[i]); content_[i]->docx_convert(Context); } // An empty paragraph of minimal height (the endnote page section and the save in DocxConverter 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 2874d843e0..457bab1551 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 41f9bccd26..2cf8b7dc30 100644 --- a/OdfFile/Reader/Format/text_elements.cpp +++ b/OdfFile/Reader/Format/text_elements.cpp @@ -562,6 +562,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)) @@ -921,6 +925,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(); @@ -979,6 +984,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) @@ -1037,6 +1044,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_) @@ -1047,6 +1086,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(); } @@ -1068,6 +1108,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 118f5f4f8b..44911c7423 100644 --- a/OdfFile/Reader/Format/text_elements.h +++ b/OdfFile/Reader/Format/text_elements.h @@ -577,6 +577,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 8fe1848057..9a4e18c959 100644 --- a/OdfFile/Test/test_odf/CMakeLists.txt +++ b/OdfFile/Test/test_odf/CMakeLists.txt @@ -92,6 +92,7 @@ add_core_gtest( xlsx2ods_page_margins.cpp chart_vary_colors.cpp odt2docx_first_body_element.cpp + odt2docx_master_page_break.cpp odt2docx_page_background_tile.cpp odt2docx_meta_template.cpp xlsx2ods_row_padding.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 0000000000..eeb0699bf2 Binary files /dev/null and b/OdfFile/Test/test_odf/ExampleFiles/master_page_break.odt differ 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 0000000000..f076557af0 Binary files /dev/null and b/OdfFile/Test/test_odf/ExampleFiles/master_page_layout_break.odt differ 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..cf3c870148 --- /dev/null +++ b/OdfFile/Test/test_odf/odt2docx_master_page_break.cpp @@ -0,0 +1,234 @@ +// 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"; 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_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"); + 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)); +} + +// A master page with another page layout also starts a new section. The page break is then written as +// w:pageBreakBefore, never as a page break run at the start of the paragraph: after a section break that already +// starts a new page, such a run gives an empty page. This holds for a named paragraph style, for an automatic style +// without paragraph properties and for one with them. +TEST(OdtToDocxMasterPageBreak, PageLayoutChangeGivesNoEmptyPage) +{ + const std::wstring dir = convert_odf_to_ooxml_dir(L"ExampleFiles/master_page_layout_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""; + const std::wstring section = L" 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); +} diff --git a/REUSE.toml b/REUSE.toml index 37f3e4e6de..0db905c78d 100644 --- a/REUSE.toml +++ b/REUSE.toml @@ -381,6 +381,8 @@ 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", + "OdfFile/Test/test_odf/ExampleFiles/master_page_layout_break.odt", ] SPDX-FileCopyrightText = "2026 Euro-Office contributors" SPDX-License-Identifier = "AGPL-3.0-only"