Sample C# code for using Apryse SDK's PDF viewer control to implement a number of custom tools (such as freehand tool, link creation tool, rectangular zoom etc) and custom GUI elements (such as custom navigation and printing). For a more introductory example of how to use PDFViewCtrl, please take a look at the PDFViewSimple sample code. Learn more about our Server SDK and PDF Viewer SDK.
1using System;
2
3using System.Drawing;
4
5using System.Drawing.Drawing2D;
6
7using System.Drawing.Printing;
8
9using System.Collections;
10
11using System.ComponentModel;
12
13using System.Windows.Forms;
14
15
16
17using pdftron;
18
19using pdftron.PDF;
20
21using pdftron.SDF;
22
23using pdftron.Common;
24
25
26
27
28
29
30
31namespace PDFViewCS
32
33{
34
35 /// <summary>
36
37 /// Summary description for PDFViewForm.
38
39 /// </summary>
40
41 public class PDFViewForm : System.Windows.Forms.Form
42
43 {
44
45 /// <summary>
46
47 /// Required designer variable.
48
49 /// </summary>
50
51 private System.ComponentModel.Container components = null;
52
53
54
55#if CUSTOM_NAV
56
57 // The following variables are used for custom page navigation pane...
58
59 private System.Windows.Forms.TabControl _pdfdoc_tab;
60
61 private System.Windows.Forms.TabPage _bookmarks_tab;
62
63 private System.Windows.Forms.TabPage _pages_tab;
64
65 private System.Windows.Forms.TabPage _layers_tab;
66
67 private System.Windows.Forms.TreeView bookmark_tree;
68
69 private System.Windows.Forms.TreeView layer_tree;
70
71 private System.Windows.Forms.Splitter splitter1;
72
73 private ThumbView _thumbview = null; // Custom thumbnail view.
74
75#endif
76
77
78
79 private PDFDoc _pdfdoc = null; // Currently open PDF document.
80
81 private MyPDFView _pdfview = null; // Main PDF view.
82
83
84
85 public PDFViewForm(MainForm main_form)
86
87 {
88
89 this.MdiParent = main_form;
90
91
92
93 // Create other controls created using Windows Form Designer
94
95 InitializeComponent();
96
97
98
99 // Create the main PDFViewCtrl control (we do it here manually for greater control)
100
101 _pdfview = new MyPDFView(this);
102
103 _pdfview.Location = new System.Drawing.Point(0, 0);
104
105 _pdfview.Dock = System.Windows.Forms.DockStyle.Fill;
106
107
108
109 // Optional: Set the error and current page delegates...
110
111 _pdfview.SetErrorReportHandler(new PDFViewErrorDelegate(ErrorMsg), null);
112
113 _pdfview.SetCurrentPageHandler(new PDFViewCurrentPageDelegate(UpdateStatusBar), main_form);
114
115 _pdfview.SetDownloadReportHandler(new PDFViewDownloadDelegate(OnDownload), null);
116
117
118
119 _pdfview.SetUrlExtraction(false);
120
121
122
123 Controls.Add(_pdfview);
124
125 }
126
127
128
129 public bool OpenPDF(String filename)
130
131 {
132
133 try
134
135 {
136
137 try { // Try to open as a PDF document...
138
139 _pdfdoc = new PDFDoc(filename);
140
141 }
142
143 catch (Exception ex) {
144
145 // Try to open as a PNG, JPEG, TIF, BMP, GIF, etc.
146
147 _pdfdoc = OpenImage(filename);
148
149 if (_pdfdoc == null) {
150
151 throw ex; // re-throw the original exception
152
153 }
154
155 }
156
157
158
159 if (!_pdfdoc.InitSecurityHandler()) // In case _pdfdoc is encrypted
160
161 {
162
163 MessageBox.Show("Document authentication error", "PDFViewCtrl Error");
164
165 return false;
166
167 }
168
169
170
171#if CUSTOM_NAV
172
173 // Populates a custom bookmark tree control with bookmark nodes (if any).
174
175 Bookmark root = _pdfdoc.GetFirstBookmark();
176
177 if (root.IsValid())
178
179 {
180
181 bookmark_tree.BeginUpdate();
182
183 BuildBookmarkTree(root, bookmark_tree.Nodes);
184
185 bookmark_tree.EndUpdate();
186
187 }
188
189 else
190
191 {
192
193 // Optional: Uncomment the following line to hide the bookmark
194
195 // tab if the document does not containing bookmarks?:
196
197 // _pdfdoc_tab.Hide();
198
199 }
200
201
202
203 // Add OCGs (if any) to 'PDF Layers' tab.
204
205 if (_pdfdoc.HasOC())
206
207 {
208
209 pdftron.PDF.OCG.Config cfg = _pdfdoc.GetOCGConfig();
210
211 Obj order = cfg.GetOrder();
212
213 if (order != null) {
214
215 layer_tree.BeginUpdate();
216
217 BuildLayerTree(order, cfg, layer_tree.Nodes, null);
218
219 layer_tree.EndUpdate();
220
221 }
222
223 }
224
225#endif
226
227
228
229 // Optional: Set page view and page presentation mode.
230
231 // _pdfview.SetPagePresentationMode(PDFViewCtrl.PagePresentationMode.e_single_page);
232
233 // _pdfview.SetPageViewMode(PDFViewCtrl.PageViewMode.e_fit_page);
234
235 // _pdfview.SetProgressiveRendering(false);
236
237
238
239 if (_pdfdoc.GetPageCount() > 2000)
240
241 {
242
243 // If the document has many pages use single page mode to seed up initial rendering.
244
245 _pdfview.SetPagePresentationMode(PDFViewCtrl.PagePresentationMode.e_single_page);
246
247 }
248
249
250
251 _pdfview.SetDoc(_pdfdoc);
252
253
254
255#if CUSTOM_NAV
256
257 _thumbview.SetDoc(_pdfdoc, _pdfview);
258
259#endif
260
261 SetToolMode(MyPDFView._base_tool_mode, MyPDFView._tool_mode); // Set the view to use the currently selected tool (i.e. hand, text selection, pencil, etc).
262
263 }
264
265 catch(PDFNetException ex)
266
267 {
268
269 MessageBox.Show(ex.Message);
270
271 return false;
272
273 }
274
275 catch(Exception ex)
276
277 {
278
279 MessageBox.Show(ex.ToString());
280
281 return false;
282
283 }
284
285
286
287 this.Text = filename; // Set the title
288
289 return true;
290
291 }
292
293
294
295 public bool OpenPDFUrl(String url, String password)
296
297 {
298
299 try
300
301 {
302
303 // Open a PDF file at the given url. This works best with PDF's that
304
305 // are linearized, as pages can be downloaded and viewed in random access order,
306
307 // without the need to download the entire document. A viewing session can also be
308
309 // persisted across multiple viewing/application sessions to remove redundant downloads
310
311 // and improve overall performance by using the optional cache_file parameter.
312
313 _pdfview.OpenURLAsync(url, "", password);
314
315 }
316
317 catch (PDFNetException ex)
318
319 {
320
321 MessageBox.Show(ex.Message);
322
323 return false;
324
325 }
326
327 catch (Exception ex)
328
329 {
330
331 MessageBox.Show(ex.ToString());
332
333 return false;
334
335 }
336
337 this.Text = url; // Set the title
338
339 return true;
340
341 }
342
343
344
345 /// <summary>
346
347 /// This callback method (delegate) was registered using _pdfview.SetDownloadReportHandler
348
349 /// in PDFViewCS constructor. The callback can be used to update download progress
350
351 /// within GUI applications etc.
352
353 /// </summary>
354
355 private void OnDownload(DownloadedType type, Int32 page_num, Int32 obj_num, String msg, Object obj)
356
357 {
358
359 switch (type)
360
361 {
362
363 case DownloadedType.e_opened:
364
365 // e_opened indicates that we have a valid, but incomplete PDFDoc.
366
367 _pdfdoc = _pdfview.GetDoc();
368
369 MainForm main_form = (MainForm) this.MdiParent;
370
371 main_form.UpdateStatusBar(_pdfview.GetCurrentPage(), _pdfview.GetDoc().GetPageCount());
372
373 // the PDF should be treated as read only, and only simple functions
374
375 // should be called on the doc, until e_finished has been called.
376
377 break;
378
379 case DownloadedType.e_page:
380
381 // this indicates the entire page is downloaded and it is safe to modify.
382
383 // for example add a new annotation
384
385 break;
386
387 case DownloadedType.e_finished:
388
389 // we now have the full PDF file and it can be treated like any other
390
391 if (MessageBox.Show("Download complete, would you like to save the PDF locally?",
392
393 "PDF Downloaded", MessageBoxButtons.YesNo) == DialogResult.Yes)
394
395 {
396
397 SaveAs();
398
399 }
400
401 break;
402
403 case DownloadedType.e_failed:
404
405 // downloading has stopped if this occurs
406
407 MessageBox.Show(msg);
408
409 this.Text = "";
410
411 break;
412
413 }
414
415 }
416
417
418
419 // A utility function used to display other images types besides PDF
420
421 // inside MyPDFView. This functionality can be used to display TIF, JPEG,
422
423 // BMP, PNG, etc.
424
425 public PDFDoc OpenImage(string filename)
426
427 {
428
429 try
430
431 {
432
433 PDFDoc pdfDoc = new PDFDoc(); // create new document
434
435 ElementBuilder f = new ElementBuilder();
436
437 ElementWriter writer = new ElementWriter();
438
439
440
441 Page page = pdfDoc.PageCreate(); // Add a blank page
442
443 writer.Begin(page);
444
445
446
447 // Add image to the document.
448
449 pdftron.PDF.Image img = pdftron.PDF.Image.Create(pdfDoc, filename);
450
451
452
453 // get image rectangle
454
455 Rect imgBox = new Rect(0, 0, img.GetImageWidth(),
456
457 img.GetImageHeight());
458
459 Rect scaledBox = new Rect();
460
461 double scaleFactor;
462
463 if(imgBox.Height()/imgBox.Width()>792/612)
464
465 {
466
467 scaleFactor=imgBox.Height()/792;
468
469 }
470
471 else
472
473 {
474
475 scaleFactor=imgBox.Width()/612;
476
477 }
478
479 scaledBox.x2=imgBox.x2/scaleFactor;
480
481 scaledBox.y2=imgBox.y2/scaleFactor;
482
483
484
485 // set crop and media box of this page to fit with the scaled image
486
487 page.SetCropBox(scaledBox);
488
489 page.SetMediaBox(scaledBox);
490
491
492
493
494
495 // create the image element and add it to the page
496
497 int width = (int)scaledBox.Width();
498
499 int height = (int) scaledBox.Height();
500
501 int offsetX = 0;
502
503 int offsetY = 0;
504
505
506
507 Element element = f.CreateImage(img, new Matrix2D(width, 0, 0,
508
509 height, offsetX, offsetY));
510
511 writer.WritePlacedElement(element);
512
513 writer.End(); // Finish writing to the page
514
515
516
517 pdfDoc.PagePushBack(page);
518
519 return pdfDoc;
520
521 }
522
523 catch (Exception)
524
525 {
526
527 // MessageBox.Show(ex.ToString());
528
529 return null;
530
531 }
532
533 }
534
535
536
537 public Boolean IsNavSidebarVisible()
538
539 {
540
541 return _pdfview.IsNavPanelVisible();
542
543 }
544
545
546
547 public void ShowNavSidebar(Boolean show)
548
549 {
550
551 _pdfview.ShowNavPanel(show);
552
553 }
554
555
556
557 public PDFDoc GetPDFDoc()
558
559 {
560
561 if (_pdfview == null) return null;
562
563 else return _pdfview.GetDoc();
564
565 }
566
567
568
569 public void FitPage()
570
571 {
572
573 _pdfview.SetPageViewMode(PDFViewCtrl.PageViewMode.e_fit_page);
574
575 }
576
577
578
579 public void FitWidth()
580
581 {
582
583 _pdfview.SetPageViewMode(PDFViewCtrl.PageViewMode.e_fit_width);
584
585 }
586
587
588
589 public void ZoomIn()
590
591 {
592
593 _pdfview.SetZoom(_pdfview.GetZoom()*2);
594
595 }
596
597
598
599 public void ZoomOut()
600
601 {
602
603 _pdfview.SetZoom(_pdfview.GetZoom()/2);
604
605 }
606
607
608
609 public void FirstPage()
610
611 {
612
613 _pdfview.GotoFirstPage();
614
615 }
616
617
618
619 public void PrevPage()
620
621 {
622
623 _pdfview.GotoPreviousPage();
624
625 }
626
627
628
629 public void NextPage()
630
631 {
632
633 _pdfview.GotoNextPage();
634
635 }
636
637
638
639 public void LastPage()
640
641 {
642
643 _pdfview.GotoLastPage();
644
645 }
646
647
648
649 public void PageSingle()
650
651 {
652
653 _pdfview.SetPagePresentationMode(PDFViewCtrl.PagePresentationMode.e_single_page);
654
655 }
656
657
658
659 public void PageSingleContinuous()
660
661 {
662
663 _pdfview.SetPagePresentationMode(PDFViewCtrl.PagePresentationMode.e_single_continuous);
664
665 }
666
667
668
669 public void PageFacingContinuous()
670
671 {
672
673 _pdfview.SetPagePresentationMode(PDFViewCtrl.PagePresentationMode.e_facing_continuous);
674
675 }
676
677
678
679 public void PageFacing()
680
681 {
682
683 _pdfview.SetPagePresentationMode(PDFViewCtrl.PagePresentationMode.e_facing);
684
685 }
686
687
688
689 public void RotateClockwise()
690
691 {
692
693 _pdfview.RotateClockwise();
694
695 }
696
697
698
699 public void RotateCounterClockwise()
700
701 {
702
703 _pdfview.RotateCounterClockwise();
704
705 }
706
707
708
709 public void DocProperties()
710
711 {
712
713 _pdfview.DocProperties();
714
715 }
716
717
718
719 public void DeletePages()
720
721 {
722
723 _pdfview.DeletePages();
724
725 }
726
727
728
729 public void InsertBlankPages()
730
731 {
732
733 _pdfview.InsertBlankPages();
734
735 }
736
737
738
739 public void InsertPages()
740
741 {
742
743 _pdfview.InsertPages();
744
745 }
746
747
748
749 public void ReplacePages()
750
751 {
752
753 _pdfview.ReplacePages();
754
755 }
756
757
758
759 public void SetAntiAliasing(bool anti_alias)
760
761 {
762
763 _pdfview.SetAntiAliasing(anti_alias);
764
765 _pdfview.Update();
766
767 }
768
769
770
771 public void SetRasterizer(bool built_in)
772
773 {
774
775 if (built_in)
776
777 _pdfview.SetRasterizerType(PDFRasterizer.Type.e_BuiltIn);
778
779 else
780
781 _pdfview.SetRasterizerType(PDFRasterizer.Type.e_GDIPlus);
782
783
784
785 _pdfview.Update();
786
787 }
788
789
790
791 public void SetSmoothImages(bool smooth_images)
792
793 {
794
795 _pdfview.SetImageSmoothing(smooth_images);
796
797 _pdfview.Update();
798
799 }
800
801
802
803 public void Save (string filename)
804
805 {
806
807 if (_pdfdoc == null) return;
808
809
810
811 _pdfdoc.Lock();
812
813
814
815 try
816
817 {
818
819 // If the user created new markup, merge it before saving the document.
820
821 if (_pdfview != null)
822
823 {
824
825 if (_pdfview._freehand_markup != null)
826
827 {
828
829 foreach( DictionaryEntry itr in _pdfview._freehand_markup)
830
831 {
832
833 int annot_page_num = (int) itr.Key;
834
835 Page pg = _pdfdoc.GetPage(annot_page_num);
836
837 if (pg != null)
838
839 {
840
841 FreeHandAnnot annot = (FreeHandAnnot) itr.Value;
842
843 annot.DrawAnnots(pg, Pens.Red);
844
845 }
846
847 }
848
849 _pdfview._freehand_markup = null;
850
851 _pdfview.Invalidate();
852
853 _pdfview.Update();
854
855 }
856
857 }
858
859
860
861 _pdfdoc.Save(filename, pdftron.SDF.SDFDoc.SaveOptions.e_remove_unused);
862
863 }
864
865 catch(Exception ex)
866
867 {
868
869 MessageBox.Show(ex.ToString(), "Error during the Save");
870
871 }
872
873
874
875 _pdfdoc.Unlock();
876
877 }
878
879
880
881 public void SaveAs ()
882
883 {
884
885 // Check if there are any annotations that need to
886
887 // be merged with the document.
888
889 if (_pdfdoc != null && _pdfview != null)
890
891 {
892
893 SaveFileDialog dlg = new SaveFileDialog();
894
895 dlg.Filter = "PDF Files (*.pdf)|*.pdf|All Files (*.*)|*.*";
896
897 dlg.DefaultExt = ".pdf";
898
899 dlg.FileName = Text;
900
901 DialogResult res = dlg.ShowDialog();
902
903
904
905 if (res == DialogResult.OK)
906
907 {
908
909 this.Save(dlg.FileName);
910
911 }
912
913 }
914
915 }
916
917
918
919 protected override void OnClosing (CancelEventArgs e)
920
921 {
922
923
924
925 // Check if there are any annotations that need to
926
927 // be merged with the document.
928
929 if (_pdfdoc != null && _pdfview != null &&
930
931 (_pdfdoc.IsModified() || _pdfview._freehand_markup != null))
932
933 {
934
935 DialogResult save = MessageBox.Show("Would you like to save the changes to the document?", "PDFViewCtrl", MessageBoxButtons.YesNoCancel);
936
937 if (save == DialogResult.Yes)
938
939 {
940
941 SaveFileDialog dlg = new SaveFileDialog();
942
943 dlg.Filter = "PDF Files (*.pdf)|*.pdf|All Files (*.*)|*.*";
944
945 dlg.DefaultExt = ".pdf";
946
947 dlg.FileName = Text;
948
949 DialogResult res = dlg.ShowDialog();
950
951
952
953 if (res == DialogResult.OK)
954
955 {
956
957 this.Save(dlg.FileName);
958
959 }
960
961 else if (res == DialogResult.Cancel)
962
963 {
964
965 e.Cancel = true;
966
967 }
968
969 }
970
971 else if (save == DialogResult.Cancel)
972
973 {
974
975 e.Cancel = true;
976
977 }
978
979
980
981 base.OnClosing(e);
982
983 }
984
985 }
986
987
988
989
990
991 /// <summary>
992
993 /// Clean up any resources being used.
994
995 /// </summary>
996
997 protected override void Dispose(bool disposing)
998
999 {
1000
1001 if( disposing )
1002
1003 {
1004
1005#if CUSTOM_NAV
1006
1007 _thumbview.Dispose(); // Close the thumbnail view.
1008
1009#endif
1010
1011 if (_pdfview != null)
1012
1013 {
1014
1015 _pdfview.CloseDoc();
1016
1017 _pdfview.Dispose(); // Close the open PDF document in the view.
1018
1019 }
1020
1021 if (_pdfdoc != null)
1022
1023 {
1024
1025 _pdfdoc.Dispose(); // Close the PDF document.
1026
1027 }
1028
1029 if(components != null)
1030
1031 {
1032
1033 components.Dispose();
1034
1035 }
1036
1037 }
1038
1039 base.Dispose( disposing );
1040
1041 }
1042
1043
1044
1045 /// <summary>
1046
1047 /// This callback method (delegate) was registered using _pdfview.SetErrorReportProc
1048
1049 /// in PDFViewCS constructor. The callback can be used to report any errors that may
1050
1051 /// occur during page rendering.
1052
1053 /// </summary>
1054
1055 public static void ErrorMsg(String message, Object obj)
1056
1057 {
1058
1059 MessageBox.Show(message, "PDFViewCtrl Error");
1060
1061 }
1062
1063
1064
1065#if CUSTOM_NAV // Custom bookmark and PDF layer navigation sample
1066
1067
1068
1069 // Handle the the bookmark select event (i.e. when the user selects a node in the bookmark tree).
1070
1071 private void BookmarkTreeAfterSelect(System.Object sender, System.Windows.Forms.TreeViewEventArgs e)
1072
1073 {
1074
1075 if (e.Node.Tag == null) return;
1076
1077
1078
1079 _pdfdoc.Lock();
1080
1081 Bookmark item = (Bookmark) e.Node.Tag;
1082
1083 Action action = item.GetAction();
1084
1085 if (action.IsValid()) // Handle goto actions.
1086
1087 { // Other types of actions can be handled in similar way.
1088
1089 if (action.GetType() == Action.Type.e_GoTo)
1090
1091 {
1092
1093 Destination dest = action.GetDest();
1094
1095 if (dest.IsValid())
1096
1097 {
1098
1099 Page page = dest.GetPage();
1100
1101 if (page != null) _pdfview.SetCurrentPage(page.GetIndex());
1102
1103 }
1104
1105 }
1106
1107 }
1108
1109 _pdfdoc.Unlock();
1110
1111 }
1112
1113
1114
1115 // Populate the bookmark tree control with bookmark items.
1116
1117 static void BuildBookmarkTree(Bookmark item, TreeNodeCollection nodes)
1118
1119 {
1120
1121 for (int i=0; item.IsValid(); item=item.GetNext(), ++i)
1122
1123 {
1124
1125 TreeNode new_node = new TreeNode(item.GetTitle());
1126
1127 nodes.Add(new_node);
1128
1129 new_node.Tag = item;
1130
1131 if (item.IsOpen()) new_node.Expand();
1132
1133 if (item.HasChildren()) // Recursively add children sub-trees
1134
1135 {
1136
1137 BuildBookmarkTree(item.GetFirstChild(), new_node.Nodes);
1138
1139 }
1140
1141 }
1142
1143 }
1144
1145
1146
1147 // Handle the the layer On/OFF event
1148
1149 private void LayerTreeAfterCheck(object sender, System.Windows.Forms.TreeViewEventArgs e)
1150
1151 {
1152
1153 pdftron.PDF.OCG.Context ctx = _pdfview.GetOCGContext();
1154
1155 if (ctx == null || e.Node.Tag == null) return;
1156
1157
1158
1159 pdftron.PDF.OCG.Group ocg = (pdftron.PDF.OCG.Group) e.Node.Tag;
1160
1161 ctx.SetState(ocg, e.Node.Checked);
1162
1163 _pdfview.Update();
1164
1165 }
1166
1167
1168
1169 // Populate the layer tree control with OCG (Optional Content Group) layers.
1170
1171 static void BuildLayerTree(Obj layer_arr, pdftron.PDF.OCG.Config init_cfg, TreeNodeCollection nodes, TreeNode parent_node)
1172
1173 {
1174
1175 if (layer_arr == null || layer_arr.IsArray() == false) return;
1176
1177
1178
1179 Obj lobj;
1180
1181 int sz = layer_arr.Size();
1182
1183 for (int i=0; i<sz; ++i)
1184
1185 {
1186
1187 lobj = layer_arr.GetAt(i);
1188
1189 if (lobj.IsArray() && lobj.Size()>0)
1190
1191 {
1192
1193 TreeNode new_node = new TreeNode();
1194
1195 nodes.Add(new_node);
1196
1197 new_node.Checked = true;
1198
1199 new_node.Expand();
1200
1201
1202
1203 // Recursively add children sub-trees
1204
1205 BuildLayerTree(lobj, init_cfg, new_node.Nodes, new_node);
1206
1207 }
1208
1209 else if (i==0 && lobj.IsString())
1210
1211 {
1212
1213 parent_node.Text = lobj.GetAsPDFText();
1214
1215 }
1216
1217 else
1218
1219 {
1220
1221 pdftron.PDF.OCG.Group grp = new pdftron.PDF.OCG.Group(lobj);
1222
1223 if (grp.IsValid())
1224
1225 {
1226
1227 TreeNode new_node = new TreeNode(grp.GetName());
1228
1229 nodes.Add(new_node);
1230
1231 new_node.Tag = grp;
1232
1233 new_node.Checked = grp.GetInitialState(init_cfg);
1234
1235 }
1236
1237 }
1238
1239 }
1240
1241 }
1242
1243#endif
1244
1245
1246
1247 /// <summary>
1248
1249 /// This callback method (delegate) was registered using _pdfview.SetCurrentPageProc
1250
1251 /// in PDFViewCS constructor. The callback can be used to update the current page number
1252
1253 /// within GUI applications etc. In this case we update the status bar in the main form.
1254
1255 /// </summary>
1256
1257 public static void UpdateStatusBar(int current_page, int num_pages, Object data)
1258
1259 {
1260
1261 if (data != null)
1262
1263 {
1264
1265 MainForm main_form = (MainForm) data;
1266
1267 main_form.UpdateStatusBar(current_page, num_pages);
1268
1269 }
1270
1271 }
1272
1273
1274
1275 protected override void OnActivated (EventArgs e)
1276
1277 {
1278
1279 if (_pdfview != null && _pdfview.GetDoc() != null)
1280
1281 {
1282
1283 MainForm main_form = (MainForm) this.MdiParent;
1284
1285 main_form.UpdateStatusBar(_pdfview.GetCurrentPage(), _pdfview.GetDoc().GetPageCount());
1286
1287 }
1288
1289 }
1290
1291
1292
1293 public void SetToolMode(PDFViewCtrl.ToolMode tool_mode, MyPDFView.CustomToolMode custom_tool_mode)
1294
1295 {
1296
1297 MyPDFView._base_tool_mode = tool_mode;
1298
1299 MyPDFView._tool_mode = custom_tool_mode;
1300
1301 _pdfview.SetToolMode(tool_mode);
1302
1303 }
1304
1305
1306
1307 public void Export()
1308
1309 {
1310
1311 ExportDialog e = new ExportDialog(this._pdfdoc);
1312
1313 e.ShowDialog();
1314
1315 }
1316
1317
1318
1319 public void CopySelectedText()
1320
1321 {
1322
1323 _pdfview.OnTextCopy(null, null);
1324
1325 }
1326
1327
1328
1329 public void SelectAll(){
1330
1331 _pdfview.SelectAll();
1332
1333 }
1334
1335
1336
1337 public void DeselectAll(){
1338
1339 _pdfview.ClearSelection();
1340
1341 }
1342
1343
1344
1345 public void FindText()
1346
1347 {
1348
1349 _pdfview.Find(); // Use the build in Find text dialog.
1350
1351 }
1352
1353
1354
1355 public void Print()
1356
1357 {
1358
1359 try
1360
1361 {
1362
1363#if CUSTOM_PRINT
1364
1365 PrintDialog print_dlg = new PrintDialog();
1366
1367 print_dlg.AllowSomePages = true;
1368
1369 print_dlg.Document = _print_doc;
1370
1371 if (print_dlg.ShowDialog() == DialogResult.OK)
1372
1373 {
1374
1375 _pdfdraw = new PDFDraw();
1376
1377 _pdfdraw.SetPrintMode(true);
1378
1379 _print_doc.Print();
1380
1381 _pdfdraw.Dispose();
1382
1383 _pdfdraw = null;
1384
1385 }
1386
1387#else
1388
1389 _pdfview.Print(); // Use built in PDFPrint function.
1390
1391#endif
1392
1393 }
1394
1395 catch (Exception ex)
1396
1397 {
1398
1399 MessageBox.Show(ex.ToString());
1400
1401 }
1402
1403 }
1404
1405
1406
1407#if CUSTOM_PRINT
1408
1409 // In this sample, PDFDraw object is used to implement print support.
1410
1411 private PDFDraw _pdfdraw = null;
1412
1413 private PageIterator print_page_itr;
1414
1415 private System.Drawing.Printing.PrintDocument _print_doc;
1416
1417
1418
1419 private void OnBeginPDFPrint(object sender, PrintEventArgs ev)
1420
1421 {
1422
1423 PDFDoc pdfdoc = GetPDFDoc();
1424
1425 if (pdfdoc == null)
1426
1427 {
1428
1429 MessageBox.Show("Error: Print document is not selected.");
1430
1431 return;
1432
1433 }
1434
1435
1436
1437 print_page_itr = pdfdoc.GetPageIterator();
1438
1439
1440
1441 // PDFNet includes two different rasterizer implementations.
1442
1443 //
1444
1445 // The two implementations offer a trade-off between print
1446
1447 // speed and accuracy/quality, as well as a trade-off between
1448
1449 // vector and raster output.
1450
1451 //
1452
1453 // e_GDIPlus rasterizer can be used to render the page
1454
1455 // using Windows GDI+, whereas e_BuiltIn rasterizer can
1456
1457 // be used to render bitmaps using platform-independent
1458
1459 // graphics engine (in this case images are always converted
1460
1461 // to bitmap prior to printing).
1462
1463 _pdfdraw.SetRasterizerType(PDFRasterizer.Type.e_GDIPlus);
1464
1465
1466
1467 // You can uncomment the following lines in order to use
1468
1469 // built-in, platform-independent rasterizer instead of GDI+.
1470
1471 // pdfdraw.SetRasterizerType(PDFRasterizer.Type.e_BuiltIn);
1472
1473 // pdfdraw.SetDPI(200);
1474
1475 }
1476
1477
1478
1479#if NET_1_1
1480
1481 [System.Runtime.InteropServices.DllImport("gdi32.dll")]
1482
1483 private static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
1484
1485 private const int PHYSICALOFFSETX = 112;
1486
1487 private const int PHYSICALOFFSETY = 113;
1488
1489#endif
1490
1491
1492
1493 private void OnPrintPDFPage(object sender, PrintPageEventArgs ev)
1494
1495 {
1496
1497 Graphics gr = ev.Graphics;
1498
1499 gr.PageUnit = GraphicsUnit.Inch;
1500
1501
1502
1503 bool use_hard_margins = false;
1504
1505
1506
1507 Rectangle rectPage = ev.PageBounds; //print without margins
1508
1509 //Rectangle rectPage = ev.MarginBounds; //print using margins
1510
1511 double left, right, top, bottom;
1512
1513
1514
1515 if (use_hard_margins) // You could adjust the rectangle to account for hard and soft margins, etc.
1516
1517 {
1518
1519#if NET_1_1
1520
1521 // This code is used to obtain printer hard margins when running on .NET 1.1x or below.
1522
1523 IntPtr hdc = new IntPtr();
1524
1525 hdc = ev.Graphics.GetHdc(); // Get handle to device context.
1526
1527
1528
1529 double hardMarginX = GetDeviceCaps(hdc, PHYSICALOFFSETX);
1530
1531 double hardMarginY = GetDeviceCaps(hdc, PHYSICALOFFSETY);
1532
1533 ev.Graphics.ReleaseHdc(hdc); // Release handle to device context.
1534
1535#else
1536
1537 // If you are running on .NET Framework 2.x or above, you can directly access 'hard margin' property.
1538
1539 double hardMarginX = ev.PageSettings.HardMarginX;
1540
1541 double hardMarginY = ev.PageSettings.HardMarginY;
1542
1543#endif
1544
1545 left = (rectPage.Left - hardMarginX) / 100.0;
1546
1547 right = (rectPage.Right - hardMarginX) / 100.0;
1548
1549 top = (rectPage.Top - hardMarginY) / 100.0;
1550
1551 bottom = (rectPage.Bottom - hardMarginY) / 100.0;
1552
1553 }
1554
1555 else
1556
1557 {
1558
1559 left= rectPage.Left / 100.0;
1560
1561 right = rectPage.Right / 100.0;
1562
1563 top = rectPage.Top / 100.0;
1564
1565 bottom= rectPage.Bottom / 100.0;
1566
1567 }
1568
1569
1570
1571 // The above page dimensions are in inches. We need to convert
1572
1573 // the page dimensions to PDF units (or points). One point is
1574
1575 // 1/72 of an inch.
1576
1577 pdftron.PDF.Rect rect = new Rect(left*72, bottom*72, right*72, top*72);
1578
1579
1580
1581 PDFDoc pdfdoc = GetPDFDoc();
1582
1583 if (pdfdoc == null)
1584
1585 {
1586
1587 MessageBox.Show("Error: Print document is not selected.");
1588
1589 return;
1590
1591 }
1592
1593 pdfdoc.Lock();
1594
1595
1596
1597 try
1598
1599 {
1600
1601 if (print_page_itr.HasNext())
1602
1603 {
1604
1605 _pdfdraw.DrawInRect(print_page_itr.Current(), gr, rect);
1606
1607
1608
1609 // Move to the next page, or finish printing
1610
1611 print_page_itr.Next();
1612
1613 ev.HasMorePages = print_page_itr.HasNext();
1614
1615 }
1616
1617 else ev.HasMorePages = false;
1618
1619 }
1620
1621 catch (Exception ex)
1622
1623 {
1624
1625 MessageBox.Show("Printing Error: " + ex.ToString());
1626
1627 }
1628
1629
1630
1631 pdfdoc.Unlock();
1632
1633 }
1634
1635#endif
1636
1637
1638
1639 private void InitializeComponent()
1640
1641 {
1642
1643 System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(PDFViewForm));
1644
1645 //
1646
1647 // PDFViewForm
1648
1649 //
1650
1651 this.BackColor = System.Drawing.SystemColors.Control;
1652
1653 this.Cursor = System.Windows.Forms.Cursors.Hand;
1654
1655 this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
1656
1657 this.KeyPreview = true;
1658
1659 this.Name = "PDFViewForm";
1660
1661 this.Text = "PDFViewForm";
1662
1663 this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
1664
1665 }
1666
1667 }
1668
1669}
1Imports System
2
3Imports System.Drawing
4
5Imports System.Drawing.Drawing2D
6
7Imports System.Drawing.Printing
8
9Imports System.Collections
10
11Imports System.ComponentModel
12
13Imports System.Windows.Forms
14
15Imports PDFTRON
16
17Imports PDFTRON.PDF
18
19Imports PDFTRON.Common
20
21
22
23Public Class PDFViewForm
24
25 Inherits System.Windows.Forms.Form
26
27
28
29 ' <summary>
30
31 ' Required designer variable.
32
33 ' </summary>
34
35 Private components As System.ComponentModel.Container = Nothing
36
37#If CUSTOM_NAV Then
38
39 'The following variables are used for custom page navigation pane...
40
41 Private _pdfdoc_tab As System.Windows.Forms.TabControl
42
43 Private _bookmarks_tab As System.Windows.Forms.TabPage
44
45 Private _pages_tab As System.Windows.Forms.TabPage
46
47 Private _layers_tab As System.Windows.Forms.TabPage
48
49 Private bookmark_tree As System.Windows.Forms.TreeView
50
51 Private layer_tree As System.Windows.Forms.TreeView
52
53 Private splitter1 As System.Windows.Forms.Splitter
54
55 Private _thumbview As ThumbView = Nothing 'Custom thumbnail view.
56
57#End If
58
59
60
61 Private _pdfdoc As PDFDoc = Nothing ' Currently open PDF document.
62
63 Private _pdfview As MyPDFView = Nothing ' Main PDF view.
64
65
66
67 Public Sub New(ByVal main_form As MainForm)
68
69 Me.MdiParent = main_form
70
71
72
73 ' Create the main PDFViewCtrl control (we do it here manually for greater control)
74
75 _pdfview = New MyPDFView(Me)
76
77 _pdfview.Location = New System.Drawing.Point(0, 0) 'Added 'System.Drawing' to distinguish from PDF.Point to resolve compilaiton error
78
79 _pdfview.Dock = System.Windows.Forms.DockStyle.Fill
80
81
82
83 'Optional: Set the error and current page delegates...
84
85 _pdfview.SetErrorReportHandler(AddressOf ErrorMsg, Nothing)
86
87 _pdfview.SetCurrentPageHandler(AddressOf UpdateStatusBar, main_form)
88
89 Controls.Add(_pdfview)
90
91
92
93 ' Create other controls created using Windows Form Designer
94
95 InitializeComponent()
96
97 End Sub
98
99
100
101 Public Function OpenPDF(ByVal filename As String) As Boolean
102
103 Try
104
105 Try
106
107 ' Try to open as a PDF document
108
109 _pdfdoc = New PDFDoc(filename)
110
111 Catch ex As Exception
112
113 ' Try to open as a PNG, JPEG, TIF, BMP, GIF, etc.
114
115 _pdfdoc = OpenImage(filename)
116
117 If _pdfdoc Is Nothing Then
118
119 ' rethrow the original exception
120
121 Throw ex
122
123 End If
124
125 End Try
126
127 If Not _pdfdoc.InitSecurityHandler Then ' In case _pdfdoc is encrypted
128
129 MessageBox.Show("Document authentication error", "PDFViewCtrl Error")
130
131 Return False
132
133 End If
134
135#If CUSTOM_NAV Then
136
137 ' Populates a custom bookmark tree control with bookmark nodes (if any).
138
139 Dim root As Bookmark = _pdfdoc.GetFirstBookmark()
140
141 If root.IsValid Then
142
143 bookmark_tree.BeginUpdate()
144
145 FillBookmarkTree(root, bookmark_tree.Nodes)
146
147 bookmark_tree.EndUpdate()
148
149 Else
150
151 ' Optional: Uncomment the following line to hide the bookmark
152
153 ' tab if the document does not contain bookmarks?:
154
155 ' _pdfdoc_tab.Hide()
156
157 End If
158
159#End If
160
161 ' Optional: Set page view and page presentation mode.
162
163 ' _pdfview.SetPagePresentationMode(PDFViewCtrl.PagePresentationMode.e_single_page)
164
165 ' _pdfview.SetPageViewMode(PDFViewCtrl.PageViewMode.e_fit_page)
166
167
168
169 If _pdfdoc.GetPageCount() > 2000 Then
170
171 ' If the document has many pages use single page mode to seed up initial rendering.
172
173 _pdfview.SetPagePresentationMode(PDFViewCtrl.PagePresentationMode.e_single_page)
174
175 End If
176
177
178
179 _pdfview.SetDoc(_pdfdoc)
180
181#If CUSTOM_NAV Then
182
183 _thumbview.SetDoc(_pdfdoc, _pdfview)
184
185#End If
186
187 SetToolMode(MyPDFView._base_tool_mode, MyPDFView._tool_mode)
188
189 Catch ex As PDFNetException
190
191 MessageBox.Show(ex.Message)
192
193 Return False
194
195 Catch ex As Exception
196
197 MessageBox.Show(ex.ToString)
198
199 Return False
200
201 End Try
202
203 Me.Text = filename ' Set the title
204
205 Return True
206
207 End Function
208
209
210
211 ' A utility function used to display other images types besides PDF
212
213 ' inside MyPDFView. This functionality can be used to display TIF, JPEG,
214
215 ' BMP, PNG, etc.
216
217 Public Function OpenImage(ByVal filename As String) As PDFDoc
218
219 Try
220
221 Dim pdfDoc As PDFDoc = New PDFDoc ' create new document
222
223 Dim f As ElementBuilder = New ElementBuilder
224
225 Dim writer As ElementWriter = New ElementWriter
226
227
228
229
230
231 Dim page As Page = pdfDoc.PageCreate ' Add a blank page
232
233 writer.Begin(page)
234
235
236
237 ' Add image to the document.
238
239 Dim img As PDFTRON.PDF.Image = PDFTRON.PDF.Image.Create(pdfDoc.GetSDFDoc(), filename)
240
241
242
243 ' get image rectangle
244
245 Dim imgBox As Rect = New Rect(0, 0, img.GetImageWidth, img.GetImageHeight)
246
247 Dim scaledBox As Rect = New Rect
248
249 Dim scaleFactor As Double
250
251 If imgBox.Height / imgBox.Width > 792 / 612 Then
252
253 scaleFactor = imgBox.Height / 792
254
255 Else
256
257 scaleFactor = imgBox.Width / 612
258
259 End If
260
261 scaledBox.x2 = imgBox.x2 / scaleFactor
262
263 scaledBox.y2 = imgBox.y2 / scaleFactor
264
265
266
267 ' set crop and media box of this page to fit with the scaled image
268
269 page.SetCropBox(scaledBox)
270
271 page.SetMediaBox(scaledBox)
272
273
274
275 ' create the image element and add it to the page
276
277 Dim width As Integer = CType(scaledBox.Width, Integer)
278
279 Dim height As Integer = CType(scaledBox.Height, Integer)
280
281 Dim offsetX As Integer = 0
282
283 Dim offsetY As Integer = 0
284
285 Dim element As Element = f.CreateImage(img, New Matrix2D(width, 0, 0, height, offsetX, offsetY))
286
287 writer.WritePlacedElement(element)
288
289
290
291 writer.End() ' Finish writing to the page
292
293 pdfDoc.PagePushBack(page)
294
295 Return pdfDoc
296
297 Catch ex As Exception
298
299 ' MessageBox.Show(ex.ToString)
300
301 Return Nothing
302
303 End Try
304
305 End Function
306
307
308
309
310
311
312
313
314
315
316
317 Public Function GetPDFDoc() As PDFDoc
318
319 If _pdfview Is Nothing Then
320
321 Return Nothing
322
323 Else
324
325 Return _pdfview.GetDoc
326
327 End If
328
329 End Function
330
331
332
333 Public Sub FitPage()
334
335 _pdfview.SetPageViewMode(PDFViewCtrl.PageViewMode.e_fit_page)
336
337 End Sub
338
339
340
341 Public Sub FitWidth()
342
343 _pdfview.SetPageViewMode(PDFViewCtrl.PageViewMode.e_fit_width)
344
345 End Sub
346
347
348
349 Public Sub ZoomIn()
350
351 _pdfview.SetZoom(_pdfview.GetZoom * 2)
352
353 End Sub
354
355
356
357 Public Sub ZoomOut()
358
359 _pdfview.SetZoom(_pdfview.GetZoom / 2)
360
361 End Sub
362
363
364
365 Public Sub FirstPage()
366
367 _pdfview.GotoFirstPage()
368
369 End Sub
370
371
372
373 Public Sub PrevPage()
374
375 _pdfview.GotoPreviousPage()
376
377 End Sub
378
379
380
381 Public Sub NextPage()
382
383 _pdfview.GotoNextPage()
384
385 End Sub
386
387
388
389 Public Sub LastPage()
390
391 _pdfview.GotoLastPage()
392
393 End Sub
394
395
396
397 Public Sub PageSingle()
398
399 _pdfview.SetPagePresentationMode(PDFViewCtrl.PagePresentationMode.e_single_page)
400
401 End Sub
402
403
404
405 Public Sub PageSingleContinuous()
406
407 _pdfview.SetPagePresentationMode(PDFViewCtrl.PagePresentationMode.e_single_continuous)
408
409 End Sub
410
411
412
413 Public Sub PageFacingContinuous()
414
415 _pdfview.SetPagePresentationMode(PDFViewCtrl.PagePresentationMode.e_facing_continuous)
416
417 End Sub
418
419
420
421 Public Sub PageFacing()
422
423 _pdfview.SetPagePresentationMode(PDFViewCtrl.PagePresentationMode.e_facing)
424
425 End Sub
426
427
428
429 Public Sub RotateClockwise()
430
431 _pdfview.RotateClockwise()
432
433 End Sub
434
435
436
437 Public Sub RotateCounterClockwise()
438
439 _pdfview.RotateCounterClockwise()
440
441 End Sub
442
443
444
445 Public Sub SetAntiAliasing(ByVal anti_alias As Boolean)
446
447 _pdfview.SetAntiAliasing(anti_alias)
448
449 _pdfview.Update()
450
451 End Sub
452
453
454
455 Public Sub SetRasterizer(ByVal built_in As Boolean)
456
457 If built_in Then
458
459 _pdfview.SetRasterizerType(PDFRasterizer.Type.e_BuiltIn)
460
461 Else
462
463 _pdfview.SetRasterizerType(PDFRasterizer.Type.e_GDIPlus)
464
465 End If
466
467 _pdfview.Update()
468
469 End Sub
470
471
472
473 Public Sub SetSmoothImages(ByVal smooth_images As Boolean)
474
475 _pdfview.SetImageSmoothing(smooth_images)
476
477 _pdfview.Update()
478
479 End Sub
480
481
482
483 Public Sub Save(ByVal filename As String)
484
485 If _pdfdoc Is Nothing Then
486
487 Return
488
489 End If
490
491
492
493 _pdfdoc.Lock()
494
495 Try
496
497 If Not (_pdfview Is Nothing) Then
498
499 ' Check if there are any annotations that need to
500
501 ' be merged with the document.
502
503 If Not (_pdfview._freehand_markup Is Nothing) Then
504
505 For Each itr As DictionaryEntry In _pdfview._freehand_markup
506
507 Dim annot_page_num As Integer = CType(itr.Key, Integer)
508
509 Dim pg As Page = _pdfdoc.GetPage(annot_page_num)
510
511 If Not pg Is Nothing Then
512
513 Dim annot As FreeHandAnnot = CType(itr.Value, FreeHandAnnot)
514
515 annot.DrawAnnots(pg, Pens.Red)
516
517 End If
518
519 Next
520
521 _pdfview._freehand_markup = Nothing
522
523 _pdfview.Invalidate()
524
525 _pdfview.Update()
526
527 End If
528
529 End If
530
531 _pdfdoc.Save(filename, PDFTRON.SDF.SDFDoc.SaveOptions.e_remove_unused)
532
533 Catch ex As Exception
534
535 MessageBox.Show(ex.ToString(), "Error during the Save")
536
537 End Try
538
539
540
541 _pdfdoc.Unlock()
542
543 End Sub
544
545
546
547 Public Sub SaveAs()
548
549
550
551 If Not (_pdfdoc Is Nothing) AndAlso Not (_pdfview Is Nothing) Then
552
553 'opens a save dialog
554
555 Dim dlg As SaveFileDialog = New SaveFileDialog
556
557 dlg.Filter = "PDF Files (*.pdf)|*.pdf|All Files (*.*)|*.*"
558
559 dlg.DefaultExt = ".pdf"
560
561 dlg.FileName = Text
562
563 Dim res As DialogResult = dlg.ShowDialog
564
565 If res = DialogResult.OK Then
566
567 'saves the file
568
569 Me.Save(dlg.FileName)
570
571 End If
572
573 End If
574
575 End Sub
576
577
578
579 Protected Overloads Overrides Sub OnClosing(ByVal e As CancelEventArgs)
580
581 'called when user closes a document
582
583 If Not (_pdfdoc Is Nothing) AndAlso Not (_pdfview Is Nothing) AndAlso (_pdfdoc.IsModified OrElse Not (_pdfview._freehand_markup Is Nothing)) Then
584
585 'if the document exists and is modified then ask the user
586
587 'whether or not to save it
588
589 Dim save As DialogResult = MessageBox.Show("Would you like to save the changes to the document?", "PDFViewCtrl", MessageBoxButtons.YesNoCancel)
590
591 If save = DialogResult.Yes Then
592
593 'opens a save dialog
594
595 Dim dlg As SaveFileDialog = New SaveFileDialog
596
597 dlg.Filter = "PDF Files (*.pdf)|*.pdf|All Files (*.*)|*.*"
598
599 dlg.DefaultExt = ".pdf"
600
601 dlg.FileName = Text
602
603 Dim res As DialogResult = dlg.ShowDialog
604
605 If res = DialogResult.OK Then
606
607 'saves the file
608
609 Me.Save(dlg.FileName)
610
611 Else
612
613 If res = DialogResult.Cancel Then
614
615 e.Cancel = True
616
617 End If
618
619 End If
620
621 Else
622
623 If save = DialogResult.Cancel Then
624
625 e.Cancel = True
626
627 End If
628
629 End If
630
631 MyBase.OnClosing(e)
632
633 End If
634
635 End Sub
636
637
638
639 ' <summary>
640
641 ' Clean up any resources being used.
642
643 ' </summary>
644
645 Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
646
647 If disposing Then
648
649#If CUSTOM_NAV Then
650
651 _thumbview.Dispose() ' Close the thumbnail view.
652
653#End If
654
655 _pdfview.Dispose() ' Close the open PDF document in the view.
656
657 _pdfdoc.Dispose() ' Close the PDF document.
658
659 If Not (components Is Nothing) Then
660
661 components.Dispose()
662
663 End If
664
665 End If
666
667 MyBase.Dispose(disposing)
668
669 End Sub
670
671
672
673 ' <summary>
674
675 ' This callback method (delegate) was registered using _pdfview.SetErrorReportProc
676
677 ' in PDFViewCS constructor. The callback can be used to report any errors that may
678
679 ' occur during page rendering.
680
681 ' </summary>
682
683 Public Shared Sub ErrorMsg(ByVal message As String, ByVal obj As Object)
684
685 MessageBox.Show(message, "PDFViewCtrl Error")
686
687 End Sub
688
689#If CUSTOM_NAV Then 'Custom bookmark and PDF layer navigation sample
690
691 ' Handle the the bookmark select event (i.e. when the user selects a node in the bookmark tree).
692
693 Private Sub BookmarkTreeAfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles bookmark_tree.AfterSelect
694
695 _pdfdoc.Lock()
696
697 Dim item As Bookmark = CType(e.Node.Tag, Bookmark)
698
699 Dim action As Action = item.GetAction
700
701 If action.IsValid Then ' Handle goto actions.
702
703 ' Other types of actions can be handled in similar way.
704
705 If action.GetType = action.Type.e_GoTo Then
706
707 Dim dest As Destination = action.GetDest
708
709 If dest.IsValid() Then
710
711 Dim page As Page = dest.GetPage
712
713 If Not page Is Nothing Then
714
715 _pdfview.SetCurrentPage(page.GetIndex)
716
717 End If
718
719 End If
720
721 End If
722
723 End If
724
725 _pdfdoc.Unlock()
726
727 End Sub
728
729
730
731 ' Populate the tree control with bookmark items.
732
733 Shared Sub FillBookmarkTree(ByVal item As Bookmark, ByVal nodes As TreeNodeCollection)
734
735 Dim i As Integer = 0
736
737 While item.IsValid
738
739 Dim new_node As TreeNode = New TreeNode(item.GetTitle)
740
741 nodes.Add(new_node)
742
743 new_node.Tag = item
744
745 If item.IsOpen Then
746
747 new_node.Expand()
748
749 End If
750
751 If item.HasChildren Then ' Recursively add children sub-trees
752
753 FillBookmarkTree(item.GetFirstChild, new_node.Nodes)
754
755 End If
756
757 item = item.GetNext
758
759 i = i + 1
760
761 End While
762
763 End Sub
764
765
766
767#End If
768
769
770
771 ' <summary>
772
773 ' This callback method (delegate) was registered using _pdfview.SetCurrentPageProc
774
775 ' in PDFViewCS constructor. The callback can be used to update the current page number
776
777 ' within GUI applications etc. In this case we update the status bar in the main form.
778
779 ' </summary>
780
781 Public Shared Sub UpdateStatusBar(ByVal current_page As Integer, ByVal num_pages As Integer, ByVal data As Object)
782
783 If Not (data Is Nothing) Then
784
785 Dim main_form As MainForm = CType(data, MainForm)
786
787 main_form._current_page = current_page
788
789 main_form._num_pages = num_pages
790
791 main_form.UpdateStatusBar(current_page, num_pages)
792
793 End If
794
795 End Sub
796
797
798
799 Public Sub SetToolMode(ByVal tool_mode As MyPDFView.CustomToolMode, ByVal custom_tool_mode As MyPDFView.CustomToolMode)
800
801 ' Set the custom tool mode.
802
803 MyPDFView._base_tool_mode = tool_mode
804
805 MyPDFView._tool_mode = custom_tool_mode
806
807 'Chaged by Kay April1------------------------
808
809 'Set built-in tool mode (pan, text select, or custom)
810
811 'Dim tm As PDFViewCtrl.ToolMode
812
813 'If (tool_mode = MyPDFView.CustomToolMode.e_pan) Then
814
815 ' tm = PDFViewCtrl.ToolMode.e_pan
816
817 'ElseIf (tool_mode = MyPDFView.CustomToolMode.e_text_struct_select) Then
818
819 ' tm = PDFViewCtrl.ToolMode.e_text_struct_select
820
821 'ElseIf (tool_mode = MyPDFView.CustomToolMode.e_text_rect_select) Then
822
823 ' tm = PDFViewCtrl.ToolMode.e_text_rect_select
824
825 'Else
826
827 ' tm = PDFViewCtrl.ToolMode.e_custom
828
829 'End If
830
831 'Chaged by Kay April1------------------------
832
833 _pdfview.SetToolMode(tool_mode)
834
835 End Sub
836
837
838
839 Protected Overloads Overrides Sub OnActivated(ByVal e As EventArgs)
840
841 If Not (_pdfview Is Nothing) Then
842
843 Dim main_form As MainForm = CType(Me.MdiParent, MainForm)
844
845 main_form._current_page = _pdfview.GetCurrentPage
846
847 main_form._num_pages = _pdfview.GetDoc.GetPageCount
848
849 main_form.UpdateStatusBar(_pdfview.GetCurrentPage(), _pdfview.GetDoc().GetPageCount())
850
851 End If
852
853 End Sub
854
855
856
857 Private Sub InitializeComponent()
858
859 Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(PDFViewForm))
860
861
862
863 'PDFViewForm
864
865 '
866
867 Me.BackColor = System.Drawing.SystemColors.Control
868
869 Me.Cursor = System.Windows.Forms.Cursors.Hand
870
871 Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
872
873 Me.KeyPreview = True
874
875 Me.Name = "PDFViewForm"
876
877 Me.Text = "PDFViewForm"
878
879 Me.WindowState = System.Windows.Forms.FormWindowState.Maximized
880
881 End Sub
882
883
884
885 Public Sub Export()
886
887 Dim temp As ExportDialog = New ExportDialog(Me._pdfdoc)
888
889 End Sub
890
891
892
893 Public Sub CopySelectedText()
894
895 _pdfview.OnTextCopy(Nothing, Nothing)
896
897 End Sub
898
899
900
901 Public Sub SelectAll()
902
903 _pdfview.SelectAll()
904
905 End Sub
906
907
908
909 Public Sub DeselectAll()
910
911 _pdfview.ClearSelection()
912
913 End Sub
914
915
916
917 Public Sub FindText()
918
919 _pdfview.Find() 'Use the build in Find text dialog.
920
921 End Sub
922
923
924
925
926
927 ' Private members used for print support -------------------------
928
929
930
931 ' In this sample, PDFDraw object is used to implement print support.
932
933 Private pdfdraw As pdfdraw = Nothing
934
935 Private print_page_itr As PageIterator
936
937
938
939 Public Sub Print()
940
941 Try
942
943#If CUSTOM_PRINT Then
944
945 Dim print_dlg As PrintDialog = New PrintDialog
946
947 print_dlg.AllowSomePages = True
948
949 print_dlg.Document = print_doc
950
951 If (print_dlg.ShowDialog() = DialogResult.OK) Then
952
953 pdfdraw = new PDFDraw
954
955 pdfdraw.SetPrintMode(True)
956
957 _print_doc.Print()
958
959 pdfdraw.Dispose()
960
961 pdfdraw = Nothing
962
963 End If
964
965#Else
966
967 _pdfview.Print()
968
969#End If
970
971 Catch ex As Exception
972
973 MessageBox.Show(ex.ToString())
974
975 End Try
976
977 End Sub
978
979
980
981 '' Me.print_doc.BeginPrint += New System.Drawing.Printing.PrintEventHandler(this.OnBeginPDFPrint)
982
983 ' Me.print_doc.PrintPage += New System.Drawing.Printing.PrintPageEventHandler(this.OnPrintPDFPage)
984
985#If CUSTOM_PRINT Then
986
987 Private Sub OnBeginPDFPrint(ByVal sender As System.Object, ByVal ev As System.Drawing.Printing.PrintEventArgs) Handles print_doc.BeginPrint
988
989 Dim doc As PDFDoc = GetPDFDoc()
990
991
992
993 If doc Is Nothing Then
994
995 MessageBox.Show("Error: Print document is not selected.")
996
997 Return
998
999 End If
1000
1001 print_page_itr = doc.GetPageIterator()
1002
1003
1004
1005 ' PDFNet includes two different rasterizer implementations.
1006
1007 '
1008
1009 ' The two implementations offer a trade-off between print
1010
1011 ' speed and accuracy/quality, as well as a trade-off between
1012
1013 ' vector and raster output.
1014
1015 '
1016
1017 ' e_GDIPlus rasterizer can be used to render the page
1018
1019 ' using Windows GDI+, whereas e_BuiltIn rasterizer can
1020
1021 ' be used to render bitmaps using platform-independent
1022
1023 ' graphics engine (in this case images are always converted
1024
1025 ' to bitmap prior to printing).
1026
1027
1028
1029 pdfdraw.SetRasterizerType(PDFRasterizer.Type.e_GDIPlus)
1030
1031
1032
1033 ' You can uncomment the following lines in order to use
1034
1035 ' built-in, platform-independent rasterizer instead of GDI+.
1036
1037 ' pdfdraw.SetRasterizerType(PDFRasterizer.Type.e_BuiltIn)
1038
1039 ' pdfdraw.SetDPI(200)
1040
1041
1042
1043 End Sub
1044
1045 #if NET_1_1
1046
1047 <System.Runtime.InteropServices.DllImport("gdi32.dll")> Private Shared Function GetDeviceCaps(ByVal hdc As IntPtr, ByVal nIndex As Integer) As Integer
1048
1049 End Function
1050
1051 Private Const PHYSICALOFFSETX As Integer = 112
1052
1053 Private Const PHYSICALOFFSETY As Integer = 113
1054
1055 #endif
1056
1057 Private Sub OnPrintPDFPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)'Handles print_doc.PrintPage
1058
1059
1060
1061 Dim gr As Graphics = ev.Graphics
1062
1063 gr.PageUnit = GraphicsUnit.Inch
1064
1065
1066
1067 Dim use_hard_margins As Boolean =False
1068
1069 Dim rectPage As Rectangle=ev.PageBounds 'print without margins
1070
1071 Dim left As Double
1072
1073 Dim right As Double
1074
1075 Dim top As Double
1076
1077 Dim bottom As Double
1078
1079
1080
1081 If use_hard_margins Is True Then
1082
1083#if NET_1_1
1084
1085 ' This code is used to obtain printer hard margins when running on .NET 1.1x or below.
1086
1087 Dim hdc As IntPtr = new IntPtr
1088
1089 hdc = ev.Graphics.GetHdc(); 'Get handle to device context.
1090
1091
1092
1093 Dim hardMarginX As Double = GetDeviceCaps(hdc, PHYSICALOFFSETX)
1094
1095 Dim hardMarginY As Double = GetDeviceCaps(hdc, PHYSICALOFFSETY)
1096
1097 ev.Graphics.ReleaseHdc(hdc) 'Release handle to device context.
1098
1099#else
1100
1101 ' If you are running on .NET Framework 2.x or above, you can directly access 'hard margin' property.
1102
1103 Dim hardMarginX As Double = ev.PageSettings.HardMarginX
1104
1105 Dim hardMarginY As Double = ev.PageSettings.HardMarginY
1106
1107#endif
1108
1109 left = (rectPage.Left - hardMarginX) / 100.0
1110
1111 right = (rectPage.Right - hardMarginX) / 100.0
1112
1113 top = (rectPage.Top - hardMarginY) / 100.0
1114
1115 bottom = (rectPage.Bottom - hardMarginY) / 100.0
1116
1117 Else
1118
1119 left= rectPage.Left / 100.0;
1120
1121 right = rectPage.Right / 100.0;
1122
1123 top = rectPage.Top / 100.0;
1124
1125 bottom= rectPage.Bottom / 100.0;
1126
1127 End If
1128
1129
1130
1131 'The above page dimensions are in inches. We need to convert
1132
1133 'the page dimensions to PDF units (or points). One point is
1134
1135 '1/72 of an inch.
1136
1137 Dim rect As pdftron.PDF.Rect = new Rect(left*72, bottom*72, right*72, top*72)
1138
1139
1140
1141 Dime pdfdoc As PDFDoc = GetPDFDoc()
1142
1143 pdfdoc.Lock()
1144
1145
1146
1147 Try
1148
1149 If print_page_itr.HasNext() Then
1150
1151
1152
1153 _pdfdraw.DrawInRect(print_page_itr.Current(), gr, rect)
1154
1155
1156
1157 'Move to the next page, or finish printing
1158
1159 print_page_itr.Next()
1160
1161 ev.HasMorePages = print_page_itr.HasNext()
1162
1163 else ev.HasMorePages = False
1164
1165 Catch ex As Exception
1166
1167 MessageBox.Show("Printing Error: " + ex.ToString)
1168
1169 End Try
1170
1171 pdfdoc.Unlock()
1172
1173End Sub
1174
1175#End If
1176
1177
1178
1179End Class
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales