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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright 2005 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.Fop;
  33. import org.apache.fop.apps.FOPException;
  34. import org.apache.fop.apps.MimeConstants;
  35. /**
  36. * This class demonstrates the conversion of an FO file to RTF using FOP.
  37. * <p>
  38. * Please note that this is practically the same as the ExampleFO2PDF example. Only
  39. * one parameter to the Fop contructor is different!
  40. */
  41. public class ExampleFO2RTF {
  42. /**
  43. * Converts an FO file to a RTF file using FOP
  44. * @param fo the FO file
  45. * @param rtf the target RTF file
  46. * @throws IOException In case of an I/O problem
  47. * @throws FOPException In case of a FOP problem
  48. */
  49. public void convertFO2RTF(File fo, File rtf) throws IOException, FOPException {
  50. OutputStream out = null;
  51. try {
  52. // Construct fop with desired output format
  53. Fop fop = new Fop(MimeConstants.MIME_RTF);
  54. // Setup output stream. Note: Using BufferedOutputStream
  55. // for performance reasons (helpful with FileOutputStreams).
  56. out = new FileOutputStream(rtf);
  57. out = new BufferedOutputStream(out);
  58. fop.setOutputStream(out);
  59. // Setup JAXP using identity transformer
  60. TransformerFactory factory = TransformerFactory.newInstance();
  61. Transformer transformer = factory.newTransformer(); // identity transformer
  62. // Setup input stream
  63. Source src = new StreamSource(fo);
  64. // Resulting SAX events (the generated FO) must be piped through to FOP
  65. Result res = new SAXResult(fop.getDefaultHandler());
  66. // Start XSLT transformation and FOP processing
  67. transformer.transform(src, res);
  68. // Please note: getResults() won't work for RTF and other flow formats (like MIF)
  69. // as the layout engine is not involved in the conversion. The page-breaking
  70. // is done by the application opening the generated file (like MS Word).
  71. //FormattingResults foResults = fop.getResults();
  72. } catch (Exception e) {
  73. e.printStackTrace(System.err);
  74. System.exit(-1);
  75. } finally {
  76. out.close();
  77. }
  78. }
  79. /**
  80. * Main method.
  81. * @param args command-line arguments
  82. */
  83. public static void main(String[] args) {
  84. try {
  85. System.out.println("FOP ExampleFO2RTF\n");
  86. System.out.println("Preparing...");
  87. //Setup directories
  88. File baseDir = new File(".");
  89. File outDir = new File(baseDir, "out");
  90. outDir.mkdirs();
  91. //Setup input and output files
  92. File fofile = new File(baseDir, "xml/fo/helloworld.fo");
  93. File rtffile = new File(outDir, "ResultFO2RTF.rtf");
  94. System.out.println("Input: XSL-FO (" + fofile + ")");
  95. System.out.println("Output: PDF (" + rtffile + ")");
  96. System.out.println();
  97. System.out.println("Transforming...");
  98. ExampleFO2RTF app = new ExampleFO2RTF();
  99. app.convertFO2RTF(fofile, rtffile);
  100. System.out.println("Success!");
  101. } catch (Exception e) {
  102. e.printStackTrace(System.err);
  103. System.exit(-1);
  104. }
  105. }
  106. }