-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrint.java
More file actions
275 lines (207 loc) · 7.33 KB
/
Copy pathPrint.java
File metadata and controls
275 lines (207 loc) · 7.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Print.java
// Andrew Davison, ad@fivedots.coe.psu.ac.th, July 2016
/* A growing collection of utility functions to make Office
easier to use. They are currently divided into the following
groups:
* setup printer
* report printer props
* print a document
*/
import com.sun.star.beans.*;
import com.sun.star.lang.*;
import com.sun.star.uno.*;
import com.sun.star.awt.*;
import com.sun.star.document.*;
import com.sun.star.view.*;
// import com.sun.star.uno.Exception;
// import com.sun.star.io.IOException;
public class Print
{
public static void setDocListener(XPrintableBroadcaster pb)
// old style listener (?) from 2002
{
pb.addPrintableListener( new XPrintableListener()
{
public void stateChanged(PrintableStateEvent e)
{ System.out.println("Print state change: " + printableState(e.State)); }
public void disposing(com.sun.star.lang.EventObject e)
{ System.out.println("Disposing of print job: " + e); }
});
} // end of setDocListener()
// --------------- setup printer -------------------
public static void usePrinter(XPrintable xp, String printer)
{
if (xp == null) {
System.out.println("Cannot set printer XPrintable is null");
return;
}
System.out.println("Using printer \"" + printer + "\"");
xp.setPrinter( Props.makeProps("Name", printer,
"PaperFormat", PaperFormat.A4) );
setListener(xp);
}
public static void setListener(XPrintable xp)
{
if (xp == null) {
System.out.println("Cannot set listener; XPrintable is null");
return;
}
XPrintJobBroadcaster pb = Lo.qi(XPrintJobBroadcaster.class, xp);
if (pb == null) {
System.out.println("Cannot obtain print job broadcaster");
return;
}
pb.addPrintJobListener( new XPrintJobListener()
{
public void printJobEvent(PrintJobEvent e)
{ System.out.println("Print Job status: " + printableState(e.State)); }
public void disposing(com.sun.star.lang.EventObject e)
{ System.out.println("Disposing of print job: " + e); }
});
} // end of setListener()
public static String printableState(PrintableState val)
{
if (val == PrintableState.JOB_STARTED )
return "JOB_STARTED";
else if (val == PrintableState.JOB_COMPLETED )
return "JOB_COMPLETED";
else if (val == PrintableState.JOB_SPOOLED )
return "JOB_SPOOLED";
else if (val == PrintableState.JOB_ABORTED )
return "JOB_ABORTED";
else if (val == PrintableState.JOB_FAILED )
return "JOB_FAILED";
else if (val == PrintableState.JOB_SPOOLING_FAILED )
return "JOB_SPOOLING_FAILED";
else {
System.out.println("Unknown printable state");
return "??";
}
} // end of printableState()
public static XPropertySet getDocSettings(int docType)
{
XPropertySet props = null;
if (docType == Lo.WRITER)
props = Lo.createInstanceMSF(XPropertySet.class,
"com.sun.star.text.DocumentSettings");
else if (docType == Lo.IMPRESS)
props = Lo.createInstanceMSF(XPropertySet.class,
"com.sun.star.presentation.DocumentSettings");
else if (docType == Lo.DRAW)
props = Lo.createInstanceMSF(XPropertySet.class,
"com.sun.star.drawing.DocumentSettings");
else if (docType == Lo.CALC)
props = Lo.createInstanceMSF(XPropertySet.class,
"com.sun.star.sheet.DocumentSettings");
else if (docType == Lo.BASE)
System.out.println("No document settings for a base document");
else if (docType == Lo.MATH)
System.out.println("No document settings for a math document");
else
System.out.println("Unknown document type");
return props;
} // end of getDocSettings()
// ------------------- report printer props -------------------
public static void reportPrinterProps(XPrintable xp)
{
if (xp == null) {
System.out.println("Cannot report printer props; XPrintable is null");
return;
}
PropertyValue[] printProps = xp.getPrinter();
if (printProps == null)
System.out.println("No Printer properties found");
else {
System.out.println("Printer properties:");
String name;
for (PropertyValue prop : printProps) {
name = prop.Name;
if (name.equals("PaperOrientation"))
System.out.println(" " + name + ": " +
paperOrientation((PaperOrientation)prop.Value));
else if (name.equals("PaperFormat"))
System.out.println(" " + name + ": " +
paperFormat((PaperFormat)prop.Value));
else if (name.equals("PaperSize")) {
Size sz = (Size)prop.Value;
System.out.println(" " + name + ": (" + sz.Width + ", " + sz.Height + ")");
}
else
System.out.println(" " + name + ": " + prop.Value);
}
System.out.println();
}
} // end of reportPrinterProps()
public static String paperOrientation(PaperOrientation val)
{
if (val == PaperOrientation.PORTRAIT)
return "PORTRAIT";
else if (val == PaperOrientation.LANDSCAPE)
return "LANDSCAPE";
else {
System.out.println("Unknown paper orientation");
return "??";
}
} // end of paperOrientation()
public static String paperFormat(PaperFormat val)
{
if (val == PaperFormat.A3)
return "A3";
else if (val == PaperFormat.A4)
return "A4";
else if (val == PaperFormat.A5)
return "A5";
else if (val == PaperFormat.B4)
return "B4";
else if (val == PaperFormat.B5)
return "B5";
else if (val == PaperFormat.LETTER)
return "LETTER";
else if (val == PaperFormat.LEGAL)
return "LEGAL";
else if (val == PaperFormat.TABLOID)
return "TABLOID";
else if (val == PaperFormat.USER)
return "USER";
else {
System.out.println("Unknown paper format");
return "??";
}
} // end of paperFormat()
public static String selectionType(SelectionType val)
{
if (val == SelectionType.NONE )
return "NONE";
else if (val == SelectionType.SINGLE )
return "SINGLE";
else if (val == SelectionType.MULTI )
return "MULTI";
else if (val == SelectionType.RANGE )
return "RANGE";
else {
System.out.println("Unknown selection type");
return "??";
}
} // end of selectionType()
// --------------- print a document -------------------------
public static boolean isPrintable(int docType)
{ return ((docType == Lo.WRITER) || (docType == Lo.CALC) ||
(docType == Lo.DRAW) || (docType == Lo.IMPRESS)); }
public static void print(XPrintable xp)
{ print(xp, "1-"); }
public static void print(XPrintable xp, String pagesStr)
{
if (xp == null) {
System.out.println("Cannot print; XPrintable is null");
return;
}
// set the desired pages that will be printed (e.g. "1, 3, 4-7, 9-")
System.out.println("Print range: " + pagesStr);
System.out.println("Sending document...");
PropertyValue[] props = Props.makeProps("Pages", pagesStr,
"Wait", true); // synchronous
// see com.sun.star.view.PrintOptions
xp.print(props); // print the document
System.out.println("Delivered");
} // end of print()
} // end of Print class