Page 1 of 1

MailMerger creates extra page at the end of the document

PostPosted: Mon May 23, 2016 8:06 pm
by sapphire
Hi, I am new here and new to XML and docx4j so please advise accordingly. I am using the MailMerger class to produce a document similar to MS Word's Mail Merge feature which substitutes MergeFields with contents in the list, one page per one set of data, resulting multiple pages in one output docx file. Everything works fine except that there is an extra blank page at the end of the document, no matter how many pages it produces. Did I do something wrong or is there a way to tell MailMerge to not add the extra page? If that is not possible, what is the easiest way to remove the extra last page from my document through the code? Or alternatively, is there a better/easier way to achieve the same mail merge result? Thank you in advance!!

My Java code is below:

Code: Select all
   
      List<Map<DataFieldName, String>> data = new ArrayList<Map<DataFieldName, String>>();
      Map<DataFieldName, String> map = new HashMap<>();
   
      map.put(new DataFieldName("Student_First_Name"), "Jane");
      map.put(new DataFieldName("Student_Last_Name"), "Smith");
      data.add(map);

      WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(
                 new java.io.File("input.docx"));
       
   
       WordprocessingMLPackage output =
           org.docx4j.model.fields.merge.MailMerger.getConsolidatedResultCrude(
                   wordMLPackage, data, true);
       System.out.println(XmlUtils.marshaltoString(
          wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true));
   
       System.out.println("OUTPUT ********");
       System.out.println(XmlUtils.marshaltoString(             
             output.getMainDocumentPart().getJaxbElement(), true, true));
   
   
       output.save(new java.io.File("output.docx") );



Here is my input.docx:
input.docx
(12.21 KiB) Downloaded 409 times

Here is my output.docx looks file:
output.docx
(12.52 KiB) Downloaded 429 times
:|

I notice that there is a ":::::::::::::::::::Section Break (Next Page) :::::::::::::::::::::::::::::" added at the end of the text. This section break wasn't in input.docx.

The 2nd (extra) page of output.docx is just a blank page.

I have also dumped out the xml for input.docx. The body section is here:

Code: Select all
        <w:body>
        <w:p w:rsidR="002B4D63" w:rsidRDefault="002B623E">
            <w:r>
                <w:t xml:space="preserve">Test </w:t>
            </w:r>
            <w:fldSimple w:instr=" MERGEFIELD Student_First_Name ">
                <w:r>
                    <w:rPr>
                        <w:noProof/>
                    </w:rPr>
                    <w:t>«Student_First_Name»</w:t>
                </w:r>
            </w:fldSimple>
            <w:r>
                <w:t xml:space="preserve">   </w:t>
            </w:r>
            <w:fldSimple w:instr=" MERGEFIELD Student_Last_Name ">
                <w:r>
                    <w:rPr>
                        <w:noProof/>
                    </w:rPr>
                    <w:t>«Student_Last_Name»</w:t>
                </w:r>
            </w:fldSimple>
            <w:bookmarkStart w:name="_GoBack" w:id="0"/>
            <w:bookmarkEnd w:id="0"/>
        </w:p>
        <w:sectPr w:rsidR="002B4D63">
            <w:pgSz w:w="12240" w:h="15840"/>
            <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440"
               w:header="720" w:footer="720" w:gutter="0"/>
            <w:cols w:space="720"/>
            <w:docGrid w:linePitch="360"/>
        </w:sectPr>
        </w:body>



And here is the body section of output xml dump:

Code: Select all
        <w:body>
            <w:p>
                <w:pPr>
                    <w:sectPr w:rsidR="002B4D63">
                        <w:pgSz w:w="12240" w:h="15840"/>
                        <w:pgMar w:top="1440" w:right="1440" w:bottom="1440"
                             w:left="1440" w:header="720" w:footer="720"
                             w:gutter="0"/>
                        <w:pgNumType w:start="1"/>
                        <w:cols w:space="720"/>
                        <w:docGrid w:linePitch="360"/>
                    </w:sectPr>
                </w:pPr>
                <w:r>
                    <w:t xml:space="preserve">Test </w:t>
                </w:r>
                <w:r>
                    <w:rPr>
                        <w:noProof/>
                    </w:rPr>
                    <w:t>Jane____________</w:t>
                </w:r>
                <w:r>
                    <w:t xml:space="preserve">   </w:t>
                </w:r>
                <w:r>
                    <w:rPr>
                        <w:noProof/>
                    </w:rPr>
                    <w:t>Smith_____________________</w:t>
                </w:r>
                <w:bookmarkStart w:name="_GoBack" w:id="0"/>
                <w:bookmarkEnd w:id="0"/>
            </w:p>
            <w:sectPr w:rsidR="002B4D63">
                <w:pgSz w:w="12240" w:h="15840"/>
                <w:pgMar w:top="1440" w:right="1440" w:bottom="1440"
                     w:left="1440"
                     w:header="720" w:footer="720" w:gutter="0"/>
                <w:pgNumType w:start="1"/>
                <w:cols w:space="720"/>
                <w:docGrid w:linePitch="360"/>
            </w:sectPr>
        </w:body>

Re: MailMerger creates extra page at the end of the document

PostPosted: Thu May 09, 2019 6:01 pm
by diana
Hi,

I have a very similar problem. Is there a solution? I'm desperate... :(

Re: MailMerger creates extra page at the end of the document

PostPosted: Wed Oct 09, 2019 12:42 am
by norwdev
Anyone know how to prevent Mailmerger from adding av blank page to the document?