Changeset 1185


Ignore:
Timestamp:
08/13/10 19:44:39 (18 months ago)
Author:
jharrop
Message:

Support Word 2003 page numbers in PDF output. ie <w:fldChar w:fldCharType="begin"/> <w:instrText xml:space="preserve">PAGE </w:instrText>
<w:fldChar w:fldCharType="end"/>

Location:
trunk/docx4j/src/main/java/org/docx4j/convert/out/pdf/viaXSLFO
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/docx4j/src/main/java/org/docx4j/convert/out/pdf/viaXSLFO/Conversion.java

    r1182 r1185  
    100100         
    101101        public static final String PART_TRACKER = "partTracker"; 
     102        public static final String FIELD_TRACKER = "fieldTracker";  // are we in a field or not? 
    102103         
    103104        public Conversion(WordprocessingMLPackage wordMLPackage) { 
     
    313314                        modelStates.put("endnoteNumber", new EndnoteState() ); 
    314315                        modelStates.put(PART_TRACKER, new PartTracker() ); 
     316                        modelStates.put(FIELD_TRACKER, new InField() ); 
    315317                         
    316318                Converter.getInstance().start(wordMLPackage); 
     
    866868                NodeIterator childResults ) { 
    867869         
    868         /* Support page numbering 
     870        /* Support page numbering. 
     871         *  
     872         * Word 2007 emits: 
    869873         *  
    870874         *  <w:fldSimple w:instr=" PAGE   \* MERGEFORMAT "> 
     
    889893                       
    890894                    could also include start at value. 
     895                     
     896                 * 
     897                 * Word 2003 emits: 
     898                 *  
     899                 *       <w:instrText xml:space="preserve">PAGE  </w:instrText> 
    891900 
    892901         */ 
    893902         
    894         try { 
    895  
    896                 CTSimpleField field = null; 
     903 
     904        CTSimpleField field = null; 
     905         
     906                try { 
     907                        field = (CTSimpleField)XmlUtils.unmarshal( 
     908                                                fldSimpleNodeIt.nextNode(),  
     909                                                Context.jc,  
     910                                                CTSimpleField.class); 
     911                } catch (JAXBException e1) { 
     912                        e1.printStackTrace(); 
     913                } 
     914                         
     915                String instr = field.getInstr();                         
     916 
     917                return handleField(instr, childResults); 
    897918                 
    898                         try { 
    899                                 field = (CTSimpleField)XmlUtils.unmarshal( 
    900                                                         fldSimpleNodeIt.nextNode(),  
    901                                                         Context.jc,  
    902                                                         CTSimpleField.class); 
    903                         } catch (JAXBException e1) { 
    904                                 e1.printStackTrace(); 
    905                         }                        
    906                  
     919        } 
     920         
     921        private static DocumentFragment handleField(String instr, NodeIterator childResults) { 
     922                 
     923                try { 
     924                         
    907925            // Create a DOM builder and parse the fragment 
    908926                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();         
     
    912930 
    913931                         
    914                         String instr = field.getInstr();                         
    915932                        if ( !instr.toLowerCase().contains( "page") ) { 
    916933                                 
     
    963980         
    964981        return null; 
     982         
     983    } 
     984 
     985    public static DocumentFragment createBlockForInstrText(  
     986                WordprocessingMLPackage wmlPackage, 
     987                NodeIterator fldSimpleNodeIt, 
     988                NodeIterator childResults ) { 
     989         
     990        /* Support page numbering. 
     991         *  
     992                 * Word 2003 emits : 
     993                 *  
     994                 *               <w:fldChar w:fldCharType="begin"/> 
     995                 *  
     996                 *       <w:instrText xml:space="preserve">PAGE  </w:instrText> 
     997 
     998                                 <w:fldChar w:fldCharType="end"/> 
     999         */ 
     1000         
     1001        org.docx4j.wml.Text field = null; 
     1002         
     1003                try { 
     1004                        field =  
     1005                                (org.docx4j.wml.Text)XmlUtils.unmarshal( 
     1006                                                fldSimpleNodeIt.nextNode(),  
     1007                                                Context.jc,  
     1008                                                org.docx4j.wml.Text.class); 
     1009                } catch (JAXBException e1) { 
     1010                        e1.printStackTrace(); 
     1011                }                        
     1012         
     1013                return handleField( field.getValue(), childResults); 
    9651014         
    9661015    } 
  • trunk/docx4j/src/main/java/org/docx4j/convert/out/pdf/viaXSLFO/docx2fo.xslt

    r1182 r1185  
    1919<!-- ======================================= 
    2020 
    21          This is the beginnings of a basic XSLT 
    22          to convert WordML2FO. 
     21         This is an XSLT to convert WordML2FO. 
    2322          
    2423         I'm not aware of any more complete 
     
    349348  <xsl:template match="w:pPr | w:rPr" /> <!--  handle via extension function --> 
    350349 
    351   <xsl:template match="w:r">     
     350  <xsl:template match="w:r"> 
     351   
    352352        <xsl:choose> 
    353                 <xsl:when test="w:rPr"> 
    354                         <!--  Invoke an extension function, so we can use 
    355                               docx4j to populate the fo:block --> 
     353                <xsl:when test="java:org.docx4j.convert.out.pdf.viaXSLFO.InField.getState($modelStates)" > 
     354                        <!-- in a field, so ignore, unless this run contains a fldChar or instrText --> 
     355                         
     356                        <xsl:if test="w:fldChar"><xsl:apply-templates/></xsl:if> 
     357                         
     358                        <xsl:if test="w:instrText"><xsl:apply-templates/></xsl:if> 
     359                         
     360                </xsl:when> 
     361                <xsl:otherwise> 
    356362                 
    357                         <xsl:variable name="childResults"> 
    358                                 <xsl:apply-templates/> 
    359                         </xsl:variable> 
     363                        <xsl:choose> 
     364                                <xsl:when test="w:rPr"> 
     365                                        <!--  Invoke an extension function, so we can use 
     366                                              docx4j to populate the fo:block --> 
     367                 
     368                                        <xsl:variable name="childResults"> 
     369                                                <xsl:apply-templates/> 
     370                                        </xsl:variable> 
    360371                         
    361                         <!-- <xsl:variable name="pPrNode" select="../w:pPr" />  -->      
    362                         <xsl:variable name="rPrNode" select="w:rPr" />           
     372                                        <!-- <xsl:variable name="pPrNode" select="../w:pPr" />  -->      
     373                                        <xsl:variable name="rPrNode" select="w:rPr" />           
    363374         
    364                         <xsl:copy-of select="java:org.docx4j.convert.out.pdf.viaXSLFO.Conversion.createBlockForRPr(  
    365                                 $wmlPackage, $rPrNode, $childResults)" /> 
     375                                        <xsl:copy-of select="java:org.docx4j.convert.out.pdf.viaXSLFO.Conversion.createBlockForRPr(  
     376                                                $wmlPackage, $rPrNode, $childResults)" /> 
    366377                         
    367                 </xsl:when> 
    368                 <xsl:otherwise> 
    369                 <xsl:apply-templates/> 
    370                 </xsl:otherwise> 
    371           </xsl:choose>                                  
     378                                </xsl:when> 
     379                                <xsl:otherwise> 
     380                                <xsl:apply-templates/> 
     381                                </xsl:otherwise> 
     382                          </xsl:choose>                                  
     383                 
     384                </xsl:otherwise> 
     385                 
     386        </xsl:choose> 
     387         
    372388                 
    373389  </xsl:template> 
     
    744760  <xsl:template match="w:fldChar" > 
    745761                <xsl:copy-of  
    746                         select="java:org.docx4j.convert.out.pdf.viaXSLFO.Conversion.notImplemented(., '' )" />           
    747   </xsl:template> 
     762                        select="java:org.docx4j.convert.out.pdf.viaXSLFO.InField.updateState($modelStates, .)" />        
     763  </xsl:template> 
     764 
    748765  <xsl:template match="w:instrText" > 
     766                <xsl:variable name="childResults"> 
     767                        <xsl:apply-templates/> 
     768                </xsl:variable> 
     769                         
    749770                <xsl:copy-of  
    750                         select="java:org.docx4j.convert.out.pdf.viaXSLFO.Conversion.notImplemented(., 'no support for fields' )" />      
     771                        select="java:org.docx4j.convert.out.pdf.viaXSLFO.Conversion.createBlockForInstrText( 
     772                                $wmlPackage, ., $childResults)" /> 
    751773  </xsl:template> 
    752774 
Note: See TracChangeset for help on using the changeset viewer.