-
Notifications
You must be signed in to change notification settings - Fork 16
/
PdfViewer.cs
4410 lines (3977 loc) · 166 KB
/
PdfViewer.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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Patagames.Pdf.Enums;
using Patagames.Pdf.Net.EventArguments;
using Patagames.Pdf.Net.Exceptions;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.ComponentModel;
using Patagames.Pdf.Net.Annotations;
using Patagames.Pdf.Net.Actions;
using Patagames.Pdf.Net.Wrappers;
namespace Patagames.Pdf.Net.Controls.Wpf
{
/// <summary>
/// Represents a pdf view control for displaying a Pdf document.
/// </summary>
[LicenseProvider]
public partial class PdfViewer : Control, IScrollInfo
{
#region Private fields
private bool _preventStackOverflowBugWorkaround = false;
private SelectInfo _selectInfo = new SelectInfo() { StartPage = -1 };
private SortedDictionary<int, List<HighlightInfo>> _highlightedText = new SortedDictionary<int, List<HighlightInfo>>();
private bool __mousePressed = false;
private bool _mousePressed { get => __mousePressed && Mouse.LeftButton == MouseButtonState.Pressed; set => __mousePressed = value; }
private bool _mousePressedInLink = false;
private bool _isShowSelection = false;
private int _onstartPageIndex = 0;
private Point _panToolInitialScrollPosition;
private Point _panToolInitialMousePosition;
private PdfForms _fillForms;
private List<Rect> _selectedRectangles = new List<Rect>();
private Pen _pageBorderColorPen = Helpers.CreatePen((Color)PageBorderColorProperty.DefaultMetadata.DefaultValue);
private Pen _pageSeparatorColorPen = Helpers.CreatePen((Color)PageSeparatorColorProperty.DefaultMetadata.DefaultValue);
private Pen _currentPageHighlightColorPen = Helpers.CreatePen((Color)CurrentPageHighlightColorProperty.DefaultMetadata.DefaultValue, 4);
private RenderRect[] _renderRects;
private int _startPage
{
get
{
if (Document == null)
return 0;
switch (ViewMode)
{
case ViewModes.SinglePage:
return Document.Pages.CurrentIndex;
case ViewModes.TilesLine:
return Document.Pages.CurrentIndex % TilesCount == 0 ? Document.Pages.CurrentIndex : Document.Pages.CurrentIndex - Document.Pages.CurrentIndex % TilesCount;
default:
return 0;
}
}
}
private int _endPage
{
get
{
if (Document == null)
return -1;
switch (ViewMode)
{
case ViewModes.SinglePage:
return Document.Pages.CurrentIndex;
case ViewModes.TilesLine:
return Math.Min(_startPage + TilesCount - 1, _renderRects != null ? _renderRects.Length - 1 : -1);
default:
return _renderRects != null ? _renderRects.Length - 1 : -1;
}
}
}
private Size _extent = new Size(0, 0);
private Size _viewport = new Size(0, 0);
private Point _autoScrollPosition = new Point(0, 0);
private bool _isProgrammaticallyFocusSetted=false;
private PRCollection _prPages = new PRCollection();
private System.Windows.Threading.DispatcherTimer _invalidateTimer = null;
WriteableBitmap _canvasWpfBitmap = null;
private bool _loadedByViewer = true;
private struct CaptureInfo
{
public PdfForms forms;
public SynchronizationContext sync;
public int color;
}
private CaptureInfo _externalDocCapture;
private Point _scrollPoint;
private bool _scrollPointSaved;
private enum SmoothSelection { None, ByCharacter, ByLine }
private SmoothSelection _smoothSelection;
#endregion
#region Events
/// <summary>
/// Occurs whenever the Document property is changed.
/// </summary>
public event EventHandler AfterDocumentChanged;
/// <summary>
/// Occurs immediately before the document property would be changed.
/// </summary>
public event EventHandler<DocumentClosingEventArgs> BeforeDocumentChanged;
/// <summary>
/// Occurs whenever the document loads.
/// </summary>
public event EventHandler DocumentLoaded;
/// <summary>
/// Occurs before the document unloads.
/// </summary>
public event EventHandler<DocumentClosingEventArgs> DocumentClosing;
/// <summary>
/// Occurs whenever the document unloads.
/// </summary>
public event EventHandler DocumentClosed;
/// <summary>
/// Occurs when the <see cref="SizeMode"/> property has changed.
/// </summary>
public event EventHandler SizeModeChanged;
/// <summary>
/// Event raised when the value of the <see cref="PageBackColor"/> property is changed on Control..
/// </summary>
public event EventHandler PageBackColorChanged;
/// <summary>
/// Occurs when the <see cref="PageMargin"/> property has changed.
/// </summary>
public event EventHandler PageMarginChanged;
/// <summary>
/// Event raised when the value of the <see cref="PageBorderColor"/> property is changed on Control.
/// </summary>
public event EventHandler PageBorderColorChanged;
/// <summary>
/// Event raised when the value of the <see cref="TextSelectColor"/> property is changed on Control.
/// </summary>
public event EventHandler TextSelectColorChanged;
/// <summary>
/// Event raised when the value of the <see cref="FormHighlightColor"/> property is changed on Control.
/// </summary>
public event EventHandler FormHighlightColorChanged;
/// <summary>
/// Occurs when the <see cref="Zoom"/> property has changed.
/// </summary>
public event EventHandler ZoomChanged;
/// <summary>
/// Occurs when the current selection has changed.
/// </summary>
public event EventHandler SelectionChanged;
/// <summary>
/// Occurs when the <see cref="ViewMode"/> property has changed.
/// </summary>
public event EventHandler ViewModeChanged;
/// <summary>
/// Occurs when the <see cref="PageSeparatorColor"/> property has changed.
/// </summary>
public event EventHandler PageSeparatorColorChanged;
/// <summary>
/// Occurs when the <see cref="ShowPageSeparator"/> property has changed.
/// </summary>
public event EventHandler ShowPageSeparatorChanged;
/// <summary>
/// Occurs when the <see cref="CurrentPage"/> or <see cref="CurrentIndex"/> property has changed.
/// </summary>
public event EventHandler CurrentPageChanged;
/// <summary>
/// Occurs when the <see cref="CurrentPageHighlightColor"/> property has changed.
/// </summary>
public event EventHandler CurrentPageHighlightColorChanged;
/// <summary>
/// Occurs when the <see cref="ShowCurrentPageHighlight"/> property has changed.
/// </summary>
public event EventHandler ShowCurrentPageHighlightChanged;
/// <summary>
/// Occurs when the value of the <see cref="PageVAlign"/> or <see cref="PageHAlign"/> property has changed.
/// </summary>
public event EventHandler PageAlignChanged;
/// <summary>
/// Occurs before PdfLink or WebLink on the page was clicked.
/// </summary>
public event EventHandler<PdfBeforeLinkClickedEventArgs> BeforeLinkClicked;
/// <summary>
/// Occurs after PdfLink or WebLink on the page was clicked.
/// </summary>
public event EventHandler<PdfAfterLinkClickedEventArgs> AfterLinkClicked;
/// <summary>
/// Occurs when the value of the <see cref="RenderFlags"/> property has changed.
/// </summary>
public event EventHandler RenderFlagsChanged;
/// <summary>
/// Occurs when the value of the <see cref="TilesCount"/> property has changed.
/// </summary>
public event EventHandler TilesCountChanged;
/// <summary>
/// Occurs when the text highlighting changed
/// </summary>
public event EventHandler HighlightedTextChanged;
/// <summary>
/// Occurs when the value of the <see cref="MouseModes"/> property has changed.
/// </summary>
public event EventHandler MouseModeChanged;
/// <summary>
/// Occurs when the value of the <see cref="ShowLoadingIcon"/> property has changed.
/// </summary>
public event EventHandler ShowLoadingIconChanged;
/// <summary>
/// Occurs when the value of the <see cref="UseProgressiveRender"/> property has changed.
/// </summary>
public event EventHandler UseProgressiveRenderChanged;
/// <summary>
/// Occurs when the value of the <see cref="LoadingIconText"/> property has changed.
/// </summary>
public event EventHandler LoadingIconTextChanged;
/// <summary>
/// Occurs when the <see cref="FormsBlendMode"/> property has changed.
/// </summary>
public event EventHandler FormsBlendModeChanged;
#endregion
#region Event raises
/// <summary>
/// Raises the <see cref="AfterDocumentChanged"/> event.
/// </summary>
/// <param name="e">An System.EventArgs that contains the event data.</param>
protected virtual void OnAfterDocumentChanged(EventArgs e)
{
if (AfterDocumentChanged != null)
AfterDocumentChanged(this, e);
}
/// <summary>
/// Raises the <see cref="BeforeDocumentChanged"/> event.
/// </summary>
/// <param name="e">An System.EventArgs that contains the event data.</param>
/// <returns>True if the change should be canceled; otherwise - False</returns>
protected virtual bool OnBeforeDocumentChanged(DocumentClosingEventArgs e)
{
if (BeforeDocumentChanged != null)
BeforeDocumentChanged(this, e);
return e.Cancel;
}
/// <summary>
/// Raises the <see cref="DocumentLoaded"/> event.
/// </summary>
/// <param name="e">An System.EventArgs that contains the event data.</param>
protected virtual void OnDocumentLoaded(EventArgs e)
{
if (DocumentLoaded != null)
DocumentLoaded(this, e);
}
/// <summary>
/// Raises the <see cref="DocumentClosing"/> event.
/// </summary>
/// <param name="e">An System.EventArgs that contains the event data.</param>
/// <returns>True if the close should be canceled; otherwise - False</returns>
protected virtual bool OnDocumentClosing(DocumentClosingEventArgs e)
{
if (DocumentClosing != null)
DocumentClosing(this, e);
return e.Cancel;
}
/// <summary>
/// Raises the <see cref="DocumentClosed"/> event.
/// </summary>
/// <param name="e">An System.EventArgs that contains the event data.</param>
protected virtual void OnDocumentClosed(EventArgs e)
{
if (DocumentClosed != null)
DocumentClosed(this, e);
}
/// <summary>
/// Raises the <see cref="SizeModeChanged"/> event.
/// </summary>
/// <param name="e">An System.EventArgs that contains the event data.</param>
protected virtual void OnSizeModeChanged(EventArgs e)
{
if (SizeModeChanged != null)
SizeModeChanged(this, e);
}
/// <summary>
/// Raises the <see cref="PageBackColorChanged"/> event.
/// </summary>
/// <param name="e">An System.EventArgs that contains the event data.</param>
protected virtual void OnPageBackColorChanged(EventArgs e)
{
if (PageBackColorChanged != null)
PageBackColorChanged(this, e);
}
/// <summary>
/// Raises the <see cref="PageMarginChanged"/> event.
/// </summary>
/// <param name="e">An System.EventArgs that contains the event data.</param>
protected virtual void OnPageMarginChanged(EventArgs e)
{
if (PageMarginChanged != null)
PageMarginChanged(this, e);
}
/// <summary>
/// Raises the <see cref="PageBorderColorChanged"/> event.
/// </summary>
/// <param name="e">An System.EventArgs that contains the event data.</param>
protected virtual void OnPageBorderColorChanged(EventArgs e)
{
if (PageBorderColorChanged != null)
PageBorderColorChanged(this, e);
}
/// <summary>
/// Raises the <see cref="TextSelectColorChanged"/> event.
/// </summary>
/// <param name="e">An System.EventArgs that contains the event data.</param>
protected virtual void OnTextSelectColorChanged(EventArgs e)
{
if (TextSelectColorChanged != null)
TextSelectColorChanged(this, e);
}
/// <summary>
/// Raises the <see cref="FormHighlightColorChanged"/> event.
/// </summary>
/// <param name="e">An System.EventArgs that contains the event data.</param>
protected virtual void OnFormHighlightColorChanged(EventArgs e)
{
if (FormHighlightColorChanged != null)
FormHighlightColorChanged(this, e);
}
/// <summary>
/// Raises the <see cref="ZoomChanged"/> event.
/// </summary>
/// <param name="e">An System.EventArgs that contains the event data.</param>
protected virtual void OnZoomChanged(EventArgs e)
{
if (ZoomChanged != null)
ZoomChanged(this, e);
}
/// <summary>
/// Raises the <see cref="SelectionChanged"/> event.
/// </summary>
/// <param name="e">An System.EventArgs that contains the event data.</param>
protected virtual void OnSelectionChanged(EventArgs e)
{
if (SelectionChanged != null)
SelectionChanged(this, e);
}
/// <summary>
/// Raises the <see cref="ViewModeChanged"/> event.
/// </summary>
/// <param name="e">An System.EventArgs that contains the event data.</param>
protected virtual void OnViewModeChanged(EventArgs e)
{
if (ViewModeChanged != null)
ViewModeChanged(this, e);
}
/// <summary>
/// Raises the <see cref="PageSeparatorColorChanged"/> event.
/// </summary>
/// <param name="e">An System.EventArgs that contains the event data.</param>
protected virtual void OnPageSeparatorColorChanged(EventArgs e)
{
if (PageSeparatorColorChanged != null)
PageSeparatorColorChanged(this, e);
}
/// <summary>
/// Raises the <see cref="ShowPageSeparatorChanged"/> event.
/// </summary>
/// <param name="e">An System.EventArgs that contains the event data.</param>
protected virtual void OnShowPageSeparatorChanged(EventArgs e)
{
if (ShowPageSeparatorChanged != null)
ShowPageSeparatorChanged(this, e);
}
/// <summary>
/// Raises the <see cref="CurrentPageChanged"/> event.
/// </summary>
/// <param name="e">An System.EventArgs that contains the event data.</param>
protected virtual void OnCurrentPageChanged(EventArgs e)
{
if (CurrentPageChanged != null)
CurrentPageChanged(this, e);
}
/// <summary>
/// Raises the <see cref="CurrentPageHighlightColorChanged"/> event.
/// </summary>
/// <param name="e">An System.EventArgs that contains the event data.</param>
protected virtual void OnCurrentPageHighlightColorChanged(EventArgs e)
{
if (CurrentPageHighlightColorChanged != null)
CurrentPageHighlightColorChanged(this, e);
}
/// <summary>
/// Raises the <see cref="ShowCurrentPageHighlightChanged"/> event.
/// </summary>
/// <param name="e">An System.EventArgs that contains the event data.</param>
protected virtual void OnShowCurrentPageHighlightChanged(EventArgs e)
{
if (ShowCurrentPageHighlightChanged != null)
ShowCurrentPageHighlightChanged(this, e);
}
/// <summary>
/// Raises the <see cref="PageAlignChanged"/> event.
/// </summary>
/// <param name="e">An System.EventArgs that contains the event data.</param>
protected virtual void OnPageAlignChanged(EventArgs e)
{
if (PageAlignChanged != null)
PageAlignChanged(this, e);
}
/// <summary>
/// Raises the <see cref="BeforeLinkClicked"/> event.
/// </summary>
/// <param name="e">An PdfBeforeLinkClickedEventArgs that contains the event data.</param>
protected virtual void OnBeforeLinkClicked(PdfBeforeLinkClickedEventArgs e)
{
if (BeforeLinkClicked != null)
BeforeLinkClicked(this, e);
}
/// <summary>
/// Raises the <see cref="AfterLinkClicked"/> event.
/// </summary>
/// <param name="e">An PdfAfterLinkClickedEventArgs that contains the event data.</param>
protected virtual void OnAfterLinkClicked(PdfAfterLinkClickedEventArgs e)
{
if (AfterLinkClicked != null)
AfterLinkClicked(this, e);
}
/// <summary>
/// Raises the <see cref="RenderFlagsChanged"/> event.
/// </summary>
/// <param name="e">An System.EventArgs that contains the event data.</param>
protected virtual void OnRenderFlagsChanged(EventArgs e)
{
if (RenderFlagsChanged != null)
RenderFlagsChanged(this, e);
}
/// <summary>
/// Raises the <see cref="TilesCountChanged"/> event.
/// </summary>
/// <param name="e">An System.EventArgs that contains the event data.</param>
protected virtual void OnTilesCountChanged(EventArgs e)
{
if (TilesCountChanged != null)
TilesCountChanged(this, e);
}
/// <summary>
/// Raises the <see cref="HighlightedTextChanged"/> event.
/// </summary>
/// <param name="e">An System.EventArgs that contains the event data.</param>
protected virtual void OnHighlightedTextChanged(EventArgs e)
{
if (HighlightedTextChanged != null)
HighlightedTextChanged(this, e);
}
/// <summary>
/// Raises the <see cref="MouseModeChanged"/> event.
/// </summary>
/// <param name="e">An System.EventArgs that contains the event data.</param>
protected virtual void OnMouseModeChanged(EventArgs e)
{
if (MouseModeChanged != null)
MouseModeChanged(this, e);
}
/// <summary>
/// Raises the <see cref="ShowLoadingIconChanged"/> event.
/// </summary>
/// <param name="e">An System.EventArgs that contains the event data.</param>
protected virtual void OnShowLoadingIconChanged(EventArgs e)
{
if (ShowLoadingIconChanged != null)
ShowLoadingIconChanged(this, e);
}
/// <summary>
/// Raises the <see cref="UseProgressiveRenderChanged"/> event.
/// </summary>
/// <param name="e">An System.EventArgs that contains the event data.</param>
protected virtual void OnUseProgressiveRenderChanged(EventArgs e)
{
if (UseProgressiveRenderChanged != null)
UseProgressiveRenderChanged(this, e);
}
/// <summary>
/// Raises the <see cref="LoadingIconTextChanged"/> event.
/// </summary>
/// <param name="e">An System.EventArgs that contains the event data.</param>
protected virtual void OnLoadingIconTextChanged(EventArgs e)
{
if (LoadingIconTextChanged != null)
LoadingIconTextChanged(this, e);
}
/// <summary>
/// Raises the <see cref="FormsBlendModeChanged"/> event.
/// </summary>
/// <param name="e">An System.EventArgs that contains the event data.</param>
protected virtual void OnFormsBlendModeChanged(EventArgs e)
{
if (FormsBlendModeChanged != null)
FormsBlendModeChanged(this, e);
}
#endregion
#region Dependency properties
/// <summary>
/// DependencyProperty as the backing store for <see cref="FormsBlendMode"/>
/// </summary>
public static readonly DependencyProperty FormsBlendModeProperty =
DependencyProperty.Register("FormsBlendMode", typeof(BlendTypes), typeof(PdfViewer),
new FrameworkPropertyMetadata(BlendTypes.FXDIB_BLEND_MULTIPLY,
FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Journal,
(o, e) => { (o as PdfViewer).OnFormsBlendModeChanged(EventArgs.Empty); }));
/// <summary>
/// DependencyProperty as the backing store for <see cref="PdfViewer"/>
/// </summary>
/// <remarks>
/// <note type="note">
/// Please note
/// <list type="bullet">
/// <item>
/// The OneWay binding would be disabled if you set the Document property with a document what is not from the binding source.
/// More explanations can be found <a href="http://stackoverflow.com/questions/1389038/why-does-data-binding-break-in-oneway-mode">here</a>
/// </item>
/// <item>The TwoWay binding does not allow you to set the Document property with the document what is not from a binding source.</item>
/// </list>
/// </note>
/// </remarks>
public static readonly DependencyProperty DocumentProperty =
DependencyProperty.Register("Document", typeof(PdfDocument), typeof(PdfViewer),
new PropertyMetadata(null,
(o, e) =>
{
var viewer = o as PdfViewer;
var oldValue = e.OldValue as PdfDocument;
var newValue = e.NewValue as PdfDocument;
if (oldValue != newValue)
{
viewer._prPages.ReleaseCanvas();
if (oldValue != null && viewer._loadedByViewer)
{
//we need to close the previous document if it was loaded by viewer
oldValue.Dispose();
//_document = null;
viewer.OnDocumentClosed(EventArgs.Empty);
}
else if (oldValue != null && !viewer._loadedByViewer)
{
oldValue.Pages.CurrentPageChanged -= viewer.Pages_CurrentPageChanged;
oldValue.Pages.PageInserted -= viewer.Pages_PageInserted;
oldValue.Pages.PageDeleted -= viewer.Pages_PageDeleted;
oldValue.Pages.ProgressiveRender -= viewer.Pages_ProgressiveRender;
}
viewer._extent = new Size(0, 0);
viewer._selectInfo = new SelectInfo() { StartPage = -1 };
viewer._highlightedText.Clear();
//viewer._onstartPageIndex = 0;
viewer._renderRects = null;
viewer._loadedByViewer = false;
viewer.ReleaseFillForms(viewer._externalDocCapture);
//_document = value;
viewer.UpdateDocLayout();
if (newValue != null)
{
if (newValue.FormFill != viewer._fillForms)
viewer._externalDocCapture = viewer.CaptureFillForms(newValue.FormFill);
newValue.Pages.CurrentPageChanged += viewer.Pages_CurrentPageChanged;
newValue.Pages.PageInserted += viewer.Pages_PageInserted;
newValue.Pages.PageDeleted += viewer.Pages_PageDeleted;
newValue.Pages.ProgressiveRender += viewer.Pages_ProgressiveRender;
if (Pdfium.IsFullAPI && newValue.OpenDestination != null)
viewer._onstartPageIndex = newValue.OpenDestination.PageIndex;
viewer.SetCurrentPage(viewer._onstartPageIndex);
if (newValue.Pages.Count > 0)
if (viewer._onstartPageIndex != 0)
viewer.ScrollToPage(viewer._onstartPageIndex);
else
viewer._autoScrollPosition = new Point(0, 0);
viewer._onstartPageIndex = 0;
}
viewer.OnAfterDocumentChanged(EventArgs.Empty);
}
},
(dobj, o) =>
{
var viewer = dobj as PdfViewer;
var oldValue = viewer.Document;
var newValue = o as PdfDocument;
if (oldValue != newValue)
{
if (viewer.OnBeforeDocumentChanged(new DocumentClosingEventArgs()))
return oldValue;
if (oldValue != null && viewer._loadedByViewer)
{
//we need to close the previous document if it was loaded by viewer
if (viewer.OnDocumentClosing(new DocumentClosingEventArgs()))
return oldValue; //the closing was canceled;
}
}
return newValue;
}));
/// <summary>
/// DependencyProperty as the backing store for <see cref="PageBackColor"/>
/// </summary>
public static readonly DependencyProperty PageBackColorProperty =
DependencyProperty.Register("PageBackColor", typeof(Color), typeof(PdfViewer),
new FrameworkPropertyMetadata(Color.FromArgb(255, 255, 255, 255),
FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Journal,
(o, e) => { (o as PdfViewer).OnPageBackColorChanged(EventArgs.Empty); }));
/// <summary>
/// DependencyProperty as the backing store for <see cref="PageMargin"/>
/// </summary>
public static readonly DependencyProperty PageMarginProperty =
DependencyProperty.Register("PageMargin", typeof(Thickness), typeof(PdfViewer),
new FrameworkPropertyMetadata(new Thickness(10),
FrameworkPropertyMetadataOptions.Journal | FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.AffectsMeasure,
(o, e) => {
(o as PdfViewer).UpdateDocLayout();
(o as PdfViewer).OnPageMarginChanged(EventArgs.Empty); }));
/// <summary>
/// DependencyProperty as the backing store for <see cref="Padding"/>
/// </summary>
public static readonly new DependencyProperty PaddingProperty =
DependencyProperty.Register("Padding", typeof(Thickness), typeof(PdfViewer),
new FrameworkPropertyMetadata(new Thickness(10),
FrameworkPropertyMetadataOptions.Journal | FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.AffectsMeasure,
(o, e) => {
(o as PdfViewer).UpdateDocLayout();
}));
/// <summary>
/// DependencyProperty as the backing store for <see cref="PageBorderColor"/>
/// </summary>
public static readonly DependencyProperty PageBorderColorProperty =
DependencyProperty.Register("PageBorderColor", typeof(Color), typeof(PdfViewer),
new FrameworkPropertyMetadata(Color.FromArgb(255, 0, 0, 0),
FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Journal,
(o, e) =>
{
var viewer = (o as PdfViewer);
viewer._pageBorderColorPen = Helpers.CreatePen((Color)e.NewValue);
viewer.OnPageBorderColorChanged(EventArgs.Empty);
}));
/// <summary>
/// DependencyProperty as the backing store for <see cref="SizeMode"/>
/// </summary>
public static readonly DependencyProperty SizeModeProperty =
DependencyProperty.Register("SizeMode", typeof(SizeModes), typeof(PdfViewer),
new FrameworkPropertyMetadata(SizeModes.FitToWidth,
FrameworkPropertyMetadataOptions.Journal,
(o, e) =>
{
var viewer = (o as PdfViewer);
viewer.UpdateDocLayout();
viewer.OnSizeModeChanged(EventArgs.Empty);
}));
/// <summary>
/// DependencyProperty as the backing store for <see cref="TextSelectColor"/>
/// </summary>
public static readonly DependencyProperty TextSelectColorProperty =
DependencyProperty.Register("TextSelectColor", typeof(Color), typeof(PdfViewer),
new FrameworkPropertyMetadata(Color.FromArgb(70, 70, 130, 180),
FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Journal,
(o, e) =>
{
var viewer = (o as PdfViewer);
viewer.OnTextSelectColorChanged(EventArgs.Empty);
}));
/// <summary>
/// DependencyProperty as the backing store for <see cref="FormHighlightColor"/>
/// </summary>
public static readonly DependencyProperty FormHighlightColorProperty =
DependencyProperty.Register("FormHighlightColor", typeof(Color), typeof(PdfViewer),
new FrameworkPropertyMetadata(Color.FromArgb(0, 255, 255, 255),
FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Journal,
(o, e) =>
{
var viewer = (o as PdfViewer);
if (viewer._fillForms != null)
viewer._fillForms.SetHighlightColorEx(FormFieldTypes.FPDF_FORMFIELD_UNKNOWN, Helpers.ToArgb((Color)e.NewValue));
if (viewer.Document != null && !viewer._loadedByViewer && viewer._externalDocCapture.forms != null)
viewer._externalDocCapture.forms.SetHighlightColorEx(FormFieldTypes.FPDF_FORMFIELD_UNKNOWN, Helpers.ToArgb((Color)e.NewValue));
viewer.OnFormHighlightColorChanged(EventArgs.Empty);
}));
/// <summary>
/// DependencyProperty as the backing store for <see cref="Zoom"/>
/// </summary>
public static readonly DependencyProperty ZoomProperty =
DependencyProperty.Register("Zoom", typeof(float), typeof(PdfViewer),
new FrameworkPropertyMetadata(1.0f,
FrameworkPropertyMetadataOptions.Journal | FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.AffectsMeasure,
(o, e) =>
{
var viewer = (o as PdfViewer);
if (viewer._preventStackOverflowBugWorkaround)
return;
viewer.UpdateDocLayout();
viewer.OnZoomChanged(EventArgs.Empty);
}));
/// <summary>
/// DependencyProperty as the backing store for <see cref="SelectedText"/>
/// </summary>
public static readonly DependencyProperty SelectedTextProperty =
DependencyProperty.Register("SelectedText", typeof(string), typeof(PdfViewer),
new FrameworkPropertyMetadata("",
FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Journal));
/// <summary>
/// DependencyProperty as the backing store for <see cref="ViewMode"/>
/// </summary>
public static readonly DependencyProperty ViewModeProperty =
DependencyProperty.Register("ViewMode", typeof(ViewModes), typeof(PdfViewer),
new FrameworkPropertyMetadata(ViewModes.Vertical,
FrameworkPropertyMetadataOptions.Journal,
(o, e) =>
{
var viewer = (o as PdfViewer);
viewer.UpdateDocLayout();
viewer.OnViewModeChanged(EventArgs.Empty);
}));
/// <summary>
/// DependencyProperty as the backing store for <see cref="PageSeparatorColor"/>
/// </summary>
public static readonly DependencyProperty PageSeparatorColorProperty =
DependencyProperty.Register("PageSeparatorColor", typeof(Color), typeof(PdfViewer),
new FrameworkPropertyMetadata(Color.FromArgb(255, 190, 190, 190),
FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Journal,
(o, e) =>
{
var viewer = (o as PdfViewer);
viewer._pageSeparatorColorPen = Helpers.CreatePen((Color)e.NewValue);
viewer.OnPageSeparatorColorChanged(EventArgs.Empty);
}));
/// <summary>
/// DependencyProperty as the backing store for <see cref="ShowPageSeparator"/>
/// </summary>
public static readonly DependencyProperty ShowPageSeparatorProperty =
DependencyProperty.Register("ShowPageSeparator", typeof(bool), typeof(PdfViewer),
new FrameworkPropertyMetadata(true,
FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Journal,
(o, e) =>
{
var viewer = (o as PdfViewer);
viewer.OnShowPageSeparatorChanged(EventArgs.Empty);
}));
/// <summary>
/// DependencyProperty as the backing store for <see cref="CurrentPageHighlightColor"/>
/// </summary>
public static readonly DependencyProperty CurrentPageHighlightColorProperty =
DependencyProperty.Register("CurrentPageHighlightColor", typeof(Color), typeof(PdfViewer),
new FrameworkPropertyMetadata(Color.FromArgb(170, 70, 130, 180),
FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Journal,
(o, e) =>
{
var viewer = (o as PdfViewer);
viewer._currentPageHighlightColorPen = Helpers.CreatePen((Color)e.NewValue, 4);
viewer.OnCurrentPageHighlightColorChanged(EventArgs.Empty);
}));
/// <summary>
/// DependencyProperty as the backing store for <see cref="ShowCurrentPageHighlight"/>
/// </summary>
public static readonly DependencyProperty ShowCurrentPageHighlightProperty =
DependencyProperty.Register("ShowCurrentPageHighlight", typeof(bool), typeof(PdfViewer),
new FrameworkPropertyMetadata(true,
FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Journal,
(o, e) =>
{
var viewer = (o as PdfViewer);
viewer.OnShowCurrentPageHighlightChanged(EventArgs.Empty);
}));
/// <summary>
/// DependencyProperty as the backing store for <see cref="PageVAlign"/>
/// </summary>
public static readonly DependencyProperty PageVAlignProperty =
DependencyProperty.Register("PageVAlign", typeof(VerticalAlignment), typeof(PdfViewer),
new FrameworkPropertyMetadata(VerticalAlignment.Center,
FrameworkPropertyMetadataOptions.Journal,
(o, e) =>
{
var viewer = (o as PdfViewer);
viewer.UpdateDocLayout();
viewer.OnPageAlignChanged(EventArgs.Empty);
}));
/// <summary>
/// DependencyProperty as the backing store for <see cref="PageHAlign"/>
/// </summary>
public static readonly DependencyProperty PageHAlignProperty =
DependencyProperty.Register("PageHAlign", typeof(HorizontalAlignment), typeof(PdfViewer),
new FrameworkPropertyMetadata(HorizontalAlignment.Center,
FrameworkPropertyMetadataOptions.Journal,
(o, e) =>
{
var viewer = (o as PdfViewer);
viewer.UpdateDocLayout();
viewer.OnPageAlignChanged(EventArgs.Empty);
}));
/// <summary>
/// DependencyProperty as the backing store for <see cref="RenderFlags"/>
/// </summary>
public static readonly DependencyProperty RenderFlagsProperty =
DependencyProperty.Register("RenderFlags", typeof(RenderFlags), typeof(PdfViewer),
new FrameworkPropertyMetadata(RenderFlags.FPDF_LCD_TEXT | RenderFlags.FPDF_NO_CATCH,
FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Journal,
(o, e) =>
{
var viewer = (o as PdfViewer);
viewer.OnRenderFlagsChanged(EventArgs.Empty);
}));
/// <summary>
/// DependencyProperty as the backing store for <see cref="TilesCount"/>
/// </summary>
public static readonly DependencyProperty TilesCountProperty =
DependencyProperty.Register("TilesCount", typeof(int), typeof(PdfViewer),
new FrameworkPropertyMetadata(2,
FrameworkPropertyMetadataOptions.Journal,
(o, e) =>
{
var viewer = (o as PdfViewer);
viewer.UpdateDocLayout();
viewer.OnTilesCountChanged(EventArgs.Empty);
},
(v, o) =>
{
return (int)o < 2 ? 2 : o;
}));
/// <summary>
/// DependencyProperty as the backing store for <see cref="MouseMode"/>
/// </summary>
public static readonly DependencyProperty MouseModeProperty =
DependencyProperty.Register("MouseMode", typeof(MouseModes), typeof(PdfViewer),
new FrameworkPropertyMetadata(MouseModes.Default,
FrameworkPropertyMetadataOptions.Journal,
(o, e) =>
{
var viewer = (o as PdfViewer);
viewer.OnMouseModeChanged(EventArgs.Empty);
}));
/// <summary>
/// DependencyProperty as the backing store for <see cref="ShowLoadingIcon"/>
/// </summary>
public static readonly DependencyProperty ShowLoadingIconProperty =
DependencyProperty.Register("ShowLoadingIcon", typeof(bool), typeof(PdfViewer),
new FrameworkPropertyMetadata(true,
FrameworkPropertyMetadataOptions.Journal,
(o, e) =>
{
var viewer = (o as PdfViewer);
viewer.OnShowLoadingIconChanged(EventArgs.Empty);
}));
/// <summary>
/// DependencyProperty as the backing store for <see cref="UseProgressiveRender"/>
/// </summary>
public static readonly DependencyProperty UseProgressiveRenderProperty =
DependencyProperty.Register("UseProgressiveRender", typeof(bool), typeof(PdfViewer),
new FrameworkPropertyMetadata(true,
FrameworkPropertyMetadataOptions.Journal,
(o, e) =>
{
var viewer = (o as PdfViewer);
viewer.UpdateDocLayout();
viewer.OnUseProgressiveRenderChanged(EventArgs.Empty);
}));
/// <summary>
/// DependencyProperty as the backing store for <see cref="LoadingIconText"/>
/// </summary>
public static readonly DependencyProperty LoadingIconTextProperty =
DependencyProperty.Register("LoadingIconText", typeof(string), typeof(PdfViewer),
new FrameworkPropertyMetadata("",
FrameworkPropertyMetadataOptions.Journal,
(o, e) =>
{
var viewer = (o as PdfViewer);
viewer.OnLoadingIconTextChanged(EventArgs.Empty);
}));
/// <summary>
/// DependencyProperty as the backing store for <see cref="PageAutoDispose"/>
/// </summary>
public static readonly DependencyProperty PageAutoDisposeProperty =
DependencyProperty.Register("PageAutoDispose", typeof(bool), typeof(PdfViewer),
new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.Journal));
/// <summary>
/// DependencyProperty as the backing store for <see cref="OptimizedLoadThreshold"/>
/// </summary>
public static readonly DependencyProperty OptimizedLoadThresholdProperty =
DependencyProperty.Register("OptimizedLoadThreshold", typeof(int), typeof(PdfViewer),
new FrameworkPropertyMetadata(1000));
#endregion
#region Public properties (dependency)
/// <summary>
/// Gets or sets blend mode which is used in drawing of acro forms.
/// </summary>
/// <remarks>
/// <para>Default value: <strong>FXDIB_BLEND_MULTIPLY</strong></para>
/// </remarks>
public BlendTypes FormsBlendMode
{
get { return (BlendTypes)GetValue(FormsBlendModeProperty); }
set { SetValue(FormsBlendModeProperty, value); }
}
/// <summary>
/// Gets or sets the PDF document associated with the current PdfViewer control.
/// </summary>
/// <remarks>It's a dependency property. Please find more details here: <see cref="PdfViewer.DocumentProperty"/></remarks>
public PdfDocument Document
{
get { return (PdfDocument)GetValue(DocumentProperty); }
set { SetValue(DocumentProperty, value); }