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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. try {
  53. //Set up a custom user agent so we can supply our own renderer instance
  54. FOUserAgent userAgent = fopFactory.newFOUserAgent();
  55. //Set up our own PrintRenderer instance so we can supply a special PrinterJob instance.
  56. PrintRenderer renderer = new PrintRenderer(printerJob);
  57. renderer.setUserAgent(userAgent);
  58. userAgent.setRendererOverride(renderer);
  59. // Construct fop with desired output format (here, it is set through the user agent)
  60. Fop fop = fopFactory.newFop(userAgent);
  61. // Setup JAXP using identity transformer
  62. TransformerFactory factory = TransformerFactory.newInstance();
  63. Transformer transformer = factory.newTransformer(); // identity transformer
  64. // Setup input stream
  65. Source src = new StreamSource(fo);
  66. // Resulting SAX events (the generated FO) must be piped through to FOP
  67. Result res = new SAXResult(fop.getDefaultHandler());
  68. // Start XSLT transformation and FOP processing
  69. transformer.transform(src, res);
  70. } catch (Exception e) {
  71. e.printStackTrace(System.err);
  72. System.exit(-1);
  73. }
  74. }
  75. /**
  76. * Main method.
  77. * @param args command-line arguments
  78. */
  79. public static void main(String[] args) {
  80. try {
  81. System.out.println("FOP ExampleFO2OldStylePrint\n");
  82. System.out.println("Preparing...");
  83. //Setup directories
  84. File baseDir = new File(".");
  85. File outDir = new File(baseDir, "out");
  86. outDir.mkdirs();
  87. //Setup input and output files
  88. File fofile = new File(baseDir, "xml/fo/helloworld.fo");
  89. System.out.println("Input: XSL-FO (" + fofile + ")");
  90. System.out.println("Output: old-style printing using PrinterJob");
  91. System.out.println();
  92. System.out.println("Transforming...");
  93. ExampleFO2OldStylePrint app = new ExampleFO2OldStylePrint();
  94. app.printFO(fofile);
  95. System.out.println("Success!");
  96. } catch (Exception e) {
  97. e.printStackTrace(System.err);
  98. System.exit(-1);
  99. }
  100. }
  101. }