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.

ExampleFO2OldStylePrint.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.awt.print.PrinterJob;
  21. import java.io.File;
  22. import java.io.IOException;
  23. //JAXP
  24. import javax.xml.transform.Transformer;
  25. import javax.xml.transform.TransformerFactory;
  26. import javax.xml.transform.Source;
  27. import javax.xml.transform.Result;
  28. import javax.xml.transform.stream.StreamSource;
  29. import javax.xml.transform.sax.SAXResult;
  30. // FOP
  31. import org.apache.fop.apps.FOUserAgent;
  32. import org.apache.fop.apps.Fop;
  33. import org.apache.fop.apps.FOPException;
  34. import org.apache.fop.apps.FopFactory;
  35. import org.apache.fop.render.print.PrintRenderer;
  36. /**
  37. * This class demonstrates printing an FO file to a PrinterJob instance.
  38. */
  39. public class ExampleFO2OldStylePrint {
  40. // configure fopFactory as desired
  41. private FopFactory fopFactory = FopFactory.newInstance();
  42. /**
  43. * Prints an FO file using an old-style PrinterJob.
  44. * @param fo the FO file
  45. * @throws IOException In case of an I/O problem
  46. * @throws FOPException In case of a FOP problem
  47. */
  48. public void printFO(File fo) throws IOException, FOPException {
  49. //Set up PrinterJob instance
  50. PrinterJob printerJob = PrinterJob.getPrinterJob();
  51. printerJob.setJobName("FOP Printing Example");
  52. PrintRenderer renderer = new PrintRenderer(printerJob);
  53. try {
  54. //Set up a custom user agent so we can supply our own renderer instance
  55. FOUserAgent userAgent = fopFactory.newFOUserAgent();
  56. userAgent.setRendererOverride(renderer);
  57. // Construct fop with desired output format (here, it is set through the user agent)
  58. Fop fop = fopFactory.newFop(userAgent);
  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. } catch (Exception e) {
  69. e.printStackTrace(System.err);
  70. System.exit(-1);
  71. }
  72. }
  73. /**
  74. * Main method.
  75. * @param args command-line arguments
  76. */
  77. public static void main(String[] args) {
  78. try {
  79. System.out.println("FOP ExampleFO2OldStylePrint\n");
  80. System.out.println("Preparing...");
  81. //Setup directories
  82. File baseDir = new File(".");
  83. File outDir = new File(baseDir, "out");
  84. outDir.mkdirs();
  85. //Setup input and output files
  86. File fofile = new File(baseDir, "xml/fo/helloworld.fo");
  87. System.out.println("Input: XSL-FO (" + fofile + ")");
  88. System.out.println("Output: old-style printing using PrinterJob");
  89. System.out.println();
  90. System.out.println("Transforming...");
  91. ExampleFO2OldStylePrint app = new ExampleFO2OldStylePrint();
  92. app.printFO(fofile);
  93. System.out.println("Success!");
  94. } catch (Exception e) {
  95. e.printStackTrace(System.err);
  96. System.exit(-1);
  97. }
  98. }
  99. }