PDFView

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}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales