2 * Copyright (C) 2003 <a href="http://www.lohndirekt.de/">lohndirekt.de</a>
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.
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.
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
19 package de
.lohndirekt
.print
.examples
;
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
;
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
;
41 import de
.lohndirekt
.print
.IppPrintServiceLookup
;
42 import de
.lohndirekt
.print
.attribute
.auth
.RequestingUserPassword
;
49 public class SimpleDocExample
{
53 * @throws URISyntaxException
54 * @throws PrintException
57 public static void main(String
[] args
) throws URISyntaxException
,
58 PrintException
, IOException
{
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$
68 // get the PrintServices
69 PrintService
[] services
= new IppPrintServiceLookup().getPrintServices();
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());
79 // let the user choose a service
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.");
86 service
= services
[serviceToUse
];
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();
100 System
.out
.println("Printing on: " + service
.getName());
102 CancelablePrintJob job
= (CancelablePrintJob
) service
105 // set the job attributes
106 PrintRequestAttributeSet attributes
= new HashPrintRequestAttributeSet();
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
);
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
127 // new LdMediaTray("Optional2");
128 // attributes.add(media);
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());
136 // finally the print action
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
) {
146 System
.out
.println("no Services found!");
153 private static String
readStdIn() {
154 StringBuffer sb
= new StringBuffer();
157 while ((ch
= System
.in
.read()) != 10) {
158 sb
.append((char) ch
);
160 } catch (IOException e
) {
163 return sb
.toString();