Page 1 of 1

Last sectPr behaves oddly

PostPosted: Wed Oct 30, 2019 2:22 pm
by Vikram
Jason,

I’m having a hard time figuring out this problem. I’m using the docx4j API to create some documents with several continuous section breaks in them. I can do this successfully, but the last section break in each document before the end of the document always shows up a next-page section break in Word. All the other continuous section breaks behave as expected, and it's always the very last section break that somehow converts into a next-page section break.

When I examine the document in the Word helper, this is what I see:

Code: Select all
<w:document xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" mc:Ignorable="w14 w15 w16se w16cid wp14">
    <w:body>
        <w:p w:rsidP="00EB730C" w:rsidR="00EB730C" w:rsidRDefault="00EB730C">
            <w:r>
                <w:t>This is text under part 1.</w:t>
            </w:r>
        </w:p>
        <w:p w:rsidP="00EB730C" w:rsidR="00EB730C" w:rsidRDefault="00EB730C"/>
        <w:p w:rsidP="00EB730C" w:rsidR="00EB730C" w:rsidRDefault="00EB730C">
            <w:r>
                <w:rPr>
                    <w:b/>
                </w:rPr>
                <w:t>Part 1, Chapter 1</w:t>
            </w:r>
        </w:p>
        <w:p w:rsidP="00EB730C" w:rsidR="00EB730C" w:rsidRDefault="00EB730C">
            <w:r>
                <w:t>This is text under Part 1, Chapter 1.</w:t>
            </w:r>
        </w:p>
        <w:p w:rsidP="00EB730C" w:rsidR="00EB730C" w:rsidRDefault="00EB730C">
            <w:r>
                <w:t>This is text under Part 1, Chapter 1, Section 1</w:t>
            </w:r>
        </w:p>
        <w:p w:rsidP="00EB730C" w:rsidR="00EB730C" w:rsidRDefault="00EB730C"/>
        <w:p w:rsidP="00EB730C" w:rsidR="00EB730C" w:rsidRDefault="00EB730C">
            <w:r>
                <w:t>This is text in Part 1, Chapter 1, Scene 1.</w:t>
            </w:r>
        </w:p>
        <w:p w:rsidP="00EB730C" w:rsidR="00EB730C" w:rsidRDefault="00EB730C">
            <w:r>
                <w:t>This is text in Part 1, Chapter 1, Scene 2.</w:t>
            </w:r>
        </w:p>
        <w:p w:rsidP="00EB730C" w:rsidR="00EB730C" w:rsidRDefault="00EB730C">
            <w:r>
                <w:t>before section break</w:t>
            </w:r>
        </w:p>
        <w:p w:rsidP="00EB730C" w:rsidR="00EB730C" w:rsidRDefault="00EB730C">
            <w:pPr>
                <w:sectPr w:rsidR="00EB730C" w:rsidSect="00EB730C">
                    <w:pgSz w:h="15840" w:w="12240"/>
                    <w:pgMar w:bottom="1440" w:footer="720" w:gutter="0" w:header="720" w:left="1440" w:right="1440" w:top="1440"/>
                    <w:cols w:space="720"/>
                </w:sectPr>
            </w:pPr>
        </w:p>
        <w:p w:rsidP="00EB730C" w:rsidR="00EB730C" w:rsidRDefault="00EB730C">
            <w:r>
                <w:t>after section break</w:t>
            </w:r>
        </w:p>
        <w:p w:rsidP="00EB730C" w:rsidR="00EB730C" w:rsidRDefault="00EB730C">
            <w:r>
                <w:t>This is text under Part 2, Chapter 2.</w:t>
            </w:r>
        </w:p>
        <w:p w:rsidP="00EB730C" w:rsidR="00EB730C" w:rsidRDefault="00EB730C">
            <w:r>
                <w:br w:type="page"/>
            </w:r>
        </w:p>
        <w:p w:rsidP="00EB730C" w:rsidR="00EB730C" w:rsidRDefault="00EB730C"/>
        <w:p w:rsidP="00EB730C" w:rsidR="00EB730C" w:rsidRDefault="00EB730C">
            <w:r>
                <w:rPr>
                    <w:b/>
                </w:rPr>
                <w:t>Part 2, Chapter 2</w:t>
            </w:r>
        </w:p>
        <w:p w:rsidP="00EB730C" w:rsidR="00EB730C" w:rsidRDefault="00EB730C">
            <w:r>
                <w:t>This is text under Part 2, Chapter 2</w:t>
            </w:r>
        </w:p>
        <w:p w:rsidP="00EB730C" w:rsidR="00EB730C" w:rsidRDefault="00EB730C">
            <w:r>
                <w:t>This is text under Part 2, Chapter 2, Section 1</w:t>
            </w:r>
        </w:p>
        <w:p w:rsidP="00EB730C" w:rsidR="00EB730C" w:rsidRDefault="00EB730C">
            <w:r>
                <w:t>This is text in Part 2, Chapter 2, Scene 1.</w:t>
            </w:r>
        </w:p>
        <w:p w:rsidP="00EB730C" w:rsidR="00EB730C" w:rsidRDefault="00EB730C">
            <w:r>
                <w:t>This is text in Part 2, Chapter 2, Scene 2.</w:t>
            </w:r>
        </w:p>
        <w:p w:rsidP="00EB730C" w:rsidR="00EB730C" w:rsidRDefault="00EB730C">
            <w:r>
                <w:t>before section break</w:t>
            </w:r>
        </w:p>
        <w:p w:rsidP="00EB730C" w:rsidR="00EB730C" w:rsidRDefault="00EB730C">
            <w:pPr>
                <w:sectPr w:rsidR="00EB730C">
                    <w:type w:val="continuous"/>
                    <w:pgSz w:h="15840" w:w="12240"/>
                    <w:pgMar w:bottom="1440" w:footer="720" w:gutter="0" w:header="720" w:left="1440" w:right="1440" w:top="1440"/>
                    <w:cols w:space="720"/>
                </w:sectPr>
            </w:pPr>
        </w:p>
        <w:p w:rsidR="00000000" w:rsidRDefault="00EB730C">
            <w:r>
                <w:t>after section break</w:t>
            </w:r>
        </w:p>
        <w:sectPr w:rsidR="00000000">
            <w:headerReference xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" r:id="rId7" w:type="default"/>
            <w:footerReference xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" r:id="rId8" w:type="default"/>
            <w:pgSz w:code="1" w:h="15840" w:w="12240"/>
            <w:pgMar w:bottom="1440" w:footer="720" w:gutter="0" w:header="720" w:left="1440" w:right="1440" w:top="1440"/>
            <w:cols w:space="720"/>
        </w:sectPr>
    </w:body>
</w:document>



As far as I can tell, the last SectPr is correctly attached to a paragraph, is continuous, and comes before the final document-ending SectPr which is attached to the body.

I’ll attach an image of what it looks like in Word. The section break causes an unnecessary page break.

Any ideas about what is going on?

VIkram

Re: Last sectPr behaves oddly

PostPosted: Thu Oct 31, 2019 12:20 pm
by jason
Hi Vikram

If you can email me a couple of documents I can merge to reproduce the problem - along with your merge settings - I'll take a look.

Of course, it would be possible to programmatically alter the final sectPr, but I'd prefer to understand the underlying cause.

cheers .. Jason

Re: Last sectPr behaves oddly

PostPosted: Thu Oct 31, 2019 3:29 pm
by Vikram
Jason,

I should’ve been more clear–I’m not merging documents, but creating them programmatically from scratch and adding the SectPrs the same way.

The code to create each SectPr is essentially:

Code: Select all

      SectPr sectPr = createSectPr();

      SectPr.Type sectPrType = objectFactory.createSectPrType();

      sectPrType.setVal("continuous");

      sectPr.setType(sectPrType);

               P sectPara = objectFactory.createP();

              sectPara.setPPr(objectFactory.createPPr());

              sectPara.getPPr().setSectPr(sectPr);

             mainDocumentPart.addObject(sectPara);
             


Of course the same code is used for each SectPr, so it’s puzzling that only the last one acts up.

I can of course email you the programmatically-generated document. Do let me know.

Vikram

Re: Last sectPr behaves oddly

PostPosted: Wed Nov 06, 2019 6:16 am
by jason
Just saw this now. Yes, please email your docx.

Re: Last sectPr behaves oddly

PostPosted: Sat Nov 09, 2019 8:40 am
by Vikram
Jason,

I just emailed you the Word document.

Re: Last sectPr behaves oddly

PostPosted: Wed Nov 20, 2019 6:43 am
by Vikram
Jason,

Just checking in—did you receive the Word document I’d emailed you?

Thanks.

Vikram

Re: Last sectPr behaves oddly

PostPosted: Wed Nov 20, 2019 2:59 pm
by jason
Your docx ends with:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
        <w:sectPr>
            <w:headerReference w:type="default" r:id="rId4"/>
            <w:footerReference w:type="default" r:id="rId5"/>
            <w:pgSz w:w="12240" w:h="15840" w:code="1"/>
            <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0"/>
        </w:sectPr>
    </w:body>
</w:document>
 
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


If you add <w:type w:val="continuous"/> into that final sectPr, you'll get the behaviour you want.