Changeset 1733 for trunk/docx4j/src
- Timestamp:
- 12/27/11 01:38:14 (5 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/docx4j/src/main/java/org/docx4j/model/fields/merge/MailMerger.java
r1732 r1733 3 3 import java.io.ByteArrayInputStream; 4 4 import java.io.ByteArrayOutputStream; 5 import java.math.BigInteger; 5 6 import java.util.ArrayList; 6 7 import java.util.HashMap; … … 9 10 10 11 import org.apache.log4j.Logger; 12 import org.docx4j.Docx4jProperties; 11 13 import org.docx4j.TraversalUtil; 12 14 import org.docx4j.XmlUtils; … … 15 17 import org.docx4j.model.fields.FieldRef; 16 18 import org.docx4j.model.fields.FieldsPreprocessor; 19 import org.docx4j.model.structure.PageDimensions; 20 import org.docx4j.model.structure.PageSizePaper; 17 21 import org.docx4j.openpackaging.exceptions.Docx4JException; 18 22 import org.docx4j.openpackaging.io.SaveToZipFile; … … 20 24 import org.docx4j.openpackaging.packages.WordprocessingMLPackage; 21 25 import org.docx4j.wml.Body; 26 import org.docx4j.wml.CTPageNumber; 22 27 import org.docx4j.wml.ContentAccessor; 23 28 import org.docx4j.wml.P; 29 import org.docx4j.wml.SectPr; 24 30 25 31 … … 79 85 byte[] template = baos.toByteArray(); 80 86 87 81 88 WordprocessingMLPackage target = WordprocessingMLPackage.load( 82 89 new ByteArrayInputStream(template)); 90 SectPr documentSeparator = getDocumentSeparator(target); 83 91 target.getMainDocumentPart().getContent().clear(); 84 92 … … 88 96 target.getMainDocumentPart().getContent().addAll(content); 89 97 90 // TODO - sectPr 98 // for all but last document 99 if (!content.equals(results.get(results.size()-1))) { 100 // add sectPr to final paragraph 101 Object last = content.get( content.size()-1); 102 P lastP = null; 103 if (last instanceof P) { 104 lastP = (P)last; 105 } else { 106 lastP = Context.getWmlObjectFactory().createP(); 107 target.getMainDocumentPart().getContent().add(lastP); 108 } 109 if (lastP.getPPr()==null) { 110 lastP.setPPr(Context.getWmlObjectFactory().createPPr()); 111 } 112 lastP.getPPr().setSectPr(documentSeparator); 113 } 91 114 } 92 115 … … 94 117 } 95 118 119 /** 120 * Word uses the existing sectPr element, but adds 121 * a page numbering restart to it. TODO: investigate 122 * what it does with headers/footers. 123 * @param template 124 * @return 125 */ 126 private static SectPr getDocumentSeparator(WordprocessingMLPackage template) { 127 128 SectPr sectPr = template.getMainDocumentPart().getJaxbElement().getBody().getSectPr(); 129 130 if (sectPr==null) { 131 // Maybe the last P already contains one? 132 // Presumably Word would reuse this. 133 List all = template.getMainDocumentPart().getContent(); 134 Object last = all.get( all.size()-1 ); 135 if (last instanceof P) { 136 if (((P) last).getPPr()!=null 137 && ((P) last).getPPr().getSectPr() !=null) { 138 sectPr = ((P) last).getPPr().getSectPr(); 139 } 140 } 141 } 142 143 if (sectPr==null) { 144 145 // Create a basic sectPr using our Page model 146 String papersize= Docx4jProperties.getProperties().getProperty("docx4j.PageSize", "A4"); 147 log.info("Using paper size: " + papersize); 148 149 String landscapeString = Docx4jProperties.getProperties().getProperty("docx4j.PageOrientationLandscape", "false"); 150 boolean landscape= Boolean.parseBoolean(landscapeString); 151 log.info("Landscape orientation: " + landscape); 152 153 PageDimensions page = new PageDimensions(); 154 page.setPgSize(PageSizePaper.valueOf(papersize), landscape); 155 156 sectPr = Context.getWmlObjectFactory().createSectPr(); 157 sectPr.setPgSz( page.getPgSz() ); 158 sectPr.setPgMar( page.getPgMar() ); 159 } 160 161 // <w:pgNumType w:start="1"/> 162 CTPageNumber pageNumber = sectPr.getPgNumType(); 163 if (pageNumber==null) { 164 pageNumber = Context.getWmlObjectFactory().createCTPageNumber(); 165 sectPr.setPgNumType(pageNumber); 166 } 167 pageNumber.setStart(BigInteger.ONE); 168 169 return sectPr; 170 } 171 96 172 public static List<WordprocessingMLPackage> getResults(WordprocessingMLPackage input, 97 173 List<Map<String, String>> data) throws Docx4JException { … … 225 301 System.getProperty("user.dir") + "/mergefield1.docx")); 226 302 303 List<Map<String, String>> data = new ArrayList<Map<String, String>>(); 304 227 305 Map<String, String> map = new HashMap<String, String>(); 228 306 map.put("Kundenname", "Plutext"); 229 307 map.put("Kundenstrasse", "Bourke Street"); 230 308 231 List<Map<String, String>> data = new ArrayList<Map<String, String>>();232 309 data.add(map); 310 311 map = new HashMap<String, String>(); 312 map.put("Kundenname", "Name 2"); 313 map.put("Kundenstrasse", "Collins Street"); 314 315 data.add(map); 316 // Word matches irrespective of case, and takes the first matching field 233 317 234 318 235 319 // System.out.println(XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true)); 236 320 237 WordprocessingMLPackage output = getConsolidatedResultCrude(wordMLPackage, data);238 239 240 System.out.println(XmlUtils.marshaltoString(output.getMainDocumentPart().getJaxbElement(), true, true));241 242 // wordMLPackage.save(new java.io.File(243 //System.getProperty("user.dir") + "/mergefield1-OUT.docx") );321 WordprocessingMLPackage output = org.docx4j.model.fields.merge.MailMerger.getConsolidatedResultCrude(wordMLPackage, data); 322 323 324 //System.out.println(XmlUtils.marshaltoString(output.getMainDocumentPart().getJaxbElement(), true, true)); 325 326 output.save(new java.io.File( 327 System.getProperty("user.dir") + "/mergefield1-OUT.docx") ); 244 328 245 329 }
Note: See TracChangeset
for help on using the changeset viewer.
