PDFViewControl.xaml.cs 7.0 KB

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