Page 1 of 1

MailMerge formatting

PostPosted: Tue Jan 08, 2013 4:12 am
by pbiwer
Hi Jason - I have just started using the docx4j API and find it really useful. Great work!

I am using the MailMerge functionality and found that the Paragraph containing the field is losing any formating it may have had. I found a fix for the FieldsPreprocessor.class that I have included below - please use it if you find it helpful.

Regards,

Paul

Code: Select all
   public static P canonicalise(P p, List<FieldRef> fieldRefs) {
      /*
       * Result is something like:
       *
              <w:p>
                  <w:r>
                      <w:fldChar w:fldCharType="begin"/>
                      <w:instrText xml:space="preserve"> DATE  </w:instrText>
                      <w:fldChar w:fldCharType="separate"/>
                  </w:r>
                  <w:r>
                      <w:t>4/12/2011</w:t>
                  </w:r>
                  <w:r>
                      <w:fldChar w:fldCharType="end"/>
                  </w:r>
              </w:p>       
       */
      
      // TODO merge adjacent instrText elements

      P newP = Context.getWmlObjectFactory().createP();
      
      int depth = 0;
      R newR = Context.getWmlObjectFactory().createR();
      
      RPr fieldRPr = null;
      
      FieldRef currentField = null;
      
      boolean seenSeparate=false;
      
      for (Object o : p.getContent() ) {
         if ( o instanceof R ) {
            R existingRun = (R)o;

//********************These lines were added to maintain formatting
//********************of the parent Paragraph 01/07/2013
            P parentP = (P) existingRun.getParent();
            newP.setPPr(parentP.getPPr() );
//********************
//********************
            
            for (Object o2 : existingRun.getContent() ) {

Re: MailMerge formatting

PostPosted: Thu Feb 07, 2013 3:26 am
by CesarDRK
Worked for me ! Thanks.