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