Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ExampleFO2OldStylePrint.java 4.0KB

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