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:
Here is my output.docx looks file:
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>