PDFViewControl.xaml.cs 6.8 KB

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