tdf#107618 DOCX export: fix missing header when doc ends with section

See commit b6e62dc0dc2b284c825f1182a67bb2f9259a30ce (tdf#106492 DOCX
export: fix duplicated section break at doc end, 2017-03-21) for the
context, this is one more case where it's not OK to suppress the two
section breaks at the end of a document, since the suppressed section
break is the one that provides the headers / footers.

Change-Id: I4abd7895436e4d2e08fd7b7ff4aabbac8d65f7fb
Reviewed-on: https://gerrit.libreoffice.org/38721
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
Tested-by: Jenkins <ci@libreoffice.org>
diff --git a/sw/qa/extras/ooxmlexport/data/tdf107618.doc b/sw/qa/extras/ooxmlexport/data/tdf107618.doc
new file mode 100644
index 0000000..8a6972f
--- /dev/null
+++ b/sw/qa/extras/ooxmlexport/data/tdf107618.doc
Binary files differ
diff --git a/sw/qa/extras/ooxmlexport/ooxmlexport9.cxx b/sw/qa/extras/ooxmlexport/ooxmlexport9.cxx
index 1dc2618..2ad8631 100644
--- a/sw/qa/extras/ooxmlexport/ooxmlexport9.cxx
+++ b/sw/qa/extras/ooxmlexport/ooxmlexport9.cxx
@@ -675,6 +675,13 @@ DECLARE_OOXMLEXPORT_TEST(testTdf107684, "tdf107684.odt")
        assertXPath(pXmlDoc, "//w:style[@w:styleId='Heading1']/w:pPr/w:outlineLvl", 1);
}

DECLARE_OOXMLEXPORT_TEST(testTdf107618, "tdf107618.doc")
{
    // This was false, header was lost on export.
    uno::Reference<beans::XPropertySet> xPageStyle(getStyles("PageStyles")->getByName("Standard"), uno::UNO_QUERY);
    CPPUNIT_ASSERT_EQUAL(true, getProperty<bool>(xPageStyle, "HeaderIsOn"));
}

CPPUNIT_PLUGIN_IMPLEMENT();

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sw/source/filter/ww8/docxattributeoutput.cxx b/sw/source/filter/ww8/docxattributeoutput.cxx
index ba3bf43..bd667fe 100644
--- a/sw/source/filter/ww8/docxattributeoutput.cxx
+++ b/sw/source/filter/ww8/docxattributeoutput.cxx
@@ -118,6 +118,7 @@
#include <docsh.hxx>
#include <docary.hxx>
#include <fmtclbl.hxx>
#include <fmthdft.hxx>
#include <IDocumentSettingAccess.hxx>
#include <IDocumentStylePoolAccess.hxx>
#include <IDocumentRedlineAccess.hxx>
@@ -5495,25 +5496,43 @@ void DocxAttributeOutput::SectionBreak( sal_uInt8 nC, const WW8_SepInfo* pSectio
                // DocxExport::WriteMainText(), don't duplicate that here.
                SwNodeIndex aCurrentNode(m_rExport.m_pCurPam->GetNode());
                SwNodeIndex aLastNode(m_rExport.m_pDoc->GetNodes().GetEndOfContent(), -1);
                bool bEmit = aCurrentNode != aLastNode;

                // Need to still emit an empty section at the end of the
                // document in case balanced columns are wanted, since the last
                // section in Word is always balanced.
                sal_uInt16 nColumns = 1;
                bool bBalance = false;
                if (const SwSectionFormat* pFormat = pSectionInfo->pSectionFormat)
                if (!bEmit)
                {
                    if (pFormat != reinterpret_cast<SwSectionFormat*>(sal_IntPtr(-1)))
                    // Need to still emit an empty section at the end of the
                    // document in case balanced columns are wanted, since the last
                    // section in Word is always balanced.
                    sal_uInt16 nColumns = 1;
                    bool bBalance = false;
                    if (const SwSectionFormat* pFormat = pSectionInfo->pSectionFormat)
                    {
                        nColumns = pFormat->GetCol().GetNumCols();
                        const SwFormatNoBalancedColumns& rNoBalanced = pFormat->GetBalancedColumns();
                        bBalance = !rNoBalanced.GetValue();
                        if (pFormat != reinterpret_cast<SwSectionFormat*>(sal_IntPtr(-1)))
                        {
                            nColumns = pFormat->GetCol().GetNumCols();
                            const SwFormatNoBalancedColumns& rNoBalanced = pFormat->GetBalancedColumns();
                            bBalance = !rNoBalanced.GetValue();
                        }
                    }
                    bEmit = (nColumns > 1 && bBalance);
                }

                if (!bEmit)
                {
                    // Also need to emit if the page desc contains a header or
                    // footer, otherwise we go with the properties of the
                    // section (and not the page style), which never has
                    // headers/footers.
                    if (const SwPageDesc* pPageDesc = pSectionInfo->pPageDesc)
                    {
                        const auto& rMaster = pPageDesc->GetMaster();
                        bEmit = rMaster.GetHeader().IsActive() || rMaster.GetFooter().IsActive();
                    }
                }

                // don't add section properties if this will be the first
                // paragraph in the document
                if ( !m_bParagraphOpened && !m_bIsFirstParagraph && (aCurrentNode != aLastNode || (nColumns > 1 && bBalance)))
                if ( !m_bParagraphOpened && !m_bIsFirstParagraph && bEmit )
                {
                    // Create a dummy paragraph if needed
                    m_pSerializer->startElementNS( XML_w, XML_p, FSEND );