You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExampleFO2RTF.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright 2005-2006 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package embedding;
  18. // Java
  19. import java.io.BufferedOutputStream;
  20. import java.io.File;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23. import java.io.OutputStream;
  24. //JAXP
  25. import javax.xml.transform.Transformer;
  26. import javax.xml.transform.TransformerFactory;
  27. import javax.xml.transform.Source;
  28. import javax.xml.transform.Result;
  29. import javax.xml.transform.stream.StreamSource;
  30. import javax.xml.transform.sax.SAXResult;
  31. // FOP
  32. import org.apache.fop.apps.FOUserAgent;
  33. import org.apache.fop.apps.Fop;
  34. import org.apache.fop.apps.FOPException;
  35. import org.apache.fop.apps.FopFactory;
  36. import org.apache.fop.apps.MimeConstants;
  37. /**
  38. * This class demonstrates the conversion of an FO file to RTF using FOP.
  39. * <p>
  40. * Please note that this is practically the same as the ExampleFO2PDF example. Only
  41. * the MIME parameter to the newFop() method is different!
  42. */
  43. public class ExampleFO2RTF {
  44. // configure fopFactory as desired
  45. private FopFactory fopFactory = FopFactory.newInstance();
  46. /**
  47. * Converts an FO file to a RTF file using FOP
  48. * @param fo the FO file
  49. * @param rtf the target RTF file
  50. * @throws IOException In case of an I/O problem
  51. * @throws FOPException In case of a FOP problem
  52. */
  53. public void convertFO2RTF(File fo, File rtf) throws IOException, FOPException {
  54. FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
  55. // configure foUserAgent as desired
  56. OutputStream out = null;
  57. try {
  58. // Setup output stream. Note: Using BufferedOutputStream
  59. // for performance reasons (helpful with FileOutputStreams).
  60. out = new FileOutputStream(rtf);
  61. out = new BufferedOutputStream(out);
  62. // Construct fop with desired output format
  63. Fop fop = fopFactory.newFop(MimeConstants.MIME_RTF, foUserAgent, out);
  64. // Setup JAXP using identity transformer
  65. TransformerFactory factory = TransformerFactory.newInstance();
  66. Transformer transformer = factory.newTransformer(); // identity transformer
  67. // Setup input stream
  68. Source src = new StreamSource(fo);
  69. // Resulting SAX events (the generated FO) must be piped through to FOP
  70. Result res = new SAXResult(fop.getDefaultHandler());
  71. // Start XSLT transformation and FOP processing
  72. transformer.transform(src, res);
  73. // Please note: getResults() won't work for RTF and other flow formats (like MIF)
  74. // as the layout engine is not involved in the conversion. The page-breaking
  75. // is done by the application opening the generated file (like MS Word).
  76. //FormattingResults foResults = fop.getResults();
  77. } catch (Exception e) {
  78. e.printStackTrace(System.err);
  79. System.exit(-1);
  80. } finally {
  81. out.close();
  82. }
  83. }
  84. /**
  85. * Main method.
  86. * @param args command-line arguments
  87. */
  88. public static void main(String[] args) {
  89. try {
  90. System.out.println("FOP ExampleFO2RTF\n");
  91. System.out.println("Preparing...");
  92. //Setup directories
  93. File baseDir = new File(".");
  94. File outDir = new File(baseDir, "out");
  95. outDir.mkdirs();
  96. //Setup input and output files
  97. File fofile = new File(baseDir, "xml/fo/helloworld.fo");
  98. File rtffile = new File(outDir, "ResultFO2RTF.rtf");
  99. System.out.println("Input: XSL-FO (" + fofile + ")");
  100. System.out.println("Output: PDF (" + rtffile + ")");
  101. System.out.println();
  102. System.out.println("Transforming...");
  103. ExampleFO2RTF app = new ExampleFO2RTF();
  104. app.convertFO2RTF(fofile, rtffile);
  105. System.out.println("Success!");
  106. } catch (Exception e) {
  107. e.printStackTrace(System.err);
  108. System.exit(-1);
  109. }
  110. }
  111. }