1
+ <% @ WebHandler Language= " C#" Class = " DemoPrintCommandsHandler" %>
2
+
3
+ using System;
4
+ using System.Web;
5
+
6
+ using Neodynamic.SDK.Web;
7
+
8
+ public class DemoPrintCommandsHandler : IHttpHandler {
9
+
10
+
11
+ /*############### IMPORTANT!!! ############
12
+ If your website requires AUTHENTICATION, then you MUST configure THIS Handler file
13
+ to be ANONYMOUS access allowed!!!
14
+ ######################################### */
15
+
16
+
17
+ const string PRINTER_ID = "-PID";
18
+ const string INSTALLED_PRINTER_NAME = "-InstalledPrinterName";
19
+ const string NET_PRINTER_HOST = "-NetPrinterHost";
20
+ const string NET_PRINTER_PORT = "-NetPrinterPort";
21
+ const string PARALLEL_PORT = "-ParallelPort";
22
+ const string SERIAL_PORT = "-SerialPort";
23
+ const string SERIAL_PORT_BAUDS = "-SerialPortBauds";
24
+ const string SERIAL_PORT_DATA_BITS = "-SerialPortDataBits";
25
+ const string SERIAL_PORT_STOP_BITS = "-SerialPortStopBits";
26
+ const string SERIAL_PORT_PARITY = "-SerialPortParity";
27
+ const string SERIAL_PORT_FLOW_CONTROL = "-SerialPortFlowControl";
28
+ const string PRINTER_COMMANDS = "-PrinterCommands";
29
+
30
+
31
+ public void ProcessRequest(HttpContext context)
32
+ {
33
+
34
+ string pid = context.Request["pid"];
35
+ string sid = context.Request["sid"];
36
+
37
+ if (string.IsNullOrEmpty(pid) == false)
38
+ {
39
+ //store client printer settings
40
+ string installedPrinterName = context.Request["installedPrinterName"];
41
+ string netPrinterHost = context.Request["netPrinterHost"];
42
+ string netPrinterPort = context.Request["netPrinterPort"];
43
+ string parallelPort = context.Request["parallelPort"];
44
+ string serialPort = context.Request["serialPort"];
45
+ string serialPortBauds = context.Request["serialPortBauds"];
46
+ string serialPortDataBits = context.Request["serialPortDataBits"];
47
+ string serialPortStopBits = context.Request["serialPortStopBits"];
48
+ string serialPortParity = context.Request["serialPortParity"];
49
+ string serialPortFlowControl = context.Request["serialPortFlowControl"];
50
+ string printerCommands = context.Request["printerCommands"];
51
+
52
+ ClientPrinterSettings(sid,
53
+ pid,
54
+ installedPrinterName,
55
+ netPrinterHost,
56
+ netPrinterPort,
57
+ parallelPort,
58
+ serialPort,
59
+ serialPortBauds,
60
+ serialPortDataBits,
61
+ serialPortStopBits,
62
+ serialPortParity,
63
+ serialPortFlowControl,
64
+ printerCommands);
65
+ }
66
+ else
67
+ {
68
+ //generate client print job
69
+ ClientPrint(sid);
70
+ }
71
+
72
+ }
73
+
74
+ private void ClientPrinterSettings(string sid,
75
+ string pid,
76
+ string installedPrinterName,
77
+ string netPrinterHost,
78
+ string netPrinterPort,
79
+ string parallelPort,
80
+ string serialPort,
81
+ string serialPortBauds,
82
+ string serialPortDataBits,
83
+ string serialPortStopBits,
84
+ string serialPortParity,
85
+ string serialPortFlowControl,
86
+ string printerCommands)
87
+ {
88
+ try
89
+ {
90
+ HttpApplicationState app = HttpContext.Current.Application;
91
+
92
+ //save settings in the global Application obj
93
+
94
+ //save the type of printer selected by the user
95
+ int i = int.Parse(pid);
96
+ app[sid + PRINTER_ID] = i;
97
+
98
+ if (i == 2)
99
+ {
100
+ app[sid + INSTALLED_PRINTER_NAME] = installedPrinterName;
101
+ }
102
+ else if (i == 3)
103
+ {
104
+ app[sid + NET_PRINTER_HOST] = netPrinterHost;
105
+ app[sid + NET_PRINTER_PORT] = netPrinterPort;
106
+ }
107
+ else if (i == 4)
108
+ {
109
+ app[sid + PARALLEL_PORT] = parallelPort;
110
+ }
111
+ else if (i == 5)
112
+ {
113
+ app[sid + SERIAL_PORT] = serialPort;
114
+ app[sid + SERIAL_PORT_BAUDS] = serialPortBauds;
115
+ app[sid + SERIAL_PORT_DATA_BITS] = serialPortDataBits;
116
+ app[sid + SERIAL_PORT_FLOW_CONTROL] = serialPortFlowControl;
117
+ app[sid + SERIAL_PORT_PARITY] = serialPortParity;
118
+ app[sid + SERIAL_PORT_STOP_BITS] = serialPortStopBits;
119
+ }
120
+
121
+ //save the printer commands specified by the user
122
+ app[sid + PRINTER_COMMANDS] = printerCommands;
123
+ }
124
+ catch (Exception ex)
125
+ {
126
+ throw ex;
127
+ }
128
+ }
129
+
130
+
131
+ //sid: user session id who is requesting a ClientPrintJob
132
+ private void ClientPrint(string sid)
133
+ {
134
+ if (WebClientPrint.ProcessPrintJob(System.Web.HttpContext.Current.Request.Url.Query))
135
+ {
136
+ HttpApplicationState app = HttpContext.Current.Application;
137
+
138
+ //Create a ClientPrintJob obj that will be processed at the client side by the WCPP
139
+ ClientPrintJob cpj = new ClientPrintJob();
140
+
141
+ //get printer commands for this user id
142
+ object printerCommands = app[sid + PRINTER_COMMANDS];
143
+ if (printerCommands != null)
144
+ {
145
+ cpj.PrinterCommands = printerCommands.ToString();
146
+ cpj.FormatHexValues = true;
147
+ }
148
+
149
+ //get printer settings for this user id
150
+ int printerTypeId = (int)app[sid + PRINTER_ID];
151
+
152
+ if (printerTypeId == 0) //use default printer
153
+ {
154
+ cpj.ClientPrinter = new DefaultPrinter();
155
+ }
156
+ else if (printerTypeId == 1) //show print dialog
157
+ {
158
+ cpj.ClientPrinter = new UserSelectedPrinter();
159
+ }
160
+ else if (printerTypeId == 2) //use specified installed printer
161
+ {
162
+ cpj.ClientPrinter = new InstalledPrinter(app[sid + INSTALLED_PRINTER_NAME].ToString());
163
+ }
164
+ else if (printerTypeId == 3) //use IP-Ethernet printer
165
+ {
166
+ cpj.ClientPrinter = new NetworkPrinter(app[sid + NET_PRINTER_HOST].ToString(), int.Parse(app[sid + NET_PRINTER_PORT].ToString()));
167
+ }
168
+ else if (printerTypeId == 4) //use Parallel Port printer
169
+ {
170
+ cpj.ClientPrinter = new ParallelPortPrinter(app[sid + PARALLEL_PORT].ToString());
171
+ }
172
+ else if (printerTypeId == 5) //use Serial Port printer
173
+ {
174
+ cpj.ClientPrinter = new SerialPortPrinter(app[sid + SERIAL_PORT].ToString(),
175
+ int.Parse(app[sid + SERIAL_PORT_BAUDS].ToString()),
176
+ (SerialPortParity)Enum.Parse(typeof(SerialPortParity), app[sid + SERIAL_PORT_PARITY].ToString()),
177
+ (SerialPortStopBits)Enum.Parse(typeof(SerialPortStopBits), app[sid + SERIAL_PORT_STOP_BITS].ToString()),
178
+ int.Parse(app[sid + SERIAL_PORT_DATA_BITS].ToString()),
179
+ (SerialPortHandshake)Enum.Parse(typeof(SerialPortHandshake), app[sid + SERIAL_PORT_FLOW_CONTROL].ToString()));
180
+ }
181
+
182
+ //Send ClientPrintJob back to the client
183
+ System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
184
+ System.Web.HttpContext.Current.Response.BinaryWrite(cpj.GetContent());
185
+ System.Web.HttpContext.Current.Response.End();
186
+
187
+ }
188
+
189
+ }
190
+
191
+ public bool IsReusable {
192
+ get {
193
+ return false;
194
+ }
195
+ }
196
+
197
+ }
0 commit comments