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

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