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

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