Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ExampleFO2RTF.java 4.7KB

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