Ignore:
Timestamp:
08/06/10 21:25:16 (22 months ago)
Author:
jharrop
Message:

Support for page number field <w:fldSimple w:instr=" PAGE \* MERGEFORMAT ">

File:
1 edited

Legend:

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

    r1139 r1182  
    1414 
    1515import javax.xml.bind.JAXBElement; 
     16import javax.xml.bind.JAXBException; 
    1617import javax.xml.bind.Unmarshaller; 
    1718import javax.xml.parsers.DocumentBuilderFactory; 
     
    5152import org.docx4j.openpackaging.packages.WordprocessingMLPackage; 
    5253import org.docx4j.openpackaging.parts.relationships.RelationshipsPart; 
     54import org.docx4j.wml.CTPageNumber; 
     55import org.docx4j.wml.CTSimpleField; 
    5356import org.docx4j.wml.Ftr; 
    5457import org.docx4j.wml.Hdr; 
     58import org.docx4j.wml.NumberFormat; 
    5559import org.docx4j.wml.PPr; 
    5660import org.docx4j.wml.RFonts; 
     
    856860        } 
    857861         
     862 
     863    public static DocumentFragment createBlockForFldSimple(  
     864                WordprocessingMLPackage wmlPackage, 
     865                NodeIterator fldSimpleNodeIt, 
     866                NodeIterator childResults ) { 
     867         
     868        /* Support page numbering 
     869         *  
     870         *  <w:fldSimple w:instr=" PAGE   \* MERGEFORMAT "> 
     871                  <w:r> 
     872                    <w:rPr> 
     873                      <w:noProof/> 
     874                    </w:rPr> 
     875                    <w:t>- 1 -</w:t> 
     876                  </w:r> 
     877                </w:fldSimple> 
     878                 
     879                could also include: 
     880                 
     881                                { PAGE \* Arabic } 
     882                                { PAGE \* alphabetic } 
     883                                { PAGE \* ALPHABETIC } 
     884                                { PAGE \* roman } 
     885                                { PAGE \* ROMAN }                
     886 
     887                    <w:sectPr> 
     888                      <w:pgNumType w:fmt="numberInDash"/> 
     889                       
     890                    could also include start at value. 
     891 
     892         */ 
     893         
     894        try { 
     895 
     896                CTSimpleField field = null; 
     897                 
     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                 
     907            // Create a DOM builder and parse the fragment 
     908                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();         
     909                        Document document = factory.newDocumentBuilder().newDocument(); 
     910                         
     911                        //log.info("Document: " + document.getClass().getName() ); 
     912 
     913                         
     914                        String instr = field.getInstr();                         
     915                        if ( !instr.toLowerCase().contains( "page") ) { 
     916                                 
     917                                if (log.isDebugEnabled() ) { 
     918                                        return message("no support for fields (except PAGE numbering)"); 
     919                                } else { 
     920                                         
     921                                        // Try this 
     922                                        Node foInlineElement = document.createElementNS("http://www.w3.org/1999/XSL/Format", "fo:inline");                       
     923                                        document.appendChild(foInlineElement); 
     924                                         
     925                                        Node n = childResults.nextNode(); 
     926                                        XmlUtils.treeCopy( (DTMNodeProxy)n,  foInlineElement ); 
     927                                         
     928                                        DocumentFragment docfrag = document.createDocumentFragment(); 
     929                                        docfrag.appendChild(document.getDocumentElement()); 
     930 
     931                                        return docfrag;                                  
     932                                } 
     933                        } 
     934 
     935                        // Its a PAGE numbering field 
     936                         
     937                        /* 
     938                         * For XSL FO page numbering, see generally 
     939                         * http://www.dpawson.co.uk/xsl/sect3/N8703.html 
     940                         *  
     941                         * In summary,  
     942                         *  
     943                         * <fo:page-sequence master-name="blagh"  
     944                         *                              format="i" 
     945                         *                              initial-page-number="1"> .... 
     946                         *  
     947                         */ 
     948 
     949                        Node foPageNumber = document.createElementNS("http://www.w3.org/1999/XSL/Format",  
     950                                        "fo:page-number");                       
     951                        document.appendChild(foPageNumber); 
     952                                                 
     953                        DocumentFragment docfrag = document.createDocumentFragment(); 
     954                        docfrag.appendChild(document.getDocumentElement()); 
     955 
     956                        return docfrag; 
     957                                                 
     958                } catch (Exception e) { 
     959                        e.printStackTrace(); 
     960                        System.out.println(e.toString() ); 
     961                        log.error(e); 
     962                }  
     963         
     964        return null; 
     965         
     966    } 
     967     
     968    public static String getPageNumberFormat(WordprocessingMLPackage wordmlPackage, int sectionNumber) { 
     969         
     970        SectionWrapper sw = wordmlPackage.getDocumentModel().getSections().get(sectionNumber-1); 
     971         
     972        if (sw.getSectPr()==null) return "1"; 
     973         
     974        CTPageNumber pageNumber = sw.getSectPr().getPgNumType(); 
     975         
     976        if (pageNumber==null) return "1"; 
     977         
     978        NumberFormat format = pageNumber.getFmt(); 
     979         
     980        log.debug("w:pgNumType/@w:fmt=" + format.toString()); 
     981         
     982        if (format==null) return "1"; 
     983         
     984//       *     &lt;enumeration value="decimal"/> 
     985//       *     &lt;enumeration value="upperRoman"/> 
     986//       *     &lt;enumeration value="lowerRoman"/> 
     987//       *     &lt;enumeration value="upperLetter"/> 
     988//       *     &lt;enumeration value="lowerLetter"/>             
     989        if (format==NumberFormat.DECIMAL) 
     990                return "1"; 
     991        else if (format==NumberFormat.UPPER_ROMAN) 
     992                return "I"; 
     993        else if (format==NumberFormat.LOWER_ROMAN) 
     994                return "i"; 
     995        //else if (format.equals(NumberFormat.UPPER_LETTER)) 
     996        else if (format==NumberFormat.UPPER_LETTER) 
     997                return "A"; 
     998        else if (format==NumberFormat.LOWER_LETTER) 
     999                return "a"; 
     1000 
     1001        // TODO .. other formats 
     1002                 
     1003        return "1"; 
     1004    } 
     1005         
     1006    public static String getPageNumberInitial(WordprocessingMLPackage wordmlPackage, int sectionNumber) { 
     1007 
     1008        SectionWrapper sw = wordmlPackage.getDocumentModel().getSections().get(sectionNumber-1); 
     1009 
     1010        if (sw.getSectPr()==null) return "1"; 
     1011         
     1012        CTPageNumber pageNumber = sw.getSectPr().getPgNumType(); 
     1013         
     1014        if (pageNumber==null) { 
     1015                log.debug("No PgNumType"); 
     1016                return "1"; 
     1017        } 
     1018         
     1019        BigInteger start = pageNumber.getStart(); 
     1020         
     1021        if (start==null) return "1"; 
     1022         
     1023        return start.toString(); 
     1024    } 
     1025         
    8581026} 
    8591027     
Note: See TracChangeset for help on using the changeset viewer.