Ignore:
Timestamp:
01/13/08 08:09:24 (4 years ago)
Author:
jharrop
Message:

Render the package as an HTML document or a PDF. (Sample renamed to OpenMainDocumentAndTraverse?)
XmlUtils?: method to return a new dom document
ResourceUtils?: abstracted this out

Location:
trunk/docx4j/src/main/java/org/docx4j/openpackaging/packages
Files:
1 added
1 edited

Legend:

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

    r124 r125  
    2121 
    2222 
     23import java.io.File; 
     24import java.io.FileOutputStream; 
     25import java.io.IOException; 
     26import java.io.OutputStream; 
     27import java.nio.ByteBuffer; 
     28 
     29import javax.xml.bind.JAXBContext; 
     30import javax.xml.bind.Marshaller; 
     31import javax.xml.parsers.DocumentBuilderFactory; 
     32 
    2333import org.apache.log4j.Logger; 
     34import org.docx4j.jaxb.Context; 
    2435import org.docx4j.openpackaging.Base; 
    2536import org.docx4j.openpackaging.parts.DocPropsCorePart; 
     
    4051import org.docx4j.openpackaging.io.LoadFromZipFile; 
    4152import org.docx4j.openpackaging.io.SaveToZipFile; 
     53import org.xhtmlrenderer.pdf.ITextRenderer; 
     54 
     55import com.lowagie.text.DocumentException; 
    4256 
    4357 
     
    147161                return mainDoc; 
    148162        } 
     163         
     164         
     165 
     166        /** Create an html version of the document.  
     167         *  
     168         * @param result 
     169         *            The javax.xml.transform.Result object to transform into  
     170         *  
     171         * */  
     172    public void html(javax.xml.transform.Result result) throws Exception { 
     173         
     174        /* 
     175         * Given that word2html.xsl is freely available, we use the second 
     176         * approach. 
     177         *  
     178         * The question then is how the stylesheet is made to work with 
     179         * our main document and style definition parts. 
     180         *  
     181         * For now, I've just edited it a little to accept our parts wrapped 
     182         * in a <w:wordDocument> element.  Since that's a completely 
     183         * arbitrary format, it may be better in due course to process 
     184         * pck:package/pck:part 
     185         *  
     186         */ 
     187         
     188                // so, put the 2 parts together into a single document  
     189        // The JAXB object org.docx4j.wml.WordDocument is 
     190        // custom built for this purpose. 
     191         
     192        // Create a org.docx4j.wml.WordDocument object 
     193        org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory(); 
     194        org.docx4j.wml.WordDocument wd = factory.createWordDocument(); 
     195        // Set its parts 
     196        // .. the main document part 
     197                MainDocumentPart documentPart = getMainDocumentPart();  
     198                org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document)documentPart.getJaxbElement();          
     199        wd.setDocument(wmlDocumentEl); 
     200        // .. the style part 
     201        org.docx4j.openpackaging.parts.WordprocessingML.StyleDefinitionsPart stylesPart = documentPart.getStyleDefinitionsPart(); 
     202        org.docx4j.wml.Styles styles = (org.docx4j.wml.Styles)stylesPart.getJaxbElement(); 
     203        wd.setStyles(styles); 
     204        // Now marshall it 
     205                JAXBContext jc = Context.jc; 
     206                Marshaller marshaller=jc.createMarshaller(); 
     207                org.w3c.dom.Document doc = org.docx4j.XmlUtils.neww3cDomDocument(); 
     208 
     209                marshaller.marshal(wd, doc); 
     210                 
     211                log.info("wordDocument created for PDF rendering!"); 
     212                 
     213                // Now transform this into XHTML 
     214                javax.xml.transform.TransformerFactory tfactory = javax.xml.transform.TransformerFactory.newInstance(); 
     215                javax.xml.transform.dom.DOMSource domSource = new javax.xml.transform.dom.DOMSource(doc); 
     216 
     217                // Get the xslt file 
     218                java.io.InputStream is = null; 
     219                        // Works in Eclipse - note absence of leading '/' 
     220                        is = org.docx4j.utils.ResourceUtils.getResource("org/docx4j/openpackaging/packages/wordml2html-2007.xslt"); 
     221                                 
     222                // Use the factory to create a template containing the xsl file 
     223                javax.xml.transform.Templates template = tfactory.newTemplates( 
     224                                new javax.xml.transform.stream.StreamSource(is)); 
     225                 
     226                // Use the template to create a transformer 
     227                javax.xml.transform.Transformer xformer = template.newTransformer(); 
     228 
     229                //DEBUGGING  
     230                // use the identity transform if you want to send wordDocument; 
     231                // otherwise you'll get the XHTML 
     232                //javax.xml.transform.Transformer xformer = tfactory.newTransformer(); 
     233                 
     234                xformer.transform(domSource, result); 
     235 
     236                log.info("wordDocument transformed to xhtml .."); 
     237         
     238    } 
     239 
     240        /** Create a pdf version of the document.  
     241         *  
     242         * @param os 
     243         *            The OutputStream to write the pdf to  
     244         *  
     245         * */      
     246    public void pdf(OutputStream os) throws Exception { 
     247         
     248        /* 
     249         * There are 2 broad approaches we could use to render the document 
     250         * as a PDF: 
     251         *  
     252         * 1.  XSL-FO 
     253         * 2.  XHTML to PDF 
     254         *  
     255         * Given that a word2html.xsl is already freely available, we use  
     256         * the second approach. 
     257         *  
     258         * The question then is how the stylesheet is made to work with 
     259         * our main document and style definition parts. 
     260         *  
     261         * For now, I've just edited it a little to accept our parts wrapped 
     262         * in a <w:wordDocument> element.  Since that's a completely 
     263         * arbitrary format, it may be better in due course to process 
     264         * pck:package/pck:part 
     265         *  
     266         */ 
     267                                 
     268        // Put the html in result 
     269                org.w3c.dom.Document xhtmlDoc = org.docx4j.XmlUtils.neww3cDomDocument(); 
     270                javax.xml.transform.dom.DOMResult result = new javax.xml.transform.dom.DOMResult(xhtmlDoc); 
     271                html(result); 
     272                                 
     273                // Now render the XHTML 
     274                org.xhtmlrenderer.pdf.ITextRenderer renderer = new org.xhtmlrenderer.pdf.ITextRenderer(); 
     275                renderer.setDocument(xhtmlDoc, null); 
     276                renderer.layout(); 
     277                 
     278                renderer.createPDF(os); 
     279                 
     280        }        
     281         
    149282 
    150283        public static WordprocessingMLPackage createTestPackage() throws InvalidFormatException { 
Note: See TracChangeset for help on using the changeset viewer.