Initial import.
[clinton/mirror/jspi/.git] / jspi / src / main / java / de / lohndirekt / print / examples / MultiDocExample.java
diff --git a/jspi/src/main/java/de/lohndirekt/print/examples/MultiDocExample.java b/jspi/src/main/java/de/lohndirekt/print/examples/MultiDocExample.java
new file mode 100644 (file)
index 0000000..3404ab6
--- /dev/null
@@ -0,0 +1,108 @@
+/**\r
+ * Copyright (C) 2003 <a href="http://www.lohndirekt.de/">lohndirekt.de</a>\r
+ *\r
+ * This library is free software; you can redistribute it and/or\r
+ * modify it under the terms of the GNU Lesser General Public\r
+ * License as published by the Free Software Foundation; either\r
+ * version 2.1 of the License, or (at your option) any later version.\r
+ * \r
+ * This library is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
+ * Lesser General Public License for more details.\r
+ * \r
+ * You should have received a copy of the GNU Lesser General Public\r
+ * License along with this library; if not, write to the Free Software\r
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
+ *  \r
+ */\r
+package de.lohndirekt.print.examples;\r
+import java.io.File;\r
+import java.io.FileInputStream;\r
+import java.io.FileNotFoundException;\r
+import java.io.InputStream;\r
+import java.net.MalformedURLException;\r
+import java.net.URISyntaxException;\r
+\r
+import javax.print.Doc;\r
+import javax.print.DocFlavor;\r
+import javax.print.MultiDocPrintJob;\r
+import javax.print.MultiDocPrintService;\r
+import javax.print.PrintException;\r
+import javax.print.SimpleDoc;\r
+import javax.print.attribute.HashDocAttributeSet;\r
+import javax.print.attribute.HashPrintRequestAttributeSet;\r
+import javax.print.attribute.PrintRequestAttributeSet;\r
+import javax.print.attribute.standard.Copies;\r
+import javax.print.attribute.standard.Media;\r
+import javax.print.attribute.standard.MediaTray;\r
+import javax.print.attribute.standard.Sides;\r
+\r
+import de.lohndirekt.print.IppPrintServiceLookup;\r
+import de.lohndirekt.print.SimpleMultiDoc;\r
+\r
+public class MultiDocExample {\r
+\r
+    public static void main(String[] args)\r
+        throws URISyntaxException, FileNotFoundException, PrintException, MalformedURLException {\r
+        // setting cups properties\r
+        System.getProperties().setProperty(IppPrintServiceLookup.URI_KEY, Messages.getString("cups.uri")); //$NON-NLS-1$\r
+        System.getProperties().setProperty(IppPrintServiceLookup.USERNAME_KEY, Messages.getString("cups.username")); //$NON-NLS-1$\r
+        System.getProperties().setProperty(IppPrintServiceLookup.PASSWORD_KEY, Messages.getString("cups.password")); //$NON-NLS-1$\r
+\r
+        // get the PrintServices\r
+        MultiDocPrintService[] services = new IppPrintServiceLookup().getMultiDocPrintServices(null, null);\r
+\r
+        // get the first Printer\r
+        if (services.length > 0) {\r
+            MultiDocPrintService service = services[0];\r
+            System.out.println("Printing on: " + service.getName());\r
+            // create a job\r
+            MultiDocPrintJob job = service.createMultiDocPrintJob();\r
+\r
+            // set the job attributes\r
+            PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();\r
+            // we just want one copy\r
+            Copies copies = new Copies(1);\r
+            attributes.add(copies);\r
+            // we want duplex printing\r
+            Sides sides = Sides.DUPLEX;\r
+            attributes.add(sides);\r
+            // print it on the main media tray\r
+            Media media = MediaTray.MAIN;\r
+            // If you have special Mediatrays (like most printers) \r
+            // you can use the class LdMediaTray and give a String to the constructor like\r
+            // new LdMediaTray("Optional2");\r
+            attributes.add(media);\r
+\r
+            // Now create some documents\r
+            File testFile = new File(Messages.getString("pdfdocument.1"));\r
+            InputStream stream = new FileInputStream(testFile);\r
+            Doc doc = new SimpleDoc(stream, DocFlavor.INPUT_STREAM.PDF, new HashDocAttributeSet());\r
+            File testFile2 = new File(Messages.getString("pdfdocument.2"));\r
+            InputStream stream2 = new FileInputStream(testFile2);\r
+            Doc doc2 = new SimpleDoc(stream2, DocFlavor.INPUT_STREAM.PDF, new HashDocAttributeSet());\r
+            // now we need a MultiDoc\r
+            SimpleMultiDoc multiDoc = SimpleMultiDoc.create(doc);\r
+            // finally the print action\r
+            try {\r
+                // we are setting the doc and the job attributes\r
+                job.print(multiDoc, attributes);\r
+\r
+                // now add several docs to your mutliDoc\r
+                // cups waits for the document with the attribute last-document = true\r
+                // wich you can set like this:\r
+                boolean lastDocument = true;\r
+                multiDoc.setNext(doc2, lastDocument);\r
+\r
+                System.out.println("printing successfull...");\r
+            } catch (PrintException e) {\r
+                e.printStackTrace();\r
+            }\r
+\r
+        } else {\r
+            System.out.println("no Services found!");\r
+        }\r
+    }\r
+\r
+}\r