/
Report.cs
419 lines (389 loc) · 12.3 KB
/
Report.cs
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using System.Web.Services.Protocols;
namespace Kesco.Lib.Win.PrintReport
{
public class Report : IReport
{
public event PrintEventHandler EndPrint;
private RS.ReportingService rs;
private byte[][] m_renderedReport;
private Graphics.EnumerateMetafileProc m_delegate;
private MemoryStream m_currentPageStream;
private Metafile m_metafile;
private int m_numberOfPages;
private bool landscape;
private int m_currentPrintingPage;
private int m_lastPrintingPage;
private int pheight;
private int pwidth;
private Margins ma;
public Report(string url)
{
// Create proxy object and authenticate
Console.WriteLine("{0}: Authenticating to the Web service...", DateTime.Now.ToString("HH:mm:ss fff"));
try
{
rs = new RS.ReportingService(url);
rs.Credentials = CredentialCache.DefaultCredentials;
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public byte[][] RenderReport(string reportPath, int id, short paperSize)
{
return RenderReport(reportPath, id, "emf", paperSize, false);
}
public byte[][] RenderReport(string reportPath, int id, string docType, short paperSize, bool save)
{
// Private variables for rendering
Console.WriteLine("{0}: Start render", DateTime.Now.ToString("HH:mm:ss fff"));
string deviceInfo = null;
const string format = "IMAGE";
Byte[] firstPage = null;
string encoding;
string mimeType;
RS.Warning[] warnings = null;
RS.ParameterValue[] rp = new RS.ParameterValue[2];
rp.SetValue(new RS.ParameterValue { Name = "id", Value = id.ToString() }, 0);
rp.SetValue(new RS.ParameterValue { Name = "DT", Value = DateTime.Now.ToString() }, 1);
RS.ParameterValue[] reportHistoryParameters = null;
string[] streamIDs = null;
Byte[][] pages = null;
// Build device info based on the start page
deviceInfo = String.Format(@"<DeviceInfo><OutputFormat>{0}</OutputFormat>", docType);
if(save)
deviceInfo += @"<DpiX>300</DpiX><DpiY>300</DpiY>";
rs.ItemNamespaceHeaderValue = new RS.ItemNamespaceHeader();
rs.ItemNamespaceHeaderValue.ItemNamespace = RS.ItemNamespaceEnum.PathBased;
Console.WriteLine("{0}: Get properties", DateTime.Now.ToString("HH:mm:ss fff"));
RS.Property[] properties = rs.GetProperties(reportPath, null);
foreach(RS.Property property in properties)
{
switch(property.Name.ToLower())
{
case "pageheight":
case "pagewidth":
case "topmargin":
case "bottommargin":
case "rightmargin":
case "leftmargin":
deviceInfo +=
String.Format(@"<{0}>{1}mm</{0}>", property.Name, property.Value);
break;
}
}
deviceInfo += "</DeviceInfo>";
Console.WriteLine("{0}: Get first page", DateTime.Now.ToString("HH:mm:ss fff"));
//Exectute the report and get page count.
try
{
// Renders the first page of the report and returns streamIDs for
// subsequent pages
firstPage = rs.Render(
reportPath,
format,
null,
deviceInfo,
rp,
null,
null,
out encoding,
out mimeType,
out reportHistoryParameters,
out warnings,
out streamIDs);
// The total number of pages of the report is 1 + the streamIDs
m_numberOfPages = streamIDs.Length + 1;
pages = new Byte[m_numberOfPages][];
Console.WriteLine("{0}: mast be pages: {1}", DateTime.Now.ToString("HH:mm:ss fff"), m_numberOfPages);
// The first page was already rendered
pages[0] = firstPage;
for(int pageIndex = 1; pageIndex < m_numberOfPages; pageIndex++)
{
// Build device info based on start page
deviceInfo = String.Format(@"<DeviceInfo><OutputFormat>{0}</OutputFormat><StartPage>{1}</StartPage></DeviceInfo>",
docType, pageIndex + 1);
pages[pageIndex] = rs.Render(
reportPath,
format,
null,
deviceInfo,
rp,
null,
null,
out encoding,
out mimeType,
out reportHistoryParameters,
out warnings,
out streamIDs);
}
}
catch(SoapException ex)
{
Kesco.Lib.Log.Logger.WriteEx(new Kesco.Lib.Log.DetailedException(reportPath, ex, id.ToString(), true));
Console.WriteLine(ex.Detail.InnerXml);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if(pages != null)
Console.WriteLine("{0}: Number of pages: {1}", DateTime.Now.ToString("HH:mm:ss fff"), pages.Length);
}
return pages;
}
public bool PrintReport(string printerName, string reportPath, int id, int printID, short paperSize, short copiesCount)
{
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
this.RenderedReport = this.RenderReport(reportPath, id, paperSize);
if(null == this.RenderedReport)
return false;
try
{
// Wait for the report to completely render.
if(m_numberOfPages < 1)
return false;
PrintDocument pd = new PrintDocument();
rs.ItemNamespaceHeaderValue = new RS.ItemNamespaceHeader();
rs.ItemNamespaceHeaderValue.ItemNamespace = RS.ItemNamespaceEnum.PathBased;
RS.Property[] properties = rs.GetProperties(reportPath, null);
pheight = pd.DefaultPageSettings.PaperSize.Height;
pwidth = pd.DefaultPageSettings.PaperSize.Width;
double theight = 0;
double twidth = 0;
ma = new Margins(0, 0, 0, 0);
bool size = false;
foreach(RS.Property property in properties)
{
switch(property.Name.ToLower())
{
case "pageheight":
theight = (double.Parse(property.Value) / 0.254);
pheight = (int)System.Math.Round(theight);
size = true;
break;
case "pagewidth":
twidth = double.Parse(property.Value) / 0.254;
pwidth = (int)System.Math.Round(twidth);
size = true;
break;
case "topmargin":
ma.Top = (int)(double.Parse(property.Value) / 0.254);
break;
case "bottommargin":
ma.Bottom = (int)(double.Parse(property.Value) / 0.254);
break;
case "rightmargin":
ma.Right = (int)(double.Parse(property.Value) / 0.254);
break;
case "leftmargin":
ma.Left = (int)(double.Parse(property.Value) / 0.254);
break;
}
//Console.WriteLine(property.Name + ": " + property.Value);
}
Console.WriteLine("{0}: paper change", DateTime.Now.ToString("HH:mm:ss fff"));
if(!size)
{
if(this.m_currentPageStream != null)
{
this.m_currentPageStream.Close();
this.m_currentPageStream = null;
}
m_currentPageStream = new MemoryStream(this.m_renderedReport[0]);
// Set its postion to start.
m_currentPageStream.Position = 0;
// Initialize the metafile
if(null != m_metafile)
{
m_metafile.Dispose();
m_metafile = null;
}
// Load the metafile image for this page
m_metafile = new Metafile((Stream)m_currentPageStream);
pheight = m_metafile.Height;
pwidth = m_metafile.Width;
}
landscape = false;
if(pwidth > pheight && !pd.DefaultPageSettings.Landscape)
landscape = true;
PrinterSettings printerSettings = new PrinterSettings();
printerSettings.MaximumPage = m_numberOfPages;
printerSettings.MinimumPage = 1;
printerSettings.PrintRange = PrintRange.SomePages;
printerSettings.FromPage = 1;
printerSettings.ToPage = m_numberOfPages;
printerSettings.Copies = copiesCount;
m_currentPrintingPage = 1;
m_lastPrintingPage = m_numberOfPages;
printerSettings.PrinterName = printerName;
pd.PrinterSettings = printerSettings;
if(landscape)
{
if(pd.DefaultPageSettings.PaperSize.Width != pheight || pd.DefaultPageSettings.PaperSize.Height != pwidth)
{
PaperSize papers = new PaperSize(reportPath, pheight, pwidth);
papers.PaperName = "ReportPrintingLandscape";
pd.DefaultPageSettings.PaperSize = papers;
}
pd.DefaultPageSettings.Landscape = true;
}
else
{
if(pd.DefaultPageSettings.PaperSize.Width != pwidth || pd.DefaultPageSettings.PaperSize.Height != pheight)
{
PaperSize papers = new PaperSize(reportPath, pwidth, pheight);
papers.PaperName = "ReportPrinting";
pd.DefaultPageSettings.PaperSize = papers;
}
pd.DefaultPageSettings.Landscape = false;
}
pd.OriginAtMargins = true;
pd.DefaultPageSettings.Margins = ma;
pd.PrintPage += pd_PrintPage;
pd.DocumentName = "?docviewprint=" + id.ToString() + "&docviewtypeid=" + printID.ToString() + "&id=" + id.ToString();
pd.EndPrint += pd_EndPrint;
// Print report
Console.WriteLine("{0}: Printing report...", DateTime.Now.ToString("HH:mm:ss fff"));
if(pd.PrinterSettings.IsValid)
pd.Print();
else
Console.WriteLine("{0}: Encorrect parameters", DateTime.Now.ToString("HH:mm:ss fff"));
pd.Dispose();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
// Clean up goes here.
}
return true;
}
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
ev.HasMorePages = false;
if(m_currentPrintingPage > m_lastPrintingPage || !MoveToPage(m_currentPrintingPage, ev.Graphics))
return;
ReportDrawPage(ev.Graphics);
// If the next page is less than or equal to the last page,
// print another page.
if(++m_currentPrintingPage <= m_lastPrintingPage)
ev.HasMorePages = true;
else
{
if(null != this.m_renderedReport)
{
for(int i = 0; i < this.m_renderedReport.Length; ++i)
{
this.m_renderedReport[i] = null;
}
this.m_renderedReport = null;
if(this.m_metafile != null)
{
this.m_metafile.Dispose();
this.m_metafile = null;
}
if(this.m_currentPageStream != null)
{
this.m_currentPageStream.Close();
this.m_currentPageStream = null;
}
}
}
}
// Method to draw the current emf memory stream
private void ReportDrawPage(Graphics g)
{
if(null == m_currentPageStream || 0 == m_currentPageStream.Length || null == m_metafile)
return;
lock(this)
{
// Set the metafile delegate.
int width = m_metafile.Width;
int height = m_metafile.Height;
m_delegate = MetafileCallback;
// Draw in the rectangle
Point[] points = new Point[3];
Point destPoint = new Point(0, 0);
Point destPoint1 = new Point(width, 0);
Point destPoint2 = new Point(0, height);
points[0] = destPoint;
points[1] = destPoint1;
points[2] = destPoint2;
g.EnumerateMetafile(m_metafile, points, m_delegate);
points = null;
// Clean up
m_delegate = null;
}
}
private bool MoveToPage(Int32 page, Graphics g)
{
// Check to make sure that the current page exists in
// the array list
if(null == RenderedReport[m_currentPrintingPage - 1])
return false;
// Set current page stream equal to the rendered page
if(m_currentPageStream != null)
{
m_currentPageStream.Close();
m_currentPageStream = null;
}
m_currentPageStream = new System.IO.MemoryStream(this.RenderedReport[m_currentPrintingPage - 1]);
// Set its postion to start.
m_currentPageStream.Position = 0;
// Initialize the metafile
if(null != m_metafile)
{
m_metafile.Dispose();
m_metafile = null;
}
// Load the metafile image for this page
m_metafile = new Metafile(m_currentPageStream);
return true;
}
private bool MetafileCallback(EmfPlusRecordType recordType, int flags, int dataSize, IntPtr data, PlayRecordCallback callbackData)
{
byte[] dataArray = null;
// Dance around unmanaged code.
if(data != IntPtr.Zero)
{
// Copy the unmanaged record to a managed byte buffer
// that can be used by PlayRecord.
dataArray = new byte[dataSize];
Marshal.Copy(data, dataArray, 0, dataSize);
}
// play the record.
m_metafile.PlayRecord(recordType, flags, dataSize, dataArray);
return true;
}
public byte[][] RenderedReport
{
get
{
return m_renderedReport;
}
private set
{
m_renderedReport = value;
}
}
private void pd_EndPrint(object sender, PrintEventArgs e)
{
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
if(this.EndPrint != null)
EndPrint.BeginInvoke(sender, e, null, null);
}
}
}