Ignore:
Timestamp:
10/22/08 17:18:02 (4 years ago)
Author:
jharrop
Message:

Use docx 2 html XSLT from OpenXMLViewer. Numbering, image handling, hyperlinks not yet implemented.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/docx4j/src/main/java/org/docx4j/openpackaging/packages/WordprocessingMLPackage.java

    r557 r558  
    242242    } 
    243243 
    244         /** Create an html version of the document, using CSS font family 
    245          *  stacks.  This is appropriate if the HTML is intended for 
    246          *  viewing in a web browser, rather than an intermediate step 
    247          *  on the way to generating PDF output.  
    248          *  
    249          * @param result 
    250          *            The javax.xml.transform.Result object to transform into  
    251          *  
    252          * */  
    253     public void html(javax.xml.transform.Result result) throws Exception { 
    254  
    255         html(result, true); 
    256     } 
    257  
    258     public void html(javax.xml.transform.Result result, boolean fontFamilyStack) throws Exception { 
    259  
    260                 // Prep parameters 
    261         HtmlSettings htmlSettings = new HtmlSettings(); 
    262         htmlSettings.setFontFamilyStack(fontFamilyStack); 
    263          
    264                 html(result, htmlSettings); 
    265     } 
    266      
    267         /** Create an html version of the document.  
    268          *  
    269          * @param result 
    270          *            The javax.xml.transform.Result object to transform into  
    271          *  
    272          * */  
    273     public void html(javax.xml.transform.Result result, HtmlSettings htmlSettings) throws Exception { 
    274          
    275         /* 
    276          * Given that word2html.xsl is freely available, use a 
    277          * version of it adapted to process the 
    278          * pck:package/pck:part stuff emitted by Word 2007. 
    279          *  
    280          */      
    281                 XmlPackage worker = new XmlPackage(this); 
    282                 org.docx4j.xmlPackage.Package pkg = worker.get(); 
    283          
    284                 JAXBContext jc = Context.jcXmlPackage; 
    285                 Marshaller marshaller=jc.createMarshaller(); 
    286                 org.w3c.dom.Document doc = org.docx4j.XmlUtils.neww3cDomDocument(); 
    287  
    288                 marshaller.marshal(pkg, doc); 
    289                  
    290                 log.info("wordDocument created for PDF rendering!"); 
    291  
    292  
    293                 // Get the xslt file - Works in Eclipse - note absence of leading '/' 
    294                 java.io.InputStream xslt = org.docx4j.utils.ResourceUtils.getResource("org/docx4j/openpackaging/packages/wordml2html-2007.xslt"); 
    295                  
    296                 // Prep parameters 
    297                 if (htmlSettings==null) { 
    298                         htmlSettings = new HtmlSettings(); 
    299                         // ..Ensure that the font names in the XHTML have been mapped to these matches 
    300                         //     possibly via an extension function in the XSLT 
    301                 } 
    302                  
    303                 if (htmlSettings.getFontSubstituter()==null) { 
    304                         if (fontSubstituter==null) { 
    305                                 log.debug("Creating new Substituter."); 
    306                                 setFontSubstituter(new Substituter()); 
    307                         } else { 
    308                                 log.debug("Using existing Substituter."); 
    309                         } 
    310                         htmlSettings.setFontSubstituter(fontSubstituter); 
    311                 } 
    312                  
    313                 // Now do the transformation 
    314                 org.docx4j.XmlUtils.transform(doc, xslt, htmlSettings.getSettings(), result); 
    315                  
    316                 log.info("wordDocument transformed to xhtml .."); 
    317          
    318     } 
    319      
    320244     
    321245     
     
    346270    } 
    347271 
    348     private Substituter fontSubstituter; 
     272    public Substituter getFontSubstituter() { 
     273                return fontSubstituter; 
     274        } 
     275 
     276        private Substituter fontSubstituter; 
    349277     
    350278        /** Create a pdf version of the document.  
     
    376304                org.w3c.dom.Document xhtmlDoc = org.docx4j.XmlUtils.neww3cDomDocument(); 
    377305                javax.xml.transform.dom.DOMResult result = new javax.xml.transform.dom.DOMResult(xhtmlDoc); 
    378                 html(result, false); // false -> don't use HTML fonts. 
     306                org.docx4j.convert.out.html.HtmlExporter.html(this, result, false); // false -> don't use HTML fonts. 
    379307                                 
    380308                // Now render the XHTML 
     
    386314                org.xhtmlrenderer.extend.FontResolver resolver = renderer.getFontResolver();             
    387315                                 
    388                 Map fontMappings = fontSubstituter.getFontMappings(); 
     316                Map fontMappings = getFontSubstituter().getFontMappings(); 
    389317                Map fontsInUse = this.getMainDocumentPart().fontsInUse(); 
    390318                Iterator fontMappingsIterator = fontsInUse.entrySet().iterator(); 
     
    571499        } 
    572500 
    573         public static class HtmlSettings { 
    574                  
    575                 Boolean fontFamilyStack = Boolean.FALSE;                 
    576                 public void setFontFamilyStack(boolean val) { 
    577                         fontFamilyStack = new Boolean(val); 
    578                 } 
    579                  
    580                 String docxWiki = null; // edit | open   
    581                 public void setDocxWiki(String docxWiki) { 
    582                         this.docxWiki = docxWiki; 
    583                 } 
    584  
    585                 String docxWikiSdtID = null;     
    586                 public void setDocxWikiSdtID(String docxWikiSdtID) { 
    587                         this.docxWikiSdtID = docxWikiSdtID; 
    588                 } 
    589                  
    590                 String docID = null; 
    591                 public void setDocID(String docID) { 
    592                         this.docID = docID; 
    593                 } 
    594                  
    595                  
    596                 Substituter fontSubstituter = null;              
    597                 public void setFontSubstituter(Substituter fontSubstituter) { 
    598                         this.fontSubstituter = fontSubstituter; 
    599                 } 
    600                 public Substituter getFontSubstituter() { 
    601                         return fontSubstituter; 
    602                 } 
    603                  
    604                  
    605                 Map<String, Object> getSettings() { 
    606                         Map<String, Object> settings = new java.util.HashMap<String, Object>(); 
    607                          
    608                         settings.put("fontFamilyStack", fontFamilyStack); 
    609                         settings.put("docxWiki", docxWiki); 
    610                         settings.put("docxWikiSdtID", docxWikiSdtID); 
    611                         settings.put("docID", docID); 
    612                         settings.put("substituterInstance", fontSubstituter); 
    613                          
    614                         return settings; 
    615                 } 
    616                  
    617         } 
    618501         
    619502} 
Note: See TracChangeset for help on using the changeset viewer.