PDFViewControl.xaml.cs 7.1 KB

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