Changeset 1733 for trunk/docx4j/src


Ignore:
Timestamp:
12/27/11 01:38:14 (5 months ago)
Author:
jharrop
Message:

Separate each output "document" with sectPr.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/docx4j/src/main/java/org/docx4j/model/fields/merge/MailMerger.java

    r1732 r1733  
    33import java.io.ByteArrayInputStream; 
    44import java.io.ByteArrayOutputStream; 
     5import java.math.BigInteger; 
    56import java.util.ArrayList; 
    67import java.util.HashMap; 
     
    910 
    1011import org.apache.log4j.Logger; 
     12import org.docx4j.Docx4jProperties; 
    1113import org.docx4j.TraversalUtil; 
    1214import org.docx4j.XmlUtils; 
     
    1517import org.docx4j.model.fields.FieldRef; 
    1618import org.docx4j.model.fields.FieldsPreprocessor; 
     19import org.docx4j.model.structure.PageDimensions; 
     20import org.docx4j.model.structure.PageSizePaper; 
    1721import org.docx4j.openpackaging.exceptions.Docx4JException; 
    1822import org.docx4j.openpackaging.io.SaveToZipFile; 
     
    2024import org.docx4j.openpackaging.packages.WordprocessingMLPackage; 
    2125import org.docx4j.wml.Body; 
     26import org.docx4j.wml.CTPageNumber; 
    2227import org.docx4j.wml.ContentAccessor; 
    2328import org.docx4j.wml.P; 
     29import org.docx4j.wml.SectPr; 
    2430 
    2531 
     
    7985                byte[] template = baos.toByteArray(); 
    8086 
     87                 
    8188                WordprocessingMLPackage target = WordprocessingMLPackage.load( 
    8289                                new ByteArrayInputStream(template)); 
     90                SectPr documentSeparator = getDocumentSeparator(target); 
    8391                target.getMainDocumentPart().getContent().clear(); 
    8492                 
     
    8896                        target.getMainDocumentPart().getContent().addAll(content); 
    8997                         
    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                        } 
    91114                } 
    92115                 
     
    94117        } 
    95118 
     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         
    96172        public static List<WordprocessingMLPackage> getResults(WordprocessingMLPackage input,  
    97173                        List<Map<String, String>> data) throws Docx4JException { 
     
    225301                                                System.getProperty("user.dir") + "/mergefield1.docx")); 
    226302                 
     303                List<Map<String, String>> data = new ArrayList<Map<String, String>>(); 
     304 
    227305                Map<String, String> map = new HashMap<String, String>(); 
    228306                map.put("Kundenname", "Plutext"); 
    229307                map.put("Kundenstrasse", "Bourke Street"); 
    230308                 
    231                 List<Map<String, String>> data = new ArrayList<Map<String, String>>(); 
    232309                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 
    233317                 
    234318                 
    235319//              System.out.println(XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true)); 
    236320 
    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") ); 
    244328                 
    245329        } 
Note: See TracChangeset for help on using the changeset viewer.