1 17 18 19 20 package embedding; 21 22 import java.io.BufferedOutputStream ; 24 import java.io.File ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.io.OutputStream ; 28 29 import javax.xml.transform.Transformer ; 31 import javax.xml.transform.TransformerFactory ; 32 import javax.xml.transform.Source ; 33 import javax.xml.transform.Result ; 34 import javax.xml.transform.stream.StreamSource ; 35 import javax.xml.transform.sax.SAXResult ; 36 37 import org.apache.fop.apps.FOUserAgent; 39 import org.apache.fop.apps.Fop; 40 import org.apache.fop.apps.FOPException; 41 import org.apache.fop.apps.FopFactory; 42 import org.apache.fop.apps.MimeConstants; 43 44 50 public class ExampleFO2RTF { 51 52 private FopFactory fopFactory = FopFactory.newInstance(); 54 55 62 public void convertFO2RTF(File fo, File rtf) throws IOException , FOPException { 63 64 FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); 65 67 OutputStream out = null; 68 69 try { 70 out = new FileOutputStream (rtf); 73 out = new BufferedOutputStream (out); 74 75 Fop fop = fopFactory.newFop(MimeConstants.MIME_RTF, foUserAgent, out); 77 78 TransformerFactory factory = TransformerFactory.newInstance(); 80 Transformer transformer = factory.newTransformer(); 82 Source src = new StreamSource (fo); 84 85 Result res = new SAXResult (fop.getDefaultHandler()); 87 88 transformer.transform(src, res); 90 91 96 } catch (Exception e) { 97 e.printStackTrace(System.err); 98 System.exit(-1); 99 } finally { 100 out.close(); 101 } 102 } 103 104 105 109 public static void main(String [] args) { 110 try { 111 System.out.println("FOP ExampleFO2RTF\n"); 112 System.out.println("Preparing..."); 113 114 File baseDir = new File ("."); 116 File outDir = new File (baseDir, "out"); 117 outDir.mkdirs(); 118 119 File fofile = new File (baseDir, "xml/fo/helloworld.fo"); 121 File rtffile = new File (outDir, "ResultFO2RTF.rtf"); 122 123 System.out.println("Input: XSL-FO (" + fofile + ")"); 124 System.out.println("Output: PDF (" + rtffile + ")"); 125 System.out.println(); 126 System.out.println("Transforming..."); 127 128 ExampleFO2RTF app = new ExampleFO2RTF(); 129 app.convertFO2RTF(fofile, rtffile); 130 131 System.out.println("Success!"); 132 } catch (Exception e) { 133 e.printStackTrace(System.err); 134 System.exit(-1); 135 } 136 } 137 } 138
| Popular Tags
|