Initial import.
[clinton/mirror/jspi/.git] / jspi / src / main / java / de / lohndirekt / print / examples / MultiDocExample.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 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileNotFoundException;
23 import java.io.InputStream;
24 import java.net.MalformedURLException;
25 import java.net.URISyntaxException;
26
27 import javax.print.Doc;
28 import javax.print.DocFlavor;
29 import javax.print.MultiDocPrintJob;
30 import javax.print.MultiDocPrintService;
31 import javax.print.PrintException;
32 import javax.print.SimpleDoc;
33 import javax.print.attribute.HashDocAttributeSet;
34 import javax.print.attribute.HashPrintRequestAttributeSet;
35 import javax.print.attribute.PrintRequestAttributeSet;
36 import javax.print.attribute.standard.Copies;
37 import javax.print.attribute.standard.Media;
38 import javax.print.attribute.standard.MediaTray;
39 import javax.print.attribute.standard.Sides;
40
41 import de.lohndirekt.print.IppPrintServiceLookup;
42 import de.lohndirekt.print.SimpleMultiDoc;
43
44 public class MultiDocExample {
45
46 public static void main(String[] args)
47 throws URISyntaxException, FileNotFoundException, PrintException, MalformedURLException {
48 // setting cups properties
49 System.getProperties().setProperty(IppPrintServiceLookup.URI_KEY, Messages.getString("cups.uri")); //$NON-NLS-1$
50 System.getProperties().setProperty(IppPrintServiceLookup.USERNAME_KEY, Messages.getString("cups.username")); //$NON-NLS-1$
51 System.getProperties().setProperty(IppPrintServiceLookup.PASSWORD_KEY, Messages.getString("cups.password")); //$NON-NLS-1$
52
53 // get the PrintServices
54 MultiDocPrintService[] services = new IppPrintServiceLookup().getMultiDocPrintServices(null, null);
55
56 // get the first Printer
57 if (services.length > 0) {
58 MultiDocPrintService service = services[0];
59 System.out.println("Printing on: " + service.getName());
60 // create a job
61 MultiDocPrintJob job = service.createMultiDocPrintJob();
62
63 // set the job attributes
64 PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
65 // we just want one copy
66 Copies copies = new Copies(1);
67 attributes.add(copies);
68 // we want duplex printing
69 Sides sides = Sides.DUPLEX;
70 attributes.add(sides);
71 // print it on the main media tray
72 Media media = MediaTray.MAIN;
73 // If you have special Mediatrays (like most printers)
74 // you can use the class LdMediaTray and give a String to the constructor like
75 // new LdMediaTray("Optional2");
76 attributes.add(media);
77
78 // Now create some documents
79 File testFile = new File(Messages.getString("pdfdocument.1"));
80 InputStream stream = new FileInputStream(testFile);
81 Doc doc = new SimpleDoc(stream, DocFlavor.INPUT_STREAM.PDF, new HashDocAttributeSet());
82 File testFile2 = new File(Messages.getString("pdfdocument.2"));
83 InputStream stream2 = new FileInputStream(testFile2);
84 Doc doc2 = new SimpleDoc(stream2, DocFlavor.INPUT_STREAM.PDF, new HashDocAttributeSet());
85 // now we need a MultiDoc
86 SimpleMultiDoc multiDoc = SimpleMultiDoc.create(doc);
87 // finally the print action
88 try {
89 // we are setting the doc and the job attributes
90 job.print(multiDoc, attributes);
91
92 // now add several docs to your mutliDoc
93 // cups waits for the document with the attribute last-document = true
94 // wich you can set like this:
95 boolean lastDocument = true;
96 multiDoc.setNext(doc2, lastDocument);
97
98 System.out.println("printing successfull...");
99 } catch (PrintException e) {
100 e.printStackTrace();
101 }
102
103 } else {
104 System.out.println("no Services found!");
105 }
106 }
107
108 }