3ea135bb |
1 | /**\r |
2 | * Copyright (C) 2003 <a href="http://www.lohndirekt.de/">lohndirekt.de</a>\r |
3 | *\r |
4 | * This library is free software; you can redistribute it and/or\r |
5 | * modify it under the terms of the GNU Lesser General Public\r |
6 | * License as published by the Free Software Foundation; either\r |
7 | * version 2.1 of the License, or (at your option) any later version.\r |
8 | * \r |
9 | * This library is distributed in the hope that it will be useful,\r |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of\r |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r |
12 | * Lesser General Public License for more details.\r |
13 | * \r |
14 | * You should have received a copy of the GNU Lesser General Public\r |
15 | * License along with this library; if not, write to the Free Software\r |
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r |
17 | * \r |
18 | */\r |
19 | package de.lohndirekt.print.examples;\r |
20 | \r |
21 | import java.io.File;\r |
22 | import java.io.FileInputStream;\r |
23 | import java.io.IOException;\r |
24 | import java.io.InputStream;\r |
25 | import java.net.URISyntaxException;\r |
26 | import java.util.Locale;\r |
27 | \r |
28 | import javax.print.CancelablePrintJob;\r |
29 | import javax.print.Doc;\r |
30 | import javax.print.DocFlavor;\r |
31 | import javax.print.PrintException;\r |
32 | import javax.print.PrintService;\r |
33 | import javax.print.SimpleDoc;\r |
34 | import javax.print.attribute.HashDocAttributeSet;\r |
35 | import javax.print.attribute.HashPrintRequestAttributeSet;\r |
36 | import javax.print.attribute.PrintRequestAttributeSet;\r |
37 | import javax.print.attribute.standard.Copies;\r |
38 | import javax.print.attribute.standard.RequestingUserName;\r |
39 | import javax.print.attribute.standard.Sides;\r |
40 | \r |
41 | import de.lohndirekt.print.IppPrintServiceLookup;\r |
42 | import de.lohndirekt.print.attribute.auth.RequestingUserPassword;\r |
43 | \r |
44 | /**\r |
45 | * @author sefftinge\r |
46 | * \r |
47 | * \r |
48 | */\r |
49 | public class SimpleDocExample {\r |
50 | \r |
51 | /**\r |
52 | * @param args\r |
53 | * @throws URISyntaxException\r |
54 | * @throws PrintException\r |
55 | * @throws IOException\r |
56 | */\r |
57 | public static void main(String[] args) throws URISyntaxException,\r |
58 | PrintException, IOException {\r |
59 | \r |
60 | // setting cups properties\r |
61 | System.getProperties().setProperty(IppPrintServiceLookup.URI_KEY,\r |
62 | Messages.getString("cups.uri")); //$NON-NLS-1$\r |
63 | System.getProperties().setProperty(IppPrintServiceLookup.USERNAME_KEY,\r |
64 | Messages.getString("cups.username")); //$NON-NLS-1$\r |
65 | System.getProperties().setProperty(IppPrintServiceLookup.PASSWORD_KEY,\r |
66 | Messages.getString("cups.password")); //$NON-NLS-1$\r |
67 | \r |
68 | // get the PrintServices\r |
69 | PrintService[] services = new IppPrintServiceLookup().getPrintServices();\r |
70 | \r |
71 | // get the first Printer\r |
72 | if (services.length > 0) {\r |
73 | PrintService service = null;\r |
74 | System.out.println("Please choose a print service:");\r |
75 | for (int i = 0; i < services.length; i++) {\r |
76 | PrintService service2 = services[i];\r |
77 | System.out.println("[" + i + "] : " + service2.getName());\r |
78 | }\r |
79 | // let the user choose a service\r |
80 | while (true) {\r |
81 | int serviceToUse = Integer.valueOf(readStdIn()).intValue();\r |
82 | if (serviceToUse < 0 || serviceToUse >= services.length) {\r |
83 | System.out.println("Bitte eine Zahl zwischen 0 und "\r |
84 | + (services.length - 1) + " eingeben.");\r |
85 | } else {\r |
86 | service = services[serviceToUse];\r |
87 | break;\r |
88 | }\r |
89 | }\r |
90 | \r |
91 | // ask for username\r |
92 | System.out.print("Username : ");\r |
93 | String username = readStdIn().trim();\r |
94 | String password = null;\r |
95 | if (username != null && username.trim().length() > 0) {\r |
96 | System.out.print("Password : ");\r |
97 | password = readStdIn().trim();\r |
98 | }\r |
99 | \r |
100 | System.out.println("Printing on: " + service.getName());\r |
101 | // create a job\r |
102 | CancelablePrintJob job = (CancelablePrintJob) service\r |
103 | .createPrintJob();\r |
104 | \r |
105 | // set the job attributes\r |
106 | PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();\r |
107 | \r |
108 | if (username != null && username.trim().length() > 0) {\r |
109 | RequestingUserName usernameAttr = new RequestingUserName(\r |
110 | username, Locale.GERMANY);\r |
111 | RequestingUserPassword userpassAttr = new RequestingUserPassword(\r |
112 | password, Locale.GERMANY);\r |
113 | attributes.add(usernameAttr);\r |
114 | attributes.add(userpassAttr);\r |
115 | }\r |
116 | // we just want one copy\r |
117 | Copies copies = new Copies(1);\r |
118 | attributes.add(copies);\r |
119 | // we want duplex printing\r |
120 | Sides sides = Sides.DUPLEX;\r |
121 | attributes.add(sides);\r |
122 | // print it on the main media tray\r |
123 | // Media media = MediaTray.MAIN;\r |
124 | // If you have special Mediatrays (like most printers)\r |
125 | // you can use the class LdMediaTray and give a String to the\r |
126 | // constructor like\r |
127 | // new LdMediaTray("Optional2");\r |
128 | // attributes.add(media);\r |
129 | \r |
130 | // Now create a document\r |
131 | File testFile = new File(Messages.getString("pdfdocument.1"));\r |
132 | InputStream stream = new FileInputStream(testFile);\r |
133 | Doc doc = new SimpleDoc(stream, DocFlavor.INPUT_STREAM.PDF,\r |
134 | new HashDocAttributeSet());\r |
135 | \r |
136 | // finally the print action\r |
137 | try {\r |
138 | // we are setting the doc and the job attributes\r |
139 | job.print(doc, attributes);\r |
140 | System.out.println("printing successfull...");\r |
141 | } catch (PrintException e) {\r |
142 | e.printStackTrace();\r |
143 | }\r |
144 | \r |
145 | } else {\r |
146 | System.out.println("no Services found!");\r |
147 | }\r |
148 | }\r |
149 | \r |
150 | /**\r |
151 | * @return\r |
152 | */\r |
153 | private static String readStdIn() {\r |
154 | StringBuffer sb = new StringBuffer();\r |
155 | int ch;\r |
156 | try {\r |
157 | while ((ch = System.in.read()) != 10) {\r |
158 | sb.append((char) ch);\r |
159 | }\r |
160 | } catch (IOException e) {\r |
161 | e.printStackTrace();\r |
162 | }\r |
163 | return sb.toString();\r |
164 | }\r |
165 | } |