-
Notifications
You must be signed in to change notification settings - Fork 617
/
Copy pathPreparedPage.cs
503 lines (438 loc) · 16.2 KB
/
PreparedPage.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
using FastReport.Engine;
using FastReport.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace FastReport.Preview
{
internal partial class PreparedPage : IDisposable
{
#region Fields
private XmlItem xmlItem;
private PreparedPages preparedPages;
private SizeF pageSize;
private long tempFilePosition;
private bool uploaded;
private PreparedPagePostprocessor postprocessor;
#endregion Fields
#region Properties
private Report Report
{
get { return preparedPages.Report; }
}
private bool UseFileCache
{
get { return Report.UseFileCache; }
}
public XmlItem Xml
{
get { return xmlItem; }
set
{
xmlItem = value;
value.Parent = null;
}
}
public SizeF PageSize
{
get
{
if (pageSize.IsEmpty)
{
ReCalcSizes();
}
return pageSize;
}
set
{
pageSize = value;
}
}
public int CurPosition
{
get { return xmlItem.Count; }
}
#endregion Properties
#region Private Methods
private bool DoAdd(Base c, XmlItem item)
{
if (c == null)
return false;
ReportEngine engine = Report.Engine;
bool isRunning = Report.IsRunning && engine != null;
if (c is ReportComponentBase)
{
if (isRunning && !engine.CanPrint(c as ReportComponentBase))
return false;
}
item = item.Add();
using (FRWriter writer = new FRWriter(item))
{
writer.SerializeTo = SerializeTo.Preview;
writer.SaveChildren = false;
writer.BlobStore = preparedPages.BlobStore;
writer.Write(c);
}
if (isRunning)
engine.AddObjectToProcess(c, item);
if ((c.Flags & Flags.CanWriteChildren) == 0)
{
ObjectCollection childObjects = c.ChildObjects;
foreach (Base obj in childObjects)
{
DoAdd(obj, item);
}
}
return true;
}
private Base ReadObject(Base parent, XmlItem item, bool readChildren, FRReader reader)
{
string objName = item.Name;
// try to find the object in the dictionary
Base obj = preparedPages.Dictionary.GetObject(objName);
// object not found, objName is type name
if (obj == null)
{
Type type = RegisteredObjects.FindType(objName);
if (type == null)
return null;
obj = Activator.CreateInstance(type) as Base;
}
obj.SetRunning(true);
// read object's properties
if (!item.IsNullOrEmptyProps())
{
// since the BlobStore is shared resource, lock it to avoid problems with multi-thread access.
// this may happen in the html export that uses several threads to export one report.
lock (reader.BlobStore)
{
reader.Read(obj, item);
}
if (obj is TextObject txt)
ProcessText(txt);
}
if (readChildren)
{
for (int i = 0; i < item.Count; i++)
{
ReadObject(obj, item[i], true, reader);
}
}
obj.Parent = parent;
return obj;
}
private void UpdateUnlimitedPage(Base obj, XmlItem item)
{
item.Clear();
using (FRWriter writer = new FRWriter(item))
{
writer.SerializeTo = SerializeTo.Preview;
writer.SaveChildren = false;
writer.BlobStore = preparedPages.BlobStore;
writer.Write(obj);
}
foreach (Base child in obj.ChildObjects)
{
UpdateUnlimitedPage(child, item.Add());
}
}
#endregion Private Methods
#region Internal Methods
internal ReportPage ReadPage(Base parent, XmlItem item, bool readchild, FRReader reader)
{
ReportPage page = ReadObject(parent, item, false, reader) as ReportPage;
if (readchild)
for (int i = 0; i < item.Count; i++)
{
ReadObject(page, item[i], true, reader);
}
return page;
}
internal ReportPage StartGetPage(int index)
{
Load();
ReportPage page;
using (FRReader reader = new FRReader(null))
{
reader.DeserializeFrom = SerializeTo.Preview;
reader.ReadChildren = false;
reader.BlobStore = preparedPages.BlobStore;
page = ReadPage(null, xmlItem, false, reader);
if (!(page.UnlimitedHeight || page.UnlimitedWidth))
{
page.Dispose();
page = ReadPage(null, xmlItem, true, reader);
page.SetReport(preparedPages.Report);
postprocessor = new PreparedPagePostprocessor();
postprocessor.Postprocess(page);
postprocessor = null;
}
else
{
page.SetReport(preparedPages.Report);
postprocessor = new PreparedPagePostprocessor();
postprocessor.PostprocessUnlimited(this, page);
}
}
if (page.MirrorMargins && (index + 1) % 2 == 0)
{
float f = page.LeftMargin;
page.LeftMargin = page.RightMargin;
page.RightMargin = f;
}
return page;
}
internal void EndGetPage(ReportPage page)
{
if (postprocessor != null) postprocessor = null;
if (page != null)
page.Dispose();
ClearUploadedXml();
}
internal IEnumerable<Base> GetPageItems(ReportPage page, bool postprocess)
{
if (!(page.UnlimitedHeight || page.UnlimitedWidth))
{
foreach (Base child in page.ChildObjects)
{
if (postprocess) yield return child;
else
using (child)
yield return child;
}
}
else
{
if (Export.ExportBase.HAVE_TO_WORK_WITH_OVERLAY)
#pragma warning disable CS0162 // Unreachable code detected
foreach (Base child in page.ChildObjects)
#pragma warning restore CS0162 // Unreachable code detected
{
if (child is OverlayBand)
yield return child;
}
using (FRReader reader = new FRReader(null))
{
reader.DeserializeFrom = SerializeTo.Preview;
reader.ReadChildren = false;
reader.BlobStore = preparedPages.BlobStore;
for (int i = 0; i < xmlItem.Count; i++)
{
if (postprocess) yield return ReadObject(page, xmlItem[i], true, reader);
else
using (Base obj = ReadObject(page, xmlItem[i], true, reader))
{
using (Base obj2 = postprocessor.PostProcessBandUnlimitedPage(obj))
yield return obj2;
}
}
}
}
}
internal string GetName()
{
using (FRReader reader = new FRReader(null))
{
reader.DeserializeFrom = SerializeTo.Preview;
reader.ReadChildren = false;
reader.BlobStore = preparedPages.BlobStore;
ReportPage page = ReadObject(null, xmlItem, false, reader) as ReportPage;
return page.Name;
}
}
internal void ReCalcSizes()
{
XmlItem item = xmlItem;
using (FRReader reader = new FRReader(null, item))
{
reader.DeserializeFrom = SerializeTo.Preview;
reader.BlobStore = preparedPages.BlobStore;
reader.ReadChildren = false;
using (ReportPage page = ReadPage(Report, item, false, reader))
{
if (page.UnlimitedHeight | page.UnlimitedWidth)
{
float maxWidth = 0.0f;
float maxHeight = 0.0f;
for (int i = 0; i < item.Count; i++)
{
using (Base obj = ReadObject(page, item[i], true, reader))
{
if (obj is BandBase)
{
BandBase band = obj as BandBase;
float bandsHeight = band.Top + band.Height;
if (maxHeight < bandsHeight)
maxHeight = bandsHeight;
float bandWidth = 0.0f;
foreach (ComponentBase comp in band.Objects)
{
if ((comp.Anchor & AnchorStyles.Right) == 0 && comp.Dock == DockStyle.None)
{
bandWidth = Math.Max(bandWidth, comp.Left + comp.Width);
}
}
if (maxWidth < bandWidth)
maxWidth = bandWidth;
}
}
}
if (page.UnlimitedHeight)
page.UnlimitedHeightValue = maxHeight + (page.TopMargin + page.BottomMargin) * Units.Millimeters;
if (page.UnlimitedWidth)
page.UnlimitedWidthValue = maxWidth + (page.LeftMargin + page.RightMargin) * Units.Millimeters;
}
pageSize = new SizeF(page.WidthInPixels, page.HeightInPixels);
using (FRWriter writer = new FRWriter(item))
{
writer.SerializeTo = SerializeTo.Preview;
writer.SaveChildren = false;
writer.BlobStore = preparedPages.BlobStore;
writer.Write(page);
}
}
}
}
#endregion Internal Methods
#region Public Methods
public PreparedPage(ReportPage page, PreparedPages preparedPages)
{
this.preparedPages = preparedPages;
xmlItem = new XmlItem();
// page == null when we load prepared report from a file
if (page != null)
{
using (FRWriter writer = new FRWriter(xmlItem))
{
writer.SerializeTo = SerializeTo.Preview;
writer.SaveChildren = false;
writer.Write(page);
}
pageSize = new SizeF(page.WidthInPixels, page.HeightInPixels);
}
}
public bool AddBand(BandBase band)
{
return DoAdd(band, xmlItem);
}
public ReportPage GetPage()
{
Load();
ReportPage page;
using (FRReader reader = new FRReader(null))
{
reader.DeserializeFrom = SerializeTo.Preview;
reader.ReadChildren = false;
reader.BlobStore = preparedPages.BlobStore;
page = ReadPage(null, xmlItem, true, reader);
}
page.SetReport(preparedPages.Report);
new PreparedPagePostprocessor().Postprocess(page);
ClearUploadedXml();
return page;
}
public void Load()
{
if (UseFileCache && uploaded)
{
preparedPages.TempFile.Position = tempFilePosition;
XmlReader reader = new XmlReader(preparedPages.TempFile);
reader.Read(xmlItem);
}
}
public void ClearUploadedXml()
{
if (UseFileCache && uploaded)
xmlItem.Clear();
}
public void Upload()
{
if (UseFileCache && !uploaded)
{
preparedPages.TempFile.Seek(0, SeekOrigin.End);
tempFilePosition = preparedPages.TempFile.Position;
XmlWriter writer = new XmlWriter(preparedPages.TempFile);
writer.Write(xmlItem);
xmlItem.Clear();
uploaded = true;
}
}
public XmlItem CutObjects(int index)
{
XmlItem result = new XmlItem();
while (xmlItem.Count > index)
{
result.AddItem(xmlItem[index]);
}
return result;
}
public void PasteObjects(XmlItem objects, float deltaX, float deltaY)
{
if (objects.Count > 0)
{
while (objects.Count > 0)
{
XmlItem obj = objects[0];
// shift the object's location
float objX = (obj.GetProp("l") != "") ?
Converter.StringToFloat(obj.GetProp("l")) : 0;
float objY = (obj.GetProp("t") != "") ?
Converter.StringToFloat(obj.GetProp("t")) : 0;
obj.SetProp("l", Converter.ToString(objX + deltaX));
obj.SetProp("t", Converter.ToString(objY + deltaY));
// add object to a page
xmlItem.AddItem(obj);
}
}
}
public float GetLastY()
{
float result = 0;
for (int i = 0; i < xmlItem.Count; i++)
{
XmlItem xi = xmlItem[i];
BandBase obj = preparedPages.Dictionary.GetOriginalObject(xi.Name) as BandBase;
if (obj != null && !(obj is PageFooterBand) && !(obj is OverlayBand))
{
string s = xi.GetProp("t");
float top = (s != "") ? Converter.StringToFloat(s) : obj.Top;
s = xi.GetProp("h");
float height = (s != "") ? Converter.StringToFloat(s) : obj.Height;
if (top + height > result)
result = top + height;
}
}
return result;
}
public bool ContainsBand(Type bandType)
{
for (int i = 0; i < xmlItem.Count; i++)
{
XmlItem xi = xmlItem[i];
BandBase obj = preparedPages.Dictionary.GetOriginalObject(xi.Name) as BandBase;
if (obj != null && obj.GetType() == bandType)
return true;
}
return false;
}
public bool ContainsBand(string bandName)
{
for (int i = 0; i < xmlItem.Count; i++)
{
XmlItem xi = xmlItem[i];
BandBase obj = preparedPages.Dictionary.GetOriginalObject(xi.Name) as BandBase;
if (obj != null && obj.Name == bandName)
return true;
}
return false;
}
public void Dispose()
{
xmlItem.Dispose();
}
#endregion Public Methods
}
}