PDFViewControl.xaml.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using ComPDFKit.Import;
  2. using ComPDFKit.PDFAnnotation;
  3. using ComPDFKit.PDFAnnotation.Form;
  4. using ComPDFKit.PDFDocument;
  5. using ComPDFKit.PDFDocument.Action;
  6. using ComPDFKit.PDFPage;
  7. using ComPDFKitViewer.AnnotEvent;
  8. using ComPDFKitViewer.PdfViewer;
  9. using Microsoft.Win32;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Diagnostics;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Windows;
  16. using System.Windows.Controls;
  17. using System.Windows.Media.Imaging;
  18. namespace Compdfkit_Tools.PDFControl
  19. {
  20. public partial class PDFViewControl : UserControl
  21. {
  22. #region Properties
  23. public CPDFViewer PDFView { get; set; }
  24. public bool CustomSignHandle { get; set; }
  25. public string Password { get; set; }
  26. private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  27. #endregion
  28. public PDFViewControl()
  29. {
  30. InitializeComponent();
  31. PDFView = new CPDFViewer();
  32. Content = PDFView;
  33. PDFView.MouseWheelZoomHandler += PDFView_MouseWheelZoomHandler;
  34. PDFView.PDFActionHandler += PDFView_PDFActionHandler;
  35. }
  36. #region Private Command Methods
  37. private void PDFView_PDFActionHandler(object sender, CPDFAction pdfAction)
  38. {
  39. if (pdfAction != null)
  40. {
  41. switch (pdfAction.ActionType)
  42. {
  43. case C_ACTION_TYPE.ACTION_TYPE_NAMED:
  44. {
  45. CPDFNamedAction namedAction = pdfAction as CPDFNamedAction;
  46. string namedStr = namedAction.GetName();
  47. switch (namedStr)
  48. {
  49. case "FirstPage":
  50. {
  51. PDFView?.GoToPage(0);
  52. break;
  53. }
  54. case "LastPage":
  55. {
  56. PDFView?.GoToPage(PDFView.Document.PageCount - 1);
  57. break;
  58. }
  59. case "NextPage":
  60. if (PDFView != null)
  61. {
  62. int nextIndex = PDFView.CurrentIndex + 1;
  63. if (nextIndex < PDFView.Document.PageCount)
  64. {
  65. PDFView.GoToPage(nextIndex);
  66. }
  67. }
  68. break;
  69. case "PrevPage":
  70. if (PDFView != null)
  71. {
  72. int prevIndex = PDFView.CurrentIndex - 1;
  73. if (prevIndex >= 0)
  74. {
  75. PDFView.GoToPage(prevIndex);
  76. }
  77. }
  78. break;
  79. default:
  80. break;
  81. }
  82. break;
  83. }
  84. case C_ACTION_TYPE.ACTION_TYPE_GOTO:
  85. if (PDFView != null)
  86. {
  87. CPDFGoToAction gotoAction = pdfAction as CPDFGoToAction;
  88. CPDFDestination dest = gotoAction.GetDestination(PDFView.Document);
  89. if (dest != null)
  90. PDFView.GoToPage(dest.PageIndex, new Point(0, 0));
  91. }
  92. break;
  93. case C_ACTION_TYPE.ACTION_TYPE_GOTOR:
  94. if (PDFView != null)
  95. {
  96. CPDFGoToRAction gotorAction = pdfAction as CPDFGoToRAction;
  97. CPDFDestination dest = gotorAction.GetDestination(PDFView.Document);
  98. if (dest != null)
  99. {
  100. PDFView.GoToPage(dest.PageIndex, new Point(0, 0));
  101. }
  102. }
  103. break;
  104. case C_ACTION_TYPE.ACTION_TYPE_URI:
  105. {
  106. CPDFUriAction uriAction = pdfAction as CPDFUriAction;
  107. string uri = uriAction.GetUri();
  108. try
  109. {
  110. if (!string.IsNullOrEmpty(uri))
  111. {
  112. Process.Start(uri);
  113. }
  114. }
  115. catch (Exception ex)
  116. {
  117. }
  118. }
  119. break;
  120. default:
  121. break;
  122. }
  123. }
  124. }
  125. private void PDFView_MouseWheelZoomHandler(object sender, bool e)
  126. {
  127. double newZoom = CheckZoomLevel(PDFView.ZoomFactor + (e ? 0.01 : -0.01), e);
  128. PDFView.Zoom(newZoom);
  129. }
  130. private double CheckZoomLevel(double zoom, bool IsGrowth)
  131. {
  132. double standardZoom = 100;
  133. if (zoom <= 0.01)
  134. {
  135. return 0.01;
  136. }
  137. if (zoom >= 10)
  138. {
  139. return 10;
  140. }
  141. zoom *= 100;
  142. for (int i = 0; i < zoomLevelList.Length - 1; i++)
  143. {
  144. if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth)
  145. {
  146. standardZoom = zoomLevelList[i + 1];
  147. break;
  148. }
  149. if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth)
  150. {
  151. standardZoom = zoomLevelList[i];
  152. break;
  153. }
  154. }
  155. return standardZoom / 100;
  156. }
  157. #endregion
  158. #region Public Methods
  159. public bool CheckHasForm()
  160. {
  161. if (PDFView == null || PDFView.Document == null)
  162. return false;
  163. var document = PDFView.Document;
  164. for (int i = 0; i < document.PageCount; i++)
  165. {
  166. CPDFPage page = document.PageAtIndex(i, false);
  167. List<CPDFAnnotation> annotList = page.GetAnnotations();
  168. if (annotList == null || annotList.Count < 1)
  169. continue;
  170. List<CPDFWidget> formList = annotList.AsEnumerable().
  171. Where(x => x.Type == C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET)
  172. .Cast<CPDFWidget>()
  173. .ToList();
  174. if (formList.Count > 0)
  175. return true;
  176. }
  177. return false;
  178. }
  179. #endregion
  180. }
  181. }