]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
Example for printing to an old-style PrinterJob instance.
authorJeremias Maerki <jeremias@apache.org>
Wed, 27 Jul 2005 12:22:53 +0000 (12:22 +0000)
committerJeremias Maerki <jeremias@apache.org>
Wed, 27 Jul 2005 12:22:53 +0000 (12:22 +0000)
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@225498 13f79535-47bb-0310-9956-ffa450edef68

examples/embedding/java/embedding/ExampleFO2OldStylePrint.java [new file with mode: 0644]

diff --git a/examples/embedding/java/embedding/ExampleFO2OldStylePrint.java b/examples/embedding/java/embedding/ExampleFO2OldStylePrint.java
new file mode 100644 (file)
index 0000000..c901abc
--- /dev/null
@@ -0,0 +1,121 @@
+/*\r
+ * Copyright 1999-2005 The Apache Software Foundation.\r
+ * \r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ * \r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ * \r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+/* $Id$ */\r
\r
+package embedding;\r
+\r
+// Java\r
+import java.awt.print.PrinterJob;\r
+import java.io.File;\r
+import java.io.IOException;\r
+\r
+//JAXP\r
+import javax.xml.transform.Transformer;\r
+import javax.xml.transform.TransformerFactory;\r
+import javax.xml.transform.Source;\r
+import javax.xml.transform.Result;\r
+import javax.xml.transform.stream.StreamSource;\r
+import javax.xml.transform.sax.SAXResult;\r
+\r
+\r
+// FOP\r
+import org.apache.fop.apps.FOUserAgent;\r
+import org.apache.fop.apps.Fop;\r
+import org.apache.fop.apps.FOPException;\r
+import org.apache.fop.render.print.PrintRenderer;\r
+\r
+/**\r
+ * This class demonstrates printing an FO file to a PrinterJob instance.\r
+ */\r
+public class ExampleFO2OldStylePrint {\r
+\r
+    /**\r
+     * Prints an FO file using an old-style PrinterJob.\r
+     * @param fo the FO file\r
+     * @throws IOException In case of an I/O problem\r
+     * @throws FOPException In case of a FOP problem\r
+     */\r
+    public void printFO(File fo) throws IOException, FOPException {\r
+        \r
+        //Set up PrinterJob instance\r
+        PrinterJob printerJob = PrinterJob.getPrinterJob();\r
+        printerJob.setJobName("FOP Printing Example");\r
+\r
+        PrintRenderer renderer = new PrintRenderer(printerJob);\r
+        \r
+        try {\r
+            //Set up a custom user agent so we can supply our own renderer instance\r
+            FOUserAgent userAgent = new FOUserAgent();\r
+            userAgent.setRendererOverride(renderer);\r
+            \r
+            // Construct fop with desired output format\r
+            Fop fop = new Fop(Fop.RENDER_PRINT, userAgent);\r
+            //Note: the first parameter here has no effect if we use \r
+            //FOUserAgent.setRendererOverride()\r
+\r
+            // Setup JAXP using identity transformer\r
+            TransformerFactory factory = TransformerFactory.newInstance();\r
+            Transformer transformer = factory.newTransformer(); // identity transformer\r
+            \r
+            // Setup input stream\r
+            Source src = new StreamSource(fo);\r
+\r
+            // Resulting SAX events (the generated FO) must be piped through to FOP\r
+            Result res = new SAXResult(fop.getDefaultHandler());\r
+            \r
+            // Start XSLT transformation and FOP processing\r
+            transformer.transform(src, res);\r
+\r
+        } catch (Exception e) {\r
+            e.printStackTrace(System.err);\r
+            System.exit(-1);\r
+        }\r
+    }\r
+\r
+\r
+    /**\r
+     * Main method.\r
+     * @param args command-line arguments\r
+     */\r
+    public static void main(String[] args) {\r
+        try {\r
+            System.out.println("FOP ExampleFO2OldStylePrint\n");\r
+            System.out.println("Preparing...");\r
+            \r
+            //Setup directories\r
+            File baseDir = new File(".");\r
+            File outDir = new File(baseDir, "out");\r
+            outDir.mkdirs();\r
+\r
+            //Setup input and output files            \r
+            File fofile = new File(baseDir, "xml/fo/helloworld.fo");\r
+\r
+            System.out.println("Input: XSL-FO (" + fofile + ")");\r
+            System.out.println("Output: old-style printing using PrinterJob");\r
+            System.out.println();\r
+            System.out.println("Transforming...");\r
+            \r
+            ExampleFO2OldStylePrint app = new ExampleFO2OldStylePrint();\r
+            app.printFO(fofile);\r
+            \r
+            System.out.println("Success!");\r
+        } catch (Exception e) {\r
+            e.printStackTrace(System.err);\r
+            System.exit(-1);\r
+        }\r
+    }\r
+}\r