PDFViewWPFSimple

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
27using System.Windows.Controls.Primitives;
28
29using System.Runtime.InteropServices;
30
31using System.Printing;
32
33
34
35using pdftron;
36
37using pdftron.PDF;
38
39using pdftron.SDF;
40
41using pdftron.Filters;
42
43using pdftron.Common;
44
45using System.ComponentModel;
46
47
48
49namespace PDFViewWPFTestCS
50
51{
52
53 /// <summary>
54
55 /// Interaction logic for MainWindow.xaml
56
57 /// </summary>
58
59 public partial class MainWindow : Window
60
61 {
62
63 private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
64
65
66
67 private bool _sliderShouldForwardValue = true;
68
69 private PDFViewViewer _currentViewer;
70
71 private TabItem _currentTab;
72
73
74
75 FindTextDialog _findTextDialog;
76
77 public FindTextDialog FindTextDialog
78
79 {
80
81 get { return _findTextDialog; }
82
83 set { _findTextDialog = value; }
84
85 }
86
87
88
89 public struct PrinterArguments
90
91 {
92
93 public PDFDoc DocToPrint;
94
95 public string PrinterName;
96
97 public string FileName;
98
99 public PageSet PageSet;
100
101 public PrinterMode PrinterMode;
102
103 public pdftron.PDF.OCG.Context OCG;
104
105
106
107 public PrinterArguments(PDFDoc docToPrint, string printerName, string fileName, PageSet pageSet, PrinterMode printerMode, pdftron.PDF.OCG.Context ocg)
108
109 {
110
111 this.DocToPrint = docToPrint;
112
113 this.PrinterName = printerName;
114
115 this.FileName = fileName;
116
117 this.PageSet = pageSet;
118
119 this.PrinterMode = printerMode;
120
121 this.OCG = ocg;
122
123 }
124
125 }
126
127
128
129 public MainWindow()
130
131 {
132
133 // Initialize PDFNet.
134
135 try
136
137 {
138
139 PDFNet.Initialize();
140
141 PDFNet.EnableJavaScript(true);
142
143 PDFNet.SetViewerCache(100 * 1024 * 1024, false);
144
145 }
146
147 catch (pdftron.Common.PDFNetException ex)
148
149 {
150
151 MessageBox.Show(ex.GetMessage(), "Licensing Error");
152
153 }
154
155
156
157 PDFViewWPFSecurityHandler.MainWindow = this;
158
159 pdftron.SDF.CreateDelegate del = new pdftron.SDF.CreateDelegate(PDFViewWPFSecurityHandler.Create);
160
161 pdftron.SDF.SecurityManagerSingleton.Instance().RegisterSecurityHandler("Standard",
162
163 new pdftron.SDF.SecurityDescriptor("Standard Security", del));
164
165 InitializeComponent();
166
167
168
169 slider_zoom.ValueChanged += slider_zoom_ValueChanged;
170
171 txtbox_zoom.KeyDown += txtbox_zoom_KeyDown;
172
173 txtbox_pagenum.KeyDown += txtbox_pagenum_KeyDown;
174
175 }
176
177
178
179 //void toolbar_footer_Loaded(object sender, RoutedEventArgs e)
180
181 //{
182
183 // var overflowGrid = toolbar_footer.Template.FindName("OverflowGrid", toolbar_footer) as FrameworkElement;
184
185 // if (overflowGrid != null)
186
187 // {
188
189 // overflowGrid.Visibility = Visibility.Collapsed;
190
191 // }
192
193 //}
194
195
196
197 #region Methods
198
199
200
201 public PDFViewViewer GetCurrentViewer()
202
203 {
204
205 PDFViewViewer view = null;
206
207 for (int i = 0; i < PDFTab.Items.Count; ++i)
208
209 {
210
211 TabItem current = (TabItem)PDFTab.Items.GetItemAt(i);
212
213 if (current.IsSelected)
214
215 {
216
217 StackPanel sp = current.Header as StackPanel;
218
219 sp.Children[1].Visibility = Visibility.Visible;
220
221 Grid g = (Grid)current.Content;
222
223 view = (PDFViewViewer)g.Children[0];
224
225 _currentTab = current;
226
227 break;
228
229 }
230
231 }
232
233 return view;
234
235 }
236
237
238
239 private TabItem GetCurrentTab()
240
241 {
242
243 TabItem item = null;
244
245 for (int i = 0; i < PDFTab.Items.Count; ++i)
246
247 {
248
249 item = (TabItem)PDFTab.Items.GetItemAt(i);
250
251 if (item.IsSelected)
252
253 {
254
255 break;
256
257 }
258
259 }
260
261
262
263 return item;
264
265 }
266
267
268
269 public void FindText(string str, bool match_case, bool match_whole_word, bool search_up, bool reg_exp)
270
271 {
272
273 PDFViewViewer view = GetCurrentViewer();
274
275 if (view != null)
276
277 {
278
279 view.FindText(str, match_case, match_whole_word, search_up, reg_exp); // Use the build in Find text dialog.
280
281 }
282
283 return;
284
285 }
286
287
288
289
290
291 public void UpdateSidewindowButtons()
292
293 {
294
295 PDFViewViewer view = GetCurrentViewer();
296
297 if (view == null)
298
299 {
300
301 return;
302
303 }
304
305 double length = view.NaviWidth;
306
307 if (length > 10)
308
309 {
310
311 btn_collapse.IsEnabled = true;
312
313 }
314
315 else
316
317 {
318
319 btn_collapse.IsEnabled = false;
320
321 }
322
323 if (length < 200)
324
325 {
326
327 btn_expand.IsEnabled = true;
328
329 }
330
331 else
332
333 {
334
335 btn_expand.IsEnabled = false;
336
337 }
338
339 }
340
341
342
343 public void UpdateZoomState()
344
345 {
346
347 PDFViewViewer view = GetCurrentViewer();
348
349 if (view == null)
350
351 {
352
353 return;
354
355 }
356
357 SetZoomValue(view.ZoomLevel);
358
359
360
361 }
362
363
364
365 // Let's a viewer tell the mainwindow that it can be closed (e.g. open URL failed).
366
367 public void CloseViewer(PDFViewViewer viewer)
368
369 {
370
371 // Find the tab with the viewer
372
373 TabItem itemToDelete = null;
374
375 foreach (TabItem item in PDFTab.Items)
376
377 {
378
379 Grid g = item.Content as Grid;
380
381 if (g != null && g.Children.Contains(viewer))
382
383 {
384
385 itemToDelete = item;
386
387 break;
388
389 }
390
391 }
392
393
394
395 if (itemToDelete != null)
396
397 {
398
399 itemToDelete.Content = null;
400
401 PDFTab.Items.Remove(itemToDelete);
402
403 }
404
405 }
406
407
408
409 public void SetZoomValue(double zoom)
410
411 {
412
413 _sliderShouldForwardValue = false;
414
415 slider_zoom.Value = zoom;
416
417 _sliderShouldForwardValue = true;
418
419 string.Format("{0,0:1}", zoom);
420
421 txtbox_zoom.Text = string.Format("{0:0.#}", zoom * 100);
422
423 }
424
425
426
427 #endregion
428
429
430
431 #region Events
432
433
434
435 public void FindTextFinishedHandler(PDFViewWPF viewer, bool found, PDFViewWPF.Selection selection)
436
437 {
438
439 if (_findTextDialog != null)
440
441 {
442
443 _findTextDialog.TextSearchFinished(viewer, found, selection);
444
445 }
446
447 }
448
449
450
451 private void PDFTab_SelectionChanged(object sender, SelectionChangedEventArgs e)
452
453 {
454
455 PDFViewViewer newViewer = GetCurrentViewer();
456
457
458
459 if (_currentViewer == newViewer)
460
461 {
462
463 return;
464
465 }
466
467
468
469 if (_currentViewer != null)
470
471 {
472
473 _currentViewer.ClearSearchSelection(true);
474
475 _currentViewer.Deactivate();
476
477 }
478
479 if (_currentTab != null)
480
481 {
482
483 StackPanel sp = _currentTab.Header as StackPanel;
484
485 sp.Children[1].Visibility = Visibility.Collapsed;
486
487 }
488
489 _currentViewer = GetCurrentViewer();
490
491 if (_currentViewer != null && _currentViewer.GetPDFDoc() != null)
492
493 {
494
495 toolbar_footer.IsEnabled = true;
496
497 toolbar_header2.IsEnabled = true;
498
499 toolbar_header3.IsEnabled = true;
500
501 btn_save.IsEnabled = true;
502
503 btn_print.IsEnabled = true;
504
505 txt_pagecount.Content = " / " + _currentViewer.GetPageCount().ToString();
506
507 txtbox_pagenum.Text = _currentViewer.GetCurrentPageNumber().ToString();
508
509 _currentViewer.UpdateToolButtonStates();
510
511 _currentViewer.UpdatePresentationModeButtonStates();
512
513
514
515 UpdateSidewindowButtons();
516
517 UpdateZoomState();
518
519
520
521 // set main window title
522
523 this.Title = _currentViewer.GetPDFDoc().GetFileName();
524
525 _currentViewer.Activate();
526
527 }
528
529 else
530
531 {
532
533 slider_zoom.Value = slider_zoom.Minimum;
534
535 txtbox_zoom.Text = "";
536
537 txtbox_pagenum.Text = "";
538
539 txt_pagecount.Content = "";
540
541 toolbar_footer.IsEnabled = false;
542
543 toolbar_header2.IsEnabled = false;
544
545 toolbar_header3.IsEnabled = false;
546
547 btn_save.IsEnabled = false;
548
549 btn_print.IsEnabled = false;
550
551 this.Title = "PDFViewWPF";
552
553
554
555 // No page presentation mode selected
556
557 SinglePageButton.IsChecked = false;
558
559 SingleContinuousButton.IsChecked = false;
560
561 FacingButton.IsChecked = false;
562
563 FacingContinuousButton.IsChecked = false;
564
565 FacingCoverButton.IsChecked = false;
566
567 FacingCoverContinuousButton.IsChecked = false;
568
569
570
571 // No tool mode selected
572
573 t_pan.IsChecked = false;
574
575 PanToolMenuItem.IsChecked = false;
576
577
578
579 t_annot_edit.IsChecked = false;
580
581 AnnotationEditToolMenuItem.IsChecked = false;
582
583
584
585 t_text_rect.IsChecked = false;
586
587
588
589 t_text_struct.IsChecked = false;
590
591
592
593 LineToolMenuItem.IsChecked = false;
594
595 ArrowToolMenuItem.IsChecked = false;
596
597 RectangleToolMenuItem.IsChecked = false;
598
599 OvalToolMenuItem.IsChecked = false;
600
601 InkToolMenuItem.IsChecked = false;
602
603 TextBoxToolMenuItem.IsChecked = false;
604
605 CalloutToolMenuItem.IsChecked = false;
606
607 StickyNoteToolMenuItem.IsChecked = false;
608
609 HighlightToolMenuItem.IsChecked = false;
610
611 UnderlineToolMenuItem.IsChecked = false;
612
613 StrikeoutToolMenuItem.IsChecked = false;
614
615 SquigglyToolMenuItem.IsChecked = false;
616
617
618
619 }
620
621 }
622
623
624
625
626
627 private void slider_zoom_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
628
629 {
630
631 if (_sliderShouldForwardValue)
632
633 {
634
635 PDFViewViewer view = GetCurrentViewer();
636
637 if (view != null)
638
639 {
640
641 view.ZoomLevel = slider_zoom.Value;
642
643 }
644
645 }
646
647 }
648
649
650
651 void txtbox_pagenum_KeyDown(object sender, KeyEventArgs e)
652
653 {
654
655 if (e.Key == Key.Enter && !string.IsNullOrWhiteSpace(txtbox_pagenum.Text))
656
657 {
658
659 int pagenum;
660
661 if (Int32.TryParse(txtbox_pagenum.Text, out pagenum))
662
663 {
664
665 PDFViewViewer view = GetCurrentViewer();
666
667 if (view != null)
668
669 {
670
671 view.SetCurrentPage(pagenum);
672
673 }
674
675 }
676
677 }
678
679 }
680
681
682
683 private void txtbox_zoom_KeyDown(object sender, KeyEventArgs e)
684
685 {
686
687 if (e.Key == Key.Enter && !string.IsNullOrWhiteSpace(txtbox_zoom.Text))
688
689 {
690
691 double newzoom;
692
693
694
695 if (Double.TryParse(txtbox_zoom.Text, out newzoom))
696
697 {
698
699 PDFViewViewer view = GetCurrentViewer();
700
701 if (view != null)
702
703 {
704
705 view.ZoomLevel = newzoom / 100.0;
706
707 }
708
709 }
710
711 }
712
713 }
714
715
716
717 private void btnOpen_Clicked(object sender, RoutedEventArgs e)
718
719 {
720
721 Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
722
723 dlg.CheckFileExists = true;
724
725 dlg.CheckPathExists = true;
726
727 dlg.Filter = "PDF (*.pdf)|*.pdf|All files (*.*)|*.*";
728
729 dlg.DefaultExt = ".pdf";
730
731
732
733 if (dlg.ShowDialog() == true)
734
735 {
736
737 PDFViewViewer view = new PDFViewViewer(this);
738
739 if (view.OpenPDF(dlg.FileName))
740
741 {
742
743 LoadViewer(view, dlg.SafeFileName);
744
745 }
746
747 }
748
749 }
750
751
752
753 private void btnOpenUrl_Clicked(object sender, RoutedEventArgs e)
754
755 {
756
757 OpenUrlDlg dlg = new OpenUrlDlg();
758
759 dlg.Owner = this;
760
761 if(dlg.ShowDialog() == true)
762
763 {
764
765 PDFViewViewer view = new PDFViewViewer(this);
766
767 if (view.OpenURL(dlg.URL, dlg.Pass))
768
769 {
770
771 LoadViewer(view, dlg.URL);
772
773 }
774
775 }
776
777 }
778
779
780
781 private void LoadViewer(PDFViewViewer view, String title)
782
783 {
784
785 TabItem item = new TabItem();
786
787
788
789 // add a close button
790
791 StackPanel tabHeaderStack = new StackPanel();
792
793 tabHeaderStack.Orientation = Orientation.Horizontal;
794
795 TextBlock tabLabel = new TextBlock();
796
797 tabLabel.Text = title;
798
799 tabHeaderStack.Children.Add(tabLabel);
800
801 Button tabCloseButton = new Button();
802
803 tabCloseButton.Width = 11;
804
805 tabCloseButton.Height = 11;
806
807 tabCloseButton.Content = "X";
808
809 tabCloseButton.Padding = new System.Windows.Thickness(0, -3, 1, 0);
810
811 tabCloseButton.BorderThickness = new System.Windows.Thickness(0);
812
813 tabCloseButton.Background = Brushes.Transparent;
814
815 tabCloseButton.Foreground = Brushes.DarkGray;
816
817 tabCloseButton.Margin = new Thickness(5, 3, 0, 0);
818
819 tabHeaderStack.Children.Add(tabCloseButton);
820
821 tabCloseButton.Click += btnClose_Clicked;
822
823
824
825 item.Header = tabHeaderStack;
826
827 view._safeDocName = title;
828
829 Grid grid = new Grid();
830
831 grid.Children.Add(view);
832
833 item.Content = grid;
834
835 PDFTab.Items.Add(item);
836
837 item.IsSelected = true;
838
839 }
840
841
842
843 private void btn_Prev_Clicked(object sender, RoutedEventArgs e)
844
845 {
846
847 PDFViewViewer view = GetCurrentViewer();
848
849 if (view != null)
850
851 {
852
853 view.PrevPage();
854
855 }
856
857 }
858
859
860
861 private void btn_Next_Clicked(object sender, RoutedEventArgs e)
862
863 {
864
865 PDFViewViewer view = GetCurrentViewer();
866
867 if (view != null)
868
869 {
870
871 view.NextPage();
872
873 }
874
875 }
876
877
878
879 private void btn_ZoomIn_Clicked(object sender, RoutedEventArgs e)
880
881 {
882
883 PDFViewViewer view = GetCurrentViewer();
884
885 if (view != null)
886
887 {
888
889 view.ZoomIn();
890
891 }
892
893 }
894
895
896
897 private void btn_ZoomOut_Clicked(object sender, RoutedEventArgs e)
898
899 {
900
901 PDFViewViewer view = GetCurrentViewer();
902
903 if (view != null)
904
905 {
906
907 view.ZoomOut();
908
909 }
910
911 }
912
913
914
915 private void btnExit_Clicked(object sender, RoutedEventArgs e)
916
917 {
918
919 this.Close();
920
921 }
922
923
924
925 protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
926
927 {
928
929 List<TabItem> tabsToClose = new List<TabItem>();
930
931 if (PDFTab.HasItems)
932
933 {
934
935 for (int i = 0; i < PDFTab.Items.Count; ++i)
936
937 {
938
939 TabItem item = (TabItem)PDFTab.Items.GetItemAt(i);
940
941 Grid g = (Grid)item.Content;
942
943 PDFViewViewer view = (PDFViewViewer)g.Children[0];
944
945 if (view == null) continue;
946
947 if (!view.CloseFile())
948
949 {
950
951 e.Cancel = true;
952
953 break;
954
955 }
956
957 else
958
959 {
960
961 tabsToClose.Add(item);
962
963 }
964
965 }
966
967 }
968
969 if (e.Cancel)
970
971 {
972
973 foreach (TabItem tab in tabsToClose)
974
975 {
976
977 PDFTab.Items.Remove(tab);
978
979 }
980
981 }
982
983 base.OnClosing(e);
984
985 }
986
987
988
989 private void btn_LastPage_Clicked(object sender, RoutedEventArgs e)
990
991 {
992
993 PDFViewViewer view = GetCurrentViewer();
994
995 if (view != null)
996
997 {
998
999 view.LastPage();
1000
1001 }
1002
1003 }
1004
1005
1006
1007 private void btn_FirstPage_Clicked(object sender, RoutedEventArgs e)
1008
1009 {
1010
1011 PDFViewViewer view = GetCurrentViewer();
1012
1013 if (view != null)
1014
1015 {
1016
1017 view.FirstPage();
1018
1019 }
1020
1021 }
1022
1023
1024
1025 private void btn_RotateCCW_Clicked(object sender, RoutedEventArgs e)
1026
1027 {
1028
1029 PDFViewViewer view = GetCurrentViewer();
1030
1031 if (view != null)
1032
1033 {
1034
1035 view.RotateCounterClockwise();
1036
1037 }
1038
1039 }
1040
1041
1042
1043 private void btn_RotateCW_Clicked(object sender, RoutedEventArgs e)
1044
1045 {
1046
1047 PDFViewViewer view = GetCurrentViewer();
1048
1049 if (view != null)
1050
1051 {
1052
1053 view.RotateClockwise();
1054
1055 }
1056
1057 }
1058
1059
1060
1061 private void FitWidth_Clicked(object sender, RoutedEventArgs e)
1062
1063 {
1064
1065 PDFViewViewer view = GetCurrentViewer();
1066
1067 if (view != null)
1068
1069 {
1070
1071 view.FitWidth();
1072
1073 }
1074
1075 }
1076
1077
1078
1079 private void FitPageClicked(object sender, RoutedEventArgs e)
1080
1081 {
1082
1083 PDFViewViewer view = GetCurrentViewer();
1084
1085 if (view != null)
1086
1087 {
1088
1089 view.FitPage();
1090
1091 }
1092
1093 }
1094
1095
1096
1097 private void SinglePage_Clicked(object sender, RoutedEventArgs e)
1098
1099 {
1100
1101 PDFViewViewer view = GetCurrentViewer();
1102
1103 if (view != null)
1104
1105 {
1106
1107 view.PageSingle();
1108
1109 }
1110
1111 }
1112
1113
1114
1115 private void SingleContinuous_Clicked(object sender, RoutedEventArgs e)
1116
1117 {
1118
1119 PDFViewViewer view = GetCurrentViewer();
1120
1121 if (view != null)
1122
1123 {
1124
1125 view.PageSingleContinuous();
1126
1127 }
1128
1129 }
1130
1131
1132
1133 private void Facing_Clicked(object sender, RoutedEventArgs e)
1134
1135 {
1136
1137 PDFViewViewer view = GetCurrentViewer();
1138
1139 if (view != null)
1140
1141 {
1142
1143 view.PageFacing();
1144
1145 }
1146
1147 }
1148
1149
1150
1151 private void FacingContinuous_Clicked(object sender, RoutedEventArgs e)
1152
1153 {
1154
1155 PDFViewViewer view = GetCurrentViewer();
1156
1157 if (view != null)
1158
1159 {
1160
1161 view.PageFacingContinuous();
1162
1163 }
1164
1165 }
1166
1167
1168
1169 private void FacingCoverContinuous_Clicked(object sender, RoutedEventArgs e)
1170
1171 {
1172
1173 PDFViewViewer view = GetCurrentViewer();
1174
1175 if (view != null)
1176
1177 {
1178
1179 view.PageFacingCoverContinous();
1180
1181 }
1182
1183 }
1184
1185
1186
1187 private void FacingCover_Clicked(object sender, RoutedEventArgs e)
1188
1189 {
1190
1191 PDFViewViewer view = GetCurrentViewer();
1192
1193 if (view != null)
1194
1195 {
1196
1197 view.PageFacingCover();
1198
1199 }
1200
1201 }
1202
1203
1204
1205 private void btnPrint_Clicked(object sender, RoutedEventArgs e)
1206
1207 {
1208
1209 PDFViewViewer view = GetCurrentViewer();
1210
1211 if (view != null)
1212
1213 {
1214
1215 PrintDialog pDlg = new PrintDialog();
1216
1217 pDlg.UserPageRangeEnabled = true;
1218
1219 Nullable<Boolean> result = pDlg.ShowDialog();
1220
1221 if (result == true)
1222
1223 {
1224
1225 PrintQueue pq = pDlg.PrintQueue;
1226
1227 PrintTicket pt = pDlg.PrintTicket;
1228
1229 PageSet ps = new PageSet(pDlg.PageRange.PageFrom, pDlg.PageRange.PageTo);
1230
1231 if (pDlg.PageRangeSelection == PageRangeSelection.AllPages)
1232
1233 ps = new PageSet(1, view.GetPageCount());
1234
1235 PrinterMode pm = new PrinterMode();
1236
1237 pm.SetCopyCount(pt.CopyCount.HasValue ? pt.CopyCount.Value : 1);
1238
1239 if (view.GetRotation() == pdftron.PDF.Page.Rotate.e_90 || view.GetRotation() == pdftron.PDF.Page.Rotate.e_270)
1240
1241 pm.SetOrientation(PrinterMode.Orientation.e_Orientation_Landscape);
1242
1243 else
1244
1245 pm.SetOrientation(PrinterMode.Orientation.e_Orientation_Portrait);
1246
1247 BackgroundWorker bgPrintWorker = new BackgroundWorker();
1248
1249 bgPrintWorker.DoWork += BackgroundPrintTask;
1250
1251 bgPrintWorker.RunWorkerCompleted += bgPrintWorker_RunWorkerCompleted;
1252
1253 bgPrintWorker.RunWorkerAsync(new PrinterArguments(view.GetPDFDoc(), pq.Name, view.GetPDFDoc().GetFileName(), ps, pm, view.Current_View.GetOCGContext()));
1254
1255
1256
1257 }
1258
1259 }
1260
1261 }
1262
1263
1264
1265 void bgPrintWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
1266
1267 {
1268
1269 if (e.Error != null)
1270
1271 {
1272
1273 MessageBox.Show(string.Format("Printing failed: {0}{0}{1}", Environment.NewLine, e.Error.Message));
1274
1275 }
1276
1277 }
1278
1279
1280
1281 void BackgroundPrintTask(object sender, DoWorkEventArgs e)
1282
1283 {
1284
1285
1286
1287 PrinterArguments printerArgs = (PrinterArguments)e.Argument;
1288
1289 try
1290
1291 {
1292
1293 printerArgs.DocToPrint.LockRead();
1294
1295 Print.StartPrintJob(printerArgs.DocToPrint, printerArgs.PrinterName, printerArgs.DocToPrint.GetFileName(),
1296
1297 "", printerArgs.PageSet, printerArgs.PrinterMode, printerArgs.OCG);
1298
1299 }
1300
1301 catch (Exception ex)
1302
1303 {
1304
1305 throw new Exception(ex.Message);
1306
1307 }
1308
1309 finally
1310
1311 {
1312
1313 printerArgs.DocToPrint.UnlockRead();
1314
1315 }
1316
1317 }
1318
1319
1320
1321 private void btnClose_Clicked(object sender, RoutedEventArgs e)
1322
1323 {
1324
1325 PDFViewViewer view = GetCurrentViewer();
1326
1327 if (view != null)
1328
1329 {
1330
1331 if (view.CloseFile())
1332
1333 {
1334
1335 TabItem tab = GetCurrentTab();
1336
1337 if (tab != null)
1338
1339 {
1340
1341 PDFTab.Items.Remove(tab);
1342
1343 }
1344
1345 }
1346
1347 }
1348
1349 }
1350
1351
1352
1353 private void btnSave_Clicked(object sender, RoutedEventArgs e)
1354
1355 {
1356
1357 PDFViewViewer view = GetCurrentViewer();
1358
1359 if (view != null)
1360
1361 {
1362
1363 PDFDoc doc = view.GetPDFDoc();
1364
1365 if (doc != null)
1366
1367 {
1368
1369 if (String.IsNullOrEmpty(doc.GetFileName()))
1370
1371 {
1372
1373 btnSaveAs_Clicked(sender, e);
1374
1375 }
1376
1377 else
1378
1379 {
1380
1381 view.Save(doc.GetFileName());
1382
1383 }
1384
1385 }
1386
1387 }
1388
1389 }
1390
1391
1392
1393 private void btnSaveAs_Clicked(object sender, RoutedEventArgs e)
1394
1395 {
1396
1397 PDFViewViewer view = GetCurrentViewer();
1398
1399 if (view != null)
1400
1401 {
1402
1403 string newname = view.SaveAs();
1404
1405 if (newname != null)
1406
1407 {
1408
1409 TabItem item = GetCurrentTab();
1410
1411 StackPanel sp = item.Header as StackPanel;
1412
1413 TextBlock tb = sp.Children[0] as TextBlock;
1414
1415 tb.Text = newname;
1416
1417 view._safeDocName = newname;
1418
1419 this.Title = view.GetPDFDoc().GetFileName();
1420
1421 }
1422
1423 }
1424
1425 }
1426
1427
1428
1429 private void btnExpand_Clicked(object sender, RoutedEventArgs e)
1430
1431 {
1432
1433 PDFViewViewer view = GetCurrentViewer();
1434
1435 if (view != null)
1436
1437 {
1438
1439 view.OpenNavibar();
1440
1441 }
1442
1443 }
1444
1445
1446
1447 private void btnCollapsed_Clicked(object sender, RoutedEventArgs e)
1448
1449 {
1450
1451 PDFViewViewer view = GetCurrentViewer();
1452
1453 if (view != null)
1454
1455 {
1456
1457 view.CloseNavibar();
1458
1459 }
1460
1461 }
1462
1463
1464
1465 private void btnSelectAll_Clicked(object sender, RoutedEventArgs e)
1466
1467 {
1468
1469 PDFViewViewer view = GetCurrentViewer();
1470
1471 if (view != null)
1472
1473 {
1474
1475 view.SelectAll();
1476
1477 }
1478
1479 }
1480
1481
1482
1483 private void btnDeselectAll_Clicked(object sender, RoutedEventArgs e)
1484
1485 {
1486
1487 PDFViewViewer view = GetCurrentViewer();
1488
1489 if (view != null)
1490
1491 {
1492
1493 view.DeselectAll();
1494
1495 }
1496
1497 }
1498
1499
1500
1501 #endregion Events
1502
1503
1504
1505 #region ToolMode
1506
1507
1508
1509 //////////////////////////////////////////////////////////////////////////
1510
1511 // Note: Tools that create or add annotations can be created either as
1512
1513 // persistent or not. If not persistent any shape creation tool will revert
1514
1515 // back to the pan tool when done.
1516
1517 // If persistent, any number of shapes can be drawn, and the tool has to
1518
1519 // be closed explicitly (by creating another tool).
1520
1521 // This can be changed by setting the 3rd parameter of ToolManager.CreateTool
1522
1523 // to true (persistent) or false (not persistent).
1524
1525 //////////////////////////////////////////////////////////////////////////
1526
1527 private void PanTool_Clicked(object sender, RoutedEventArgs e)
1528
1529 {
1530
1531 PDFViewViewer view = GetCurrentViewer();
1532
1533 if (view != null)
1534
1535 {
1536
1537 view.ToolManager.CreateTool(pdftron.PDF.Tools.ToolManager.ToolType.e_pan, null);
1538
1539 }
1540
1541
1542
1543 }
1544
1545
1546
1547 private void TextSelectStruct_Clicked(object sender, RoutedEventArgs e)
1548
1549 {
1550
1551 PDFViewViewer view = GetCurrentViewer();
1552
1553 if (view != null)
1554
1555 {
1556
1557 view.ToolManager.CreateTool(pdftron.PDF.Tools.ToolManager.ToolType.e_text_select, null);
1558
1559 }
1560
1561 }
1562
1563
1564
1565 private void TextSelectRect_Clicked(object sender, RoutedEventArgs e)
1566
1567 {
1568
1569 PDFViewViewer view = GetCurrentViewer();
1570
1571 if (view != null)
1572
1573 {
1574
1575 view.ToolManager.CreateTool(pdftron.PDF.Tools.ToolManager.ToolType.e_text_select_rectangular, null);
1576
1577 }
1578
1579 }
1580
1581
1582
1583 private void AnnotationEditTool_Clicked(object sender, RoutedEventArgs e)
1584
1585 {
1586
1587 PDFViewViewer view = GetCurrentViewer();
1588
1589 if (view != null)
1590
1591 {
1592
1593 view.ToolManager.CreateTool(pdftron.PDF.Tools.ToolManager.ToolType.e_annot_edit, null);
1594
1595 }
1596
1597
1598
1599 }
1600
1601
1602
1603 private void LineTool_Clicked(object sender, RoutedEventArgs e)
1604
1605 {
1606
1607 PDFViewViewer view = GetCurrentViewer();
1608
1609 if (view != null)
1610
1611 {
1612
1613 view.ToolManager.CreateTool(pdftron.PDF.Tools.ToolManager.ToolType.e_line_create, null, true);
1614
1615 }
1616
1617 }
1618
1619
1620
1621 private void ArrowTool_Clicked(object sender, RoutedEventArgs e)
1622
1623 {
1624
1625 PDFViewViewer view = GetCurrentViewer();
1626
1627 if (view != null)
1628
1629 {
1630
1631 view.ToolManager.CreateTool(pdftron.PDF.Tools.ToolManager.ToolType.e_arrow_create, null, true);
1632
1633 }
1634
1635
1636
1637 }
1638
1639
1640
1641 private void OvalTool_Clicked(object sender, RoutedEventArgs e)
1642
1643 {
1644
1645 PDFViewViewer view = GetCurrentViewer();
1646
1647 if (view != null)
1648
1649 {
1650
1651 view.ToolManager.CreateTool(pdftron.PDF.Tools.ToolManager.ToolType.e_oval_create, null, true);
1652
1653 }
1654
1655 }
1656
1657
1658
1659 private void RectangleTool_Clicked(object sender, RoutedEventArgs e)
1660
1661 {
1662
1663 PDFViewViewer view = GetCurrentViewer();
1664
1665 if (view != null)
1666
1667 {
1668
1669 view.ToolManager.CreateTool(pdftron.PDF.Tools.ToolManager.ToolType.e_rect_create, null, true);
1670
1671 }
1672
1673 }
1674
1675
1676
1677 private void InkTool_Clicked(object sender, RoutedEventArgs e)
1678
1679 {
1680
1681 PDFViewViewer view = GetCurrentViewer();
1682
1683 if (view != null)
1684
1685 {
1686
1687 view.ToolManager.CreateTool(pdftron.PDF.Tools.ToolManager.ToolType.e_ink_create, null, true);
1688
1689
1690
1691 // You can create a multi-path ink annotation as follows:
1692
1693 // This means that you can have an annotation with multiple strokes.
1694
1695 //pdftron.PDF.Tools.FreehandCreate fhc = view.ToolManager.CurrentTool as pdftron.PDF.Tools.FreehandCreate;
1696
1697 //fhc.CreateMultiPathAnnotation = true;
1698
1699
1700
1701 // You can then use the following 2 functions to save or discard the current shape
1702
1703 //fhc.SaveShape();
1704
1705 //fhc.DiscardShape();
1706
1707
1708
1709 // Currently, the FreeHandCreate tool is set up so that the key 'y' will save the drawing and the key
1710
1711 // 'n' will discard it. Also, on the right click context menu, there's a save and discard option
1712
1713 }
1714
1715
1716
1717 }
1718
1719
1720
1721 private void HighlightTool_Clicked(object sender, RoutedEventArgs e)
1722
1723 {
1724
1725 PDFViewViewer view = GetCurrentViewer();
1726
1727 if (view != null)
1728
1729 {
1730
1731 view.ToolManager.CreateTool(pdftron.PDF.Tools.ToolManager.ToolType.e_text_highlight, null, true);
1732
1733 }
1734
1735
1736
1737 }
1738
1739
1740
1741 private void UnderlineTool_Clicked(object sender, RoutedEventArgs e)
1742
1743 {
1744
1745 PDFViewViewer view = GetCurrentViewer();
1746
1747 if (view != null)
1748
1749 {
1750
1751 view.ToolManager.CreateTool(pdftron.PDF.Tools.ToolManager.ToolType.e_text_underline, null, true);
1752
1753 }
1754
1755
1756
1757 }
1758
1759
1760
1761 private void StrikeoutTool_Clicked(object sender, RoutedEventArgs e)
1762
1763 {
1764
1765 PDFViewViewer view = GetCurrentViewer();
1766
1767 if (view != null)
1768
1769 {
1770
1771 view.ToolManager.CreateTool(pdftron.PDF.Tools.ToolManager.ToolType.e_text_strikeout, null, true);
1772
1773 }
1774
1775
1776
1777 }
1778
1779
1780
1781 private void SquigglyTool_Clicked(object sender, RoutedEventArgs e)
1782
1783 {
1784
1785 PDFViewViewer view = GetCurrentViewer();
1786
1787 if (view != null)
1788
1789 {
1790
1791 view.ToolManager.CreateTool(pdftron.PDF.Tools.ToolManager.ToolType.e_text_squiggly, null, true);
1792
1793 }
1794
1795
1796
1797 }
1798
1799
1800
1801 private void FreeTextToolTool_Clicked(object sender, RoutedEventArgs e)
1802
1803 {
1804
1805 PDFViewViewer view = GetCurrentViewer();
1806
1807 if (view != null)
1808
1809 {
1810
1811 view.ToolManager.CreateTool(pdftron.PDF.Tools.ToolManager.ToolType.e_text_annot_create, null, true);
1812
1813 }
1814
1815 }
1816
1817
1818
1819 private void CalloutTool_Clicked(object sender, RoutedEventArgs e)
1820
1821 {
1822
1823 PDFViewViewer view = GetCurrentViewer();
1824
1825 if (view != null)
1826
1827 {
1828
1829 view.ToolManager.CreateTool(pdftron.PDF.Tools.ToolManager.ToolType.e_callout_create, null, true);
1830
1831 }
1832
1833 }
1834
1835
1836
1837
1838
1839 private void StickyNoteTool_Clicked(object sender, RoutedEventArgs e)
1840
1841 {
1842
1843 PDFViewViewer view = GetCurrentViewer();
1844
1845 if (view != null)
1846
1847 {
1848
1849 view.ToolManager.CreateTool(pdftron.PDF.Tools.ToolManager.ToolType.e_sticky_note_create, null, true);
1850
1851 }
1852
1853
1854
1855 }
1856
1857
1858
1859 #endregion ToolMode
1860
1861 }
1862
1863}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales