Sample C# code for using Apryse SDK's PDFViewWPF class. Learn more about our Server SDK and PDF Viewer SDK.
1using System;
2
3using System.Collections.Generic;
4
5using System.Linq;
6
7using System.Text;
8
9using System.Windows;
10
11using System.Windows.Controls;
12
13using System.Windows.Data;
14
15using System.Windows.Documents;
16
17using System.Windows.Input;
18
19using System.Windows.Media;
20
21using System.Windows.Media.Imaging;
22
23using System.Windows.Navigation;
24
25using System.Windows.Shapes;
26
27
28
29using pdftron;
30
31using pdftron.PDF;
32
33using pdftron.SDF;
34
35using pdftron.Common;
36
37
38
39namespace PDFViewWPFTestCS
40
41{
42
43 /// <summary>
44
45 /// Interaction logic for PDFViewViewer.xaml
46
47 /// </summary>
48
49 public partial class PDFViewViewer : UserControl
50
51 {
52
53 private PDFDoc _pdfdoc = null; // currently open PDF document
54
55 private SideWindowControl _sidecontrol;
56
57 private GridLength _oldwidth = new GridLength(200);
58
59 private PDFViewWPF.Selection _currentSearchSelection;
60
61 private List<Rectangle> _onScreenSelection = new List<Rectangle>();
62
63
64
65 private MainWindow Current_Main { get; set; }
66
67 public PDFViewWPF Current_View { get; set; }
68
69 public bool IsNaviOpen { get; set; }
70
71 public double NaviWidth
72
73 {
74
75 get { return DocViewerGrid.ColumnDefinitions[0].Width.Value; }
76
77 set { DocViewerGrid.ColumnDefinitions[0].Width = new GridLength(value); }
78
79 }
80
81 public double ZoomLevel
82
83 {
84
85 get { return Current_View.GetZoom(); }
86
87 set { Current_View.SetZoom(value); }
88
89 }
90
91 public string _safeDocName { get; set; }
92
93
94
95 private pdftron.PDF.Tools.ToolManager _ToolManager;
96
97 public pdftron.PDF.Tools.ToolManager ToolManager
98
99 {
100
101 get { return _ToolManager; }
102
103 }
104
105
106
107 public PDFViewViewer(MainWindow main_window)
108
109 {
110
111 InitializeComponent();
112
113
114
115 PresentationSource source = PresentationSource.FromVisual(Application.Current.MainWindow);
116
117
118
119 double scaleFactor = 1;
120
121 if (source != null)
122
123 {
124
125 scaleFactor = 1 / source.CompositionTarget.TransformFromDevice.M11;
126
127 }
128
129
130
131 Current_Main = main_window;
132
133
134
135 Current_View = new PDFViewWPF();
136
137 Current_View.PixelsPerUnitWidth = scaleFactor;
138
139 Current_View.SetRenderedContentCacheSize(16);
140
141 Current_View.UseLayoutRounding = true;
142
143 _sidecontrol = new SideWindowControl();
144
145
146
147 Current_View.SetAntiAliasing(true);
148
149 Current_View.SetPathHinting(true);
150
151 Current_View.SetThinLineAdjustment(true, true);
152
153 Current_View.SetupThumbnails(false, true, true, 500, 200 * 1024 * 1024, 0.2);
154
155 Current_View.SetProgressiveRendering(true);
156
157 Current_View.SetProgressiveRenderingInterval(750);
158
159 Current_View.SetPageBorderVisibility(true);
160
161
162
163 NaviBarGrid.Children.Add(_sidecontrol);
164
165 PDFGrid.Children.Add(Current_View);
166
167
168
169 _ToolManager = new pdftron.PDF.Tools.ToolManager(Current_View);
170
171 _ToolManager.ToolModeChanged += ToolManager_ToolModeChanged;
172
173 Current_View.Loaded += _pdfviewWpf_Loaded;
174
175 Current_View.OnSetDoc += Current_View_OnSetDoc;
176
177
178
179 Current_View.FindTextFinished += Current_View_FindTextFinished;
180
181 Current_View.LayoutChanged += Current_View_LayoutChanged;
182
183
184
185 Current_View.CurrentScrollChanged += _pdfviewWpf_ScollChanged; // set zoom textbox value
186
187 Current_View.CurrentZoomChanged += _pdfviewWpf_CurrentZoomChanged; // set zoom textbox value
188
189 Current_View.CurrentPageNumberChanged += Current_View_CurrentPageNumberChanged;
190
191 Current_View.SetDownloadReportHandler(OnDownload, null);
192
193
194
195 this.KeyDown += new KeyEventHandler(OnViewKeyDown);
196
197 this.KeyUp += new KeyEventHandler(OnViewKeyUp);
198
199
200
201 //hide sidebar
202
203 main_window.btn_collapse.IsEnabled = false;
204
205 main_window.btn_expand.IsEnabled = true;
206
207 PDFGrid.Margin = new Thickness(0);
208
209 SideWindowSplitter.DragCompleted += SideWindowSplitter_DragCompleted;
210
211
212
213 Current_View.SetPagePresentationMode(PDFViewWPF.PagePresentationMode.e_single_continuous);
214
215 Current_View.SetPageViewMode(PDFViewWPF.PageViewMode.e_fit_width);
216
217 Current_View.SetBackgroundColor(Colors.DarkGray);
218
219 Current_View.SetUrlExtraction(false);
220
221
222
223 // This allows touch interaction
224
225 Current_View.IsManipulationEnabled = true;
226
227
228
229
230
231 this.ToolManager.SelectedAnnotationsChanged += ToolManager_SelectedAnnotationsChanged;
232
233 }
234
235
236
237 private void ToolManager_SelectedAnnotationsChanged()
238
239 {
240
241 List<Annot> annots = ToolManager.SelectedAnnotations;
242
243 }
244
245
246
247 /// <summary>
248
249 /// This will be called each time we switch to this tab, and when we first open it.
250
251 /// </summary>
252
253 /// <param name="sender"></param>
254
255 /// <param name="e"></param>
256
257 private void _pdfviewWpf_Loaded(object sender, RoutedEventArgs e)
258
259 {
260
261 Current_Main.txtbox_pagenum.Text = Current_View.GetCurrentPage().ToString();
262
263 Current_Main.SetZoomValue(Current_View.GetZoom());
264
265 }
266
267
268
269 /// <summary>
270
271 /// This will happen whenever we open a new document.
272
273 /// It is guaranteed to happen after the document is loaded and ready to be used, so we
274
275 /// replicate it here in case there are some timing issues between the loaded handler and he document being loaded.
276
277 /// </summary>
278
279 /// <param name="viewer"></param>
280
281 void Current_View_OnSetDoc(PDFViewWPF viewer)
282
283 {
284
285 Current_Main.txtbox_pagenum.Text = Current_View.GetCurrentPage().ToString();
286
287 Current_Main.slider_zoom.Value = Current_View.GetZoom();
288
289 Current_Main.SetZoomValue(Current_View.GetZoom());
290
291 pdftron.PDF.Action open_action = viewer.GetDoc().GetOpenAction();
292
293 if (open_action.IsValid() && open_action.GetType() == pdftron.PDF.Action.Type.e_JavaScript)
294
295 {
296
297 ActionParameter action_param = new ActionParameter(open_action);
298
299 viewer.ExecuteAction(action_param);
300
301 }
302
303 }
304
305
306
307 #region Events
308
309
310
311 void ToolManager_ToolModeChanged(pdftron.PDF.Tools.ToolManager.ToolType toolType)
312
313 {
314
315 UpdateToolButtonStates();
316
317 }
318
319
320
321 public void UpdateToolButtonStates()
322
323 {
324
325 if (_ToolManager == null)
326
327 {
328
329 return;
330
331 }
332
333
334
335 Current_Main.t_pan.IsChecked = (_ToolManager.CurrentTool.ToolMode == pdftron.PDF.Tools.ToolManager.ToolType.e_pan);
336
337 Current_Main.PanToolMenuItem.IsChecked = (_ToolManager.CurrentTool.ToolMode == pdftron.PDF.Tools.ToolManager.ToolType.e_pan);
338
339
340
341 Current_Main.t_annot_edit.IsChecked = (_ToolManager.CurrentTool.ToolMode == pdftron.PDF.Tools.ToolManager.ToolType.e_annot_edit);
342
343 Current_Main.AnnotationEditToolMenuItem.IsChecked = (_ToolManager.CurrentTool.ToolMode == pdftron.PDF.Tools.ToolManager.ToolType.e_annot_edit);
344
345
346
347 Current_Main.t_text_rect.IsChecked = (_ToolManager.CurrentTool.ToolMode == pdftron.PDF.Tools.ToolManager.ToolType.e_text_select_rectangular);
348
349
350
351 Current_Main.t_text_struct.IsChecked = (_ToolManager.CurrentTool.ToolMode == pdftron.PDF.Tools.ToolManager.ToolType.e_text_select);
352
353
354
355 Current_Main.LineToolMenuItem.IsChecked = (_ToolManager.CurrentTool.ToolMode == pdftron.PDF.Tools.ToolManager.ToolType.e_line_create);
356
357 Current_Main.ArrowToolMenuItem.IsChecked = (_ToolManager.CurrentTool.ToolMode == pdftron.PDF.Tools.ToolManager.ToolType.e_arrow_create);
358
359 Current_Main.RectangleToolMenuItem.IsChecked = (_ToolManager.CurrentTool.ToolMode == pdftron.PDF.Tools.ToolManager.ToolType.e_rect_create);
360
361 Current_Main.OvalToolMenuItem.IsChecked = (_ToolManager.CurrentTool.ToolMode == pdftron.PDF.Tools.ToolManager.ToolType.e_oval_create);
362
363 Current_Main.InkToolMenuItem.IsChecked = (_ToolManager.CurrentTool.ToolMode == pdftron.PDF.Tools.ToolManager.ToolType.e_ink_create);
364
365 Current_Main.TextBoxToolMenuItem.IsChecked = (_ToolManager.CurrentTool.ToolMode == pdftron.PDF.Tools.ToolManager.ToolType.e_text_annot_create);
366
367 Current_Main.CalloutToolMenuItem.IsChecked = (_ToolManager.CurrentTool.ToolMode == pdftron.PDF.Tools.ToolManager.ToolType.e_callout_create);
368
369 Current_Main.StickyNoteToolMenuItem.IsChecked = (_ToolManager.CurrentTool.ToolMode == pdftron.PDF.Tools.ToolManager.ToolType.e_sticky_note_create);
370
371 Current_Main.HighlightToolMenuItem.IsChecked = (_ToolManager.CurrentTool.ToolMode == pdftron.PDF.Tools.ToolManager.ToolType.e_text_highlight);
372
373 Current_Main.UnderlineToolMenuItem.IsChecked = (_ToolManager.CurrentTool.ToolMode == pdftron.PDF.Tools.ToolManager.ToolType.e_text_underline);
374
375 Current_Main.StrikeoutToolMenuItem.IsChecked = (_ToolManager.CurrentTool.ToolMode == pdftron.PDF.Tools.ToolManager.ToolType.e_text_strikeout);
376
377 Current_Main.SquigglyToolMenuItem.IsChecked = (_ToolManager.CurrentTool.ToolMode == pdftron.PDF.Tools.ToolManager.ToolType.e_text_squiggly);
378
379 }
380
381
382
383
384
385 void Current_View_LayoutChanged(PDFViewWPF viewer)
386
387 {
388
389 UpdatePresentationModeButtonStates();
390
391 HighlightSelection();
392
393 }
394
395
396
397 public void UpdatePresentationModeButtonStates()
398
399 {
400
401 Current_Main.SinglePageButton.IsChecked = false;
402
403 Current_Main.SingleContinuousButton.IsChecked = false;
404
405 Current_Main.FacingButton.IsChecked = false;
406
407 Current_Main.FacingContinuousButton.IsChecked = false;
408
409 Current_Main.FacingCoverButton.IsChecked = false;
410
411 Current_Main.FacingCoverContinuousButton.IsChecked = false;
412
413
414
415 switch (Current_View.GetPagePresentationMode())
416
417 {
418
419 case PDFViewWPF.PagePresentationMode.e_single_page:
420
421 Current_Main.SinglePageButton.IsChecked = true;
422
423 break;
424
425 case PDFViewWPF.PagePresentationMode.e_single_continuous:
426
427 Current_Main.SingleContinuousButton.IsChecked = true;
428
429 break;
430
431 case PDFViewWPF.PagePresentationMode.e_facing:
432
433 Current_Main.FacingButton.IsChecked = true;
434
435 break;
436
437 case PDFViewWPF.PagePresentationMode.e_facing_continuous:
438
439 Current_Main.FacingContinuousButton.IsChecked = true;
440
441 break;
442
443 case PDFViewWPF.PagePresentationMode.e_facing_cover:
444
445 Current_Main.FacingCoverButton.IsChecked = true;
446
447 break;
448
449 case PDFViewWPF.PagePresentationMode.e_facing_continuous_cover:
450
451 Current_Main.FacingCoverContinuousButton.IsChecked = true;
452
453 break;
454
455 }
456
457 }
458
459
460
461
462
463 void SideWindowSplitter_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
464
465 {
466
467 Current_Main.UpdateSidewindowButtons();
468
469 }
470
471
472
473
474
475 public void SetCurrentPage(int page)
476
477 {
478
479 Current_View.CurrentPageNumber = page;
480
481 //Current_View.SetCurrentPage(page);
482
483 }
484
485
486
487 void _pdfviewWpf_CurrentZoomChanged(PDFViewWPF viewer)
488
489 {
490
491 double zoom = Current_View.GetZoom();
492
493 Current_Main.SetZoomValue(zoom);
494
495 HighlightSelection();
496
497 }
498
499
500
501 void _pdfviewWpf_ScollChanged(PDFViewWPF viewer, ScrollChangedEventArgs e)
502
503 {
504
505 //add you code here
506
507 }
508
509
510
511 void Current_View_CurrentPageNumberChanged(PDFViewWPF viewer, int currentPage, int totalPages)
512
513 {
514
515 Current_Main.txtbox_pagenum.Text = currentPage.ToString();
516
517 HighlightSelection();
518
519 }
520
521
522
523 void txtbox_pagenum_KeyDown(object sender, KeyEventArgs e)
524
525 {
526
527 if (e.Key == Key.Enter && !string.IsNullOrWhiteSpace(Current_Main.txtbox_pagenum.Text))
528
529 {
530
531 int pagenum;
532
533 if (Int32.TryParse(Current_Main.txtbox_pagenum.Text, out pagenum))
534
535 Current_View.SetCurrentPage(pagenum);
536
537 }
538
539 }
540
541
542
543 private void txtbox_zoom_KeyDown(object sender, KeyEventArgs e)
544
545 {
546
547 if (e.Key == Key.Enter && !string.IsNullOrWhiteSpace(Current_Main.txtbox_zoom.Text))
548
549 {
550
551 int newzoom;
552
553 if (Int32.TryParse(Current_Main.txtbox_zoom.Text, out newzoom))
554
555 Current_View.SetZoom(newzoom / 100.0);
556
557 }
558
559 }
560
561
562
563 private void OnViewKeyDown(object sender, KeyEventArgs e)
564
565 {
566
567 if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
568
569 {
570
571 if (e.Key == Key.F)
572
573 {
574
575 if (Current_Main.FindTextDialog == null)
576
577 {
578
579 Current_Main.FindTextDialog = new FindTextDialog();
580
581 Current_Main.FindTextDialog.Owner = Current_Main;
582
583 Current_Main.FindTextDialog.Show();
584
585 }
586
587 else
588
589 {
590
591 Current_Main.FindTextDialog.ActivateFindText();
592
593 }
594
595 }
596
597 if (e.Key == Key.S)
598
599 {
600
601 if (_pdfdoc != null && Current_View != null && (_pdfdoc.IsModified()))
602
603 this.Save(_pdfdoc.GetFileName());
604
605 }
606
607 }
608
609 }
610
611
612
613 void OnViewKeyUp(object sender, KeyEventArgs e)
614
615 {
616
617
618
619 }
620
621
622
623 public void OpenNavibar()
624
625 {
626
627 DocViewerGrid.ColumnDefinitions[0].Width = new GridLength(200);
628
629
630
631 Current_Main.btn_expand.IsEnabled = false;
632
633 Current_Main.btn_collapse.IsEnabled = true;
634
635 IsNaviOpen = true;
636
637 }
638
639
640
641 public void CloseNavibar()
642
643 {
644
645 _oldwidth = DocViewerGrid.ColumnDefinitions[0].Width;
646
647 DocViewerGrid.ColumnDefinitions[0].Width = new GridLength(0);
648
649
650
651 Current_Main.btn_collapse.IsEnabled = false;
652
653 Current_Main.btn_expand.IsEnabled = true;
654
655 IsNaviOpen = false;
656
657 }
658
659
660
661 #endregion
662
663
664
665 #region Methods
666
667 public bool OpenPDF(String filename)
668
669 {
670
671 try
672
673 {
674
675 try
676
677 {
678
679 _pdfdoc = new PDFDoc(filename);
680
681 }
682
683 catch (Exception ex)
684
685 {
686
687 _pdfdoc = OpenImage(filename);
688
689 if (_pdfdoc == null)
690
691 throw ex;
692
693 }
694
695
696
697 if (!_pdfdoc.InitSecurityHandler())
698
699 {
700
701 MessageBox.Show("Document authentication error", "PDFViewWPF Error");
702
703 return false;
704
705 }
706
707 Current_View.SetPagePresentationMode(PDFViewWPF.PagePresentationMode.e_single_continuous);
708
709 Current_View.SetPageViewMode(PDFViewWPF.PageViewMode.e_fit_width);
710
711 Current_View.SetUrlExtraction(false);
712
713
714
715 if (_pdfdoc.GetPageCount() > 2000)
716
717 {
718
719 Current_View.SetPagePresentationMode(PDFViewWPF.PagePresentationMode.e_single_page);
720
721 }
722
723 Current_View.SetDoc(_pdfdoc);
724
725 _sidecontrol.CreateSideWindow(Current_View, _ToolManager);
726
727 }
728
729 catch (PDFNetException ex)
730
731 {
732
733 MessageBox.Show(ex.Message);
734
735 return false;
736
737 }
738
739 catch (Exception ex)
740
741 {
742
743 MessageBox.Show(ex.ToString());
744
745 return false;
746
747 }
748
749
750
751 return true;
752
753 }
754
755
756
757 // A utility function used to display other images types besides PDF (TIF, JPEG, BMG,PNG, etc)
758
759 public PDFDoc OpenImage(string filename)
760
761 {
762
763 try
764
765 {
766
767 PDFDoc pdfDoc = new PDFDoc();
768
769 ElementBuilder f = new ElementBuilder();
770
771 ElementWriter writer = new ElementWriter();
772
773
774
775 pdftron.PDF.Page page = pdfDoc.PageCreate();
776
777 writer.Begin(page);
778
779
780
781 // Add image to the document
782
783 pdftron.PDF.Image img = pdftron.PDF.Image.Create(pdfDoc, filename);
784
785
786
787 pdftron.PDF.Rect imgBox = new pdftron.PDF.Rect(0, 0, img.GetImageWidth(), img.GetImageHeight());
788
789 pdftron.PDF.Rect scaledBox = new pdftron.PDF.Rect();
790
791 double scaleFactor;
792
793 if (imgBox.Height() / imgBox.Width() > 792 / 612)
794
795 scaleFactor = imgBox.Height() / 792;
796
797 else
798
799 scaleFactor = imgBox.Width() / 612;
800
801 scaledBox.x2 = imgBox.x2 / scaleFactor;
802
803 scaledBox.y2 = imgBox.y2 / scaleFactor;
804
805
806
807 // set crop and media box of this page to fit with the scaled image
808
809 page.SetCropBox(scaledBox);
810
811 page.SetMediaBox(scaledBox);
812
813
814
815 // create the image element and add it to the page
816
817 int width = (int)scaledBox.Width();
818
819 int height = (int)scaledBox.Height();
820
821 int offsetX = 0;
822
823 int offsetY = 0;
824
825
826
827 Element element = f.CreateImage(img, new Matrix2D(width, 0, 0, height, offsetX, offsetY));
828
829 writer.WritePlacedElement(element);
830
831 writer.End(); // Finish writing to the page
832
833
834
835 pdfDoc.PagePushBack(page);
836
837 return pdfDoc;
838
839 }
840
841 catch (Exception)
842
843 {
844
845 return null;
846
847 }
848
849 }
850
851
852
853 public bool OpenURL(String url, String password)
854
855 {
856
857 try
858
859 {
860
861 // Open a PDF file at the given url. This works best with PDF's that
862
863 // are linearized, as pages can be downloaded and viewed in random access order,
864
865 // without the need to download the entire document. A viewing session can also be
866
867 // persisted across multiple viewing/application sessions to remove redundant downloads
868
869 // and improve overall performance by using the optional cache_file parameter.
870
871 _ToolManager.IsEnabled = false;
872
873 Current_View.OpenURLAsync(url, "", password);
874
875 }
876
877 catch (PDFNetException ex)
878
879 {
880
881 _ToolManager.IsEnabled = true;
882
883 MessageBox.Show(ex.Message);
884
885 return false;
886
887 }
888
889 catch (Exception ex)
890
891 {
892
893 _ToolManager.IsEnabled = true;
894
895 MessageBox.Show(ex.ToString());
896
897 return false;
898
899 }
900
901 return true;
902
903 }
904
905
906
907 public PDFDoc GetPDFDoc()
908
909 {
910
911 if (_pdfdoc == null) return null;
912
913 else return Current_View.GetDoc();
914
915 }
916
917
918
919 public void SetPageViewMode(PDFViewWPF.PageViewMode mode)
920
921 {
922
923 Current_View.SetPageViewMode(mode);
924
925 }
926
927
928
929 public PDFViewWPF.PageViewMode GetPageViewMode()
930
931 {
932
933 return Current_View.GetPageViewMode();
934
935 }
936
937
938
939 public void FitPage()
940
941 {
942
943 Current_View.SetPageViewMode(PDFViewWPF.PageViewMode.e_fit_page);
944
945 }
946
947
948
949 public void FitWidth()
950
951 {
952
953 Current_View.SetPageViewMode(PDFViewWPF.PageViewMode.e_fit_width);
954
955 }
956
957
958
959 public double GetZoom()
960
961 {
962
963 return Current_View.GetZoom();
964
965 }
966
967
968
969 public void SetZoom(double zoom)
970
971 {
972
973 Current_View.SetZoom(zoom);
974
975 }
976
977
978
979 public void ZoomIn()
980
981 {
982
983 Current_View.SetZoom(Current_View.GetZoom() * 1.25);
984
985 }
986
987
988
989 public void ZoomOut()
990
991 {
992
993 Current_View.SetZoom(Current_View.GetZoom() / 1.25);
994
995 }
996
997
998
999 public int GetCurrentPageNumber()
1000
1001 {
1002
1003 return Current_View.GetCurrentPage();
1004
1005 }
1006
1007
1008
1009 public void FirstPage()
1010
1011 {
1012
1013 Current_View.GotoFirstPage();
1014
1015 }
1016
1017
1018
1019 public void PrevPage()
1020
1021 {
1022
1023 Current_View.GotoPreviousPage();
1024
1025 }
1026
1027
1028
1029 public void NextPage()
1030
1031 {
1032
1033 Current_View.GotoNextPage();
1034
1035 }
1036
1037
1038
1039 public void LastPage()
1040
1041 {
1042
1043 Current_View.GotoLastPage();
1044
1045 }
1046
1047
1048
1049 public void PageSingle()
1050
1051 {
1052
1053 Current_View.SetPagePresentationMode(PDFViewWPF.PagePresentationMode.e_single_page);
1054
1055 }
1056
1057
1058
1059 public void PageSingleContinuous()
1060
1061 {
1062
1063 Current_View.SetPagePresentationMode(PDFViewWPF.PagePresentationMode.e_single_continuous);
1064
1065 }
1066
1067
1068
1069 public void PageFacingContinuous()
1070
1071 {
1072
1073 Current_View.SetPagePresentationMode(PDFViewWPF.PagePresentationMode.e_facing_continuous);
1074
1075 }
1076
1077
1078
1079 public void PageFacing()
1080
1081 {
1082
1083 Current_View.SetPagePresentationMode(PDFViewWPF.PagePresentationMode.e_facing);
1084
1085 }
1086
1087
1088
1089 public void PageFacingCover()
1090
1091 {
1092
1093 Current_View.SetPagePresentationMode(PDFViewWPF.PagePresentationMode.e_facing_cover);
1094
1095 }
1096
1097
1098
1099 public void PageFacingCoverContinous()
1100
1101 {
1102
1103 Current_View.SetPagePresentationMode(PDFViewWPF.PagePresentationMode.e_facing_continuous_cover);
1104
1105 }
1106
1107
1108
1109 public void RotateClockwise()
1110
1111 {
1112
1113 Current_View.RotateClockwise();
1114
1115 Current_View.UpdatePageLayout();
1116
1117 Current_View.Update();
1118
1119 }
1120
1121
1122
1123 public void RotateCounterClockwise()
1124
1125 {
1126
1127 Current_View.RotateCounterClockwise();
1128
1129 //Current_View.UpdatePageLayout();
1130
1131 //Current_View.Update();
1132
1133 }
1134
1135
1136
1137 public pdftron.PDF.Page.Rotate GetRotation()
1138
1139 {
1140
1141 return Current_View.GetRotation();
1142
1143 }
1144
1145
1146
1147 public void SetAntiAliasing(bool anti_alias)
1148
1149 {
1150
1151 Current_View.SetAntiAliasing(anti_alias);
1152
1153 Current_View.Update();
1154
1155 }
1156
1157
1158
1159 public void SetRasterizer(bool built_in)
1160
1161 {
1162
1163 if (built_in)
1164
1165 Current_View.SetRasterizerType(PDFRasterizer.Type.e_BuiltIn);
1166
1167 else
1168
1169 Current_View.SetRasterizerType(PDFRasterizer.Type.e_GDIPlus);
1170
1171
1172
1173 Current_View.Update();
1174
1175 }
1176
1177
1178
1179 public void SetSmoothImages(bool smooth_images)
1180
1181 {
1182
1183 Current_View.SetImageSmoothing(smooth_images);
1184
1185 Current_View.Update();
1186
1187 }
1188
1189
1190
1191 /// <summary>
1192
1193 ///
1194
1195 /// </summary>
1196
1197 /// <param name="filename">The path to save the file to</param>
1198
1199 /// <param name="force">true if we should save, whether or not it is modified</param>
1200
1201 public void Save(string filename, bool force = false)
1202
1203 {
1204
1205 if (_pdfdoc == null) return;
1206
1207
1208
1209 System.IO.FileInfo fi = new System.IO.FileInfo(filename);
1210
1211 if (fi.Exists && fi.IsReadOnly)
1212
1213 {
1214
1215 MessageBox.Show("This is a read only file. Please choose Save As and enter a new file name to save.", "Error during the Save");
1216
1217 return;
1218
1219 }
1220
1221
1222
1223 bool locked = false;
1224
1225 try
1226
1227 {
1228
1229 bool shouldSave = force;
1230
1231 if (!shouldSave)
1232
1233 {
1234
1235 Current_View.DocLock(true);
1236
1237 locked = true;
1238
1239 if (_pdfdoc.IsModified())
1240
1241 {
1242
1243 Current_View.DocUnlock();
1244
1245 locked = false;
1246
1247 shouldSave = true;
1248
1249 }
1250
1251 }
1252
1253 if (shouldSave)
1254
1255 {
1256
1257 Current_View.CancelRendering();
1258
1259 _pdfdoc.Save(filename, pdftron.SDF.SDFDoc.SaveOptions.e_incremental);
1260
1261 }
1262
1263
1264
1265 }
1266
1267 catch (Exception ex)
1268
1269 {
1270
1271 MessageBox.Show(ex.ToString(), "Error during the Save");
1272
1273 }
1274
1275 finally
1276
1277 {
1278
1279 if (locked)
1280
1281 {
1282
1283 Current_View.DocUnlock();
1284
1285 }
1286
1287 }
1288
1289 }
1290
1291
1292
1293 public string SaveAs()
1294
1295 {
1296
1297 if (_pdfdoc != null && Current_View != null)
1298
1299 {
1300
1301 Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
1302
1303 dlg.Filter = "PDF Files (*.pdf)|*.pdf|All Files (*.*)|*.*";
1304
1305 dlg.DefaultExt = ".pdf";
1306
1307 dlg.FileName = _pdfdoc.GetFileName();
1308
1309
1310
1311 if (dlg.ShowDialog() == true)
1312
1313 {
1314
1315 this.Save(dlg.FileName);
1316
1317 return dlg.SafeFileName;
1318
1319 }
1320
1321 }
1322
1323 return null;
1324
1325 }
1326
1327
1328
1329 public bool CloseFile()
1330
1331 {
1332
1333 if (_pdfdoc != null && Current_View != null && _pdfdoc.IsModified())
1334
1335 {
1336
1337 string messageBoxText = "Would you like to save changes to " + _safeDocName + "?";
1338
1339 string caption = "PDFViewWPF";
1340
1341 MessageBoxButton button = MessageBoxButton.YesNoCancel;
1342
1343 MessageBoxImage icon = MessageBoxImage.Question;
1344
1345 MessageBoxResult defaultResult = MessageBoxResult.Yes;
1346
1347 MessageBoxOptions options = MessageBoxOptions.DefaultDesktopOnly;
1348
1349
1350
1351 MessageBoxResult result;
1352
1353 result = MessageBox.Show(messageBoxText, caption, button, icon, defaultResult, options);
1354
1355
1356
1357 if (result == MessageBoxResult.Yes)
1358
1359 {
1360
1361 this.Save(_pdfdoc.GetFileName());
1362
1363 Dispose();
1364
1365 return true;
1366
1367 }
1368
1369 else if (result == MessageBoxResult.No)
1370
1371 {
1372
1373 Dispose();
1374
1375 return true;
1376
1377 }
1378
1379 else
1380
1381 return false;
1382
1383 }
1384
1385 else
1386
1387 {
1388
1389 Dispose();
1390
1391 return true;
1392
1393 }
1394
1395 }
1396
1397
1398
1399 public void Dispose()
1400
1401 {
1402
1403 Current_View.CloseDoc();
1404
1405 Current_View.Dispose();
1406
1407 if(_pdfdoc != null) _pdfdoc.Dispose();
1408
1409 }
1410
1411
1412
1413 public void Activate()
1414
1415 {
1416
1417 Current_View.Activate();
1418
1419 _sidecontrol.Activate();
1420
1421 }
1422
1423
1424
1425 public void Deactivate()
1426
1427 {
1428
1429 Current_View.Deactivate();
1430
1431 _sidecontrol.Deactivate();
1432
1433 }
1434
1435
1436
1437 //public void SetToolMode(PDFViewWPF.ToolMode tool_mode)
1438
1439 //{
1440
1441 // Current_View.SetToolMode(tool_mode);
1442
1443 //}
1444
1445
1446
1447 public void CopySelectedText()
1448
1449 {
1450
1451 Current_View.CopySelectedTextToClipboardAsUnicode();
1452
1453 }
1454
1455
1456
1457 public void SelectAll()
1458
1459 {
1460
1461 _ToolManager.CurrentTool.SelectAll();
1462
1463 }
1464
1465
1466
1467 public void DeselectAll()
1468
1469 {
1470
1471 _ToolManager.CurrentTool.DeselectAll();
1472
1473 }
1474
1475
1476
1477 public void FindText(string str, bool match_case, bool match_whole_word, bool search_up, bool reg_exp)
1478
1479 {
1480
1481 Current_View.FindText(str, match_case, match_whole_word, search_up, reg_exp); // Use the build in Find text dialog.
1482
1483 }
1484
1485
1486
1487
1488
1489 void Current_View_FindTextFinished(PDFViewWPF viewer, bool found, PDFViewWPF.Selection selection)
1490
1491 {
1492
1493 Current_Main.FindTextFinishedHandler(viewer, found, selection);
1494
1495 _currentSearchSelection = selection;
1496
1497 HighlightSelection();
1498
1499 viewer.ClearSelection();
1500
1501 _ToolManager.ClearSelectedTextHighlighting();
1502
1503 }
1504
1505
1506
1507 /// <summary>
1508
1509 /// Highlights the search result if any
1510
1511 /// </summary>
1512
1513 /// <param name="result">A text selection acquired by mPDFView.FindText</param>
1514
1515 /// <returns>true if and only if the selections contains at least one highlight</returns>
1516
1517 private bool HighlightSelection()
1518
1519 {
1520
1521 ClearSearchSelection();
1522
1523 if (_currentSearchSelection == null)
1524
1525 {
1526
1527 return false;
1528
1529 }
1530
1531
1532
1533 double[] quads = _currentSearchSelection.GetQuads();
1534
1535 int numQuads = quads.Length / 8;
1536
1537 int pageNumber = _currentSearchSelection.GetPageNum();
1538
1539 int quadNumber = 0;
1540
1541 int[] visiblePages = Current_View.GetVisiblePages();
1542
1543 if (!visiblePages.Contains(pageNumber))
1544
1545 {
1546
1547 return false;
1548
1549 }
1550
1551
1552
1553 List<System.Windows.Rect> rects = new List<System.Windows.Rect>();
1554
1555
1556
1557 // get highlights in control (screen) space
1558
1559 for (int i = 0; i < numQuads; i++)
1560
1561 {
1562
1563 quadNumber = i * 8;
1564
1565 double x1 = quads[quadNumber + 0];
1566
1567 double y1 = quads[quadNumber + 1];
1568
1569
1570
1571 double x2 = quads[quadNumber + 2];
1572
1573 double y2 = quads[quadNumber + 3];
1574
1575
1576
1577 double x3 = quads[quadNumber + 4];
1578
1579 double y3 = quads[quadNumber + 5];
1580
1581
1582
1583 double x4 = quads[quadNumber + 6];
1584
1585 double y4 = quads[quadNumber + 7];
1586
1587
1588
1589 Current_View.ConvPagePtToScreenPt(ref x1, ref y1, pageNumber);
1590
1591 Current_View.ConvPagePtToScreenPt(ref x2, ref y2, pageNumber);
1592
1593 Current_View.ConvPagePtToScreenPt(ref x3, ref y3, pageNumber);
1594
1595 Current_View.ConvPagePtToScreenPt(ref x4, ref y4, pageNumber);
1596
1597
1598
1599 double left, right, top, bottom;
1600
1601
1602
1603 left = Math.Min(x1, Math.Min(x2, Math.Min(x3, x4)));
1604
1605 right = Math.Max(x1, Math.Max(x2, Math.Max(x3, x4)));
1606
1607 top = Math.Min(y1, Math.Min(y2, Math.Min(y3, y4)));
1608
1609 bottom = Math.Max(y1, Math.Max(y2, Math.Max(y3, y4)));
1610
1611
1612
1613 rects.Add(new System.Windows.Rect(left, top, right - left, bottom - top));
1614
1615 }
1616
1617
1618
1619 Canvas annotCanvas = Current_View.GetCanvas();
1620
1621
1622
1623 // add highlight(s) to annotation canvas
1624
1625 foreach (System.Windows.Rect rect in rects)
1626
1627 {
1628
1629 Rectangle highlight = new Rectangle();
1630
1631 highlight.Fill = new SolidColorBrush() { Color = Colors.Blue };
1632
1633 highlight.Fill.Opacity = 0.3;
1634
1635 highlight.Width = rect.Width;
1636
1637 highlight.Height = rect.Height;
1638
1639 Canvas.SetLeft(highlight, rect.Left + Current_View.GetHScrollPos());
1640
1641 Canvas.SetTop(highlight, rect.Top + Current_View.GetVScrollPos());
1642
1643 annotCanvas.Children.Add(highlight);
1644
1645 _onScreenSelection.Add(highlight);
1646
1647 }
1648
1649
1650
1651 return numQuads > 0;
1652
1653 }
1654
1655
1656
1657 /// <summary>
1658
1659 /// Removes the rectangles highlighting the selection, but keeps the selection for future reference
1660
1661 /// to be used with HighlightSelection
1662
1663 /// </summary>
1664
1665 /// <param name="deleteSelection">If true, will also delete the underlaying selection</param>
1666
1667 public void ClearSearchSelection(bool deleteSelection = false)
1668
1669 {
1670
1671 foreach (Rectangle rect in _onScreenSelection)
1672
1673 {
1674
1675 Canvas parent = rect.Parent as Canvas;
1676
1677 if (parent != null)
1678
1679 {
1680
1681 parent.Children.Remove(rect);
1682
1683 }
1684
1685 }
1686
1687 if (deleteSelection)
1688
1689 {
1690
1691 _currentSearchSelection = null;
1692
1693 }
1694
1695 }
1696
1697
1698
1699 public int GetPageCount()
1700
1701 {
1702
1703 return Current_View.GetPageCount();
1704
1705 }
1706
1707
1708
1709 private void OnDownload(DownloadedType type, Int32 page_num, Int32 obj_num, String msg, Object obj)
1710
1711 {
1712
1713 switch (type)
1714
1715 {
1716
1717 case DownloadedType.e_opened:
1718
1719 // e_opened indicates that we have a valid, but incomplete PDFDoc.
1720
1721 _pdfdoc = Current_View.GetDoc();
1722
1723 if (_pdfdoc.GetPageCount() > 2000)
1724
1725 {
1726
1727 Current_View.SetPagePresentationMode(PDFViewWPF.PagePresentationMode.e_single_page);
1728
1729 }
1730
1731 _sidecontrol.CreateSideWindow(Current_View, _ToolManager);
1732
1733 // the PDF should be treated as read only, and only simple functions
1734
1735 // should be called on the doc, until e_finished has been called.
1736
1737 break;
1738
1739 case DownloadedType.e_page:
1740
1741 // this indicates the entire page is downloaded and it is safe to modify.
1742
1743 // for example add a new annotation
1744
1745 break;
1746
1747 case DownloadedType.e_finished:
1748
1749 // we now have the full PDF file and it can be treated like any other
1750
1751 _ToolManager.IsEnabled = true;
1752
1753 if (MessageBox.Show("Download complete, would you like to save the PDF locally?",
1754
1755 "PDF Downloaded", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
1756
1757 {
1758
1759 SaveAs();
1760
1761 }
1762
1763 break;
1764
1765 case DownloadedType.e_failed:
1766
1767 // downloading has stopped if this occurs
1768
1769 _ToolManager.IsEnabled = true;
1770
1771 MessageBox.Show(msg);
1772
1773 Current_Main.CloseViewer(this);
1774
1775 break;
1776
1777 }
1778
1779 }
1780
1781
1782
1783 #endregion
1784
1785 }
1786
1787}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales