PDFViewControl.xaml.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using ComPDFKit.Import;
  2. using ComPDFKit.PDFDocument;
  3. using ComPDFKit.PDFDocument.Action;
  4. using ComPDFKitViewer.AnnotEvent;
  5. using ComPDFKitViewer.PdfViewer;
  6. using Microsoft.Win32;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Diagnostics;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. using System.Windows.Data;
  17. using System.Windows.Documents;
  18. using System.Windows.Input;
  19. using System.Windows.Media;
  20. using System.Windows.Media.Imaging;
  21. using System.Windows.Navigation;
  22. using System.Windows.Shapes;
  23. namespace Compdfkit_Tools.PDFControl
  24. {
  25. /// <summary>
  26. /// PDFViewControl.xaml 的交互逻辑
  27. /// </summary>
  28. public partial class PDFViewControl : UserControl
  29. {
  30. public CPDFViewer PDFView { get;private set; }
  31. public bool CustomSignHandle { get; set; }
  32. private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  33. public PDFViewControl()
  34. {
  35. InitializeComponent();
  36. PDFView = new CPDFViewer();
  37. Content = PDFView;
  38. PDFView.MouseWheelZoomHandler += PDFView_MouseWheelZoomHandler;
  39. PDFView.PDFActionHandler += PDFView_PDFActionHandler;
  40. PDFView.WidgetClickHandler += PDFView_WidgetClickHandler;
  41. }
  42. private void PDFView_WidgetClickHandler(object sender, WidgetArgs e)
  43. {
  44. if ((e is WidgetSignArgs) && CustomSignHandle==false)
  45. {
  46. try
  47. {
  48. WidgetSignArgs signArgs = (WidgetSignArgs)e;
  49. OpenFileDialog openFileDialog = new OpenFileDialog();
  50. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  51. if (openFileDialog.ShowDialog() == true)
  52. {
  53. string ImagePath = openFileDialog.FileName;
  54. using (FileStream fileData = File.OpenRead(ImagePath))
  55. {
  56. string fileExt = System.IO.Path.GetExtension(ImagePath).ToLower();
  57. BitmapFrame frame = null;
  58. BitmapDecoder decoder = BitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
  59. if (decoder != null && decoder.Frames.Count > 0)
  60. {
  61. frame = decoder.Frames[0];
  62. }
  63. if (frame != null)
  64. {
  65. byte[] ImageArray = new byte[frame.PixelWidth * frame.PixelHeight * 4];
  66. int ImageWidth = frame.PixelWidth;
  67. int ImageHeight = frame.PixelHeight;
  68. frame.CopyPixels(ImageArray, frame.PixelWidth * 4, 0);
  69. signArgs.UpdateApWithImage(ImageArray, ImageWidth, ImageHeight, C_Scale_Type.fitCenter, 0);
  70. }
  71. }
  72. }
  73. }
  74. catch(Exception ex)
  75. {
  76. }
  77. }
  78. }
  79. private void PDFView_PDFActionHandler(object sender,CPDFAction pdfAction)
  80. {
  81. if (pdfAction != null)
  82. {
  83. switch (pdfAction.ActionType)
  84. {
  85. case C_ACTION_TYPE.ACTION_TYPE_NAMED:
  86. {
  87. CPDFNamedAction namedAction = pdfAction as CPDFNamedAction;
  88. string namedStr = namedAction.GetName();
  89. switch (namedStr)
  90. {
  91. case "FirstPage":
  92. {
  93. PDFView?.GoToPage(0);
  94. break;
  95. }
  96. case "LastPage":
  97. {
  98. PDFView?.GoToPage(PDFView.Document.PageCount - 1);
  99. break;
  100. }
  101. case "NextPage":
  102. if (PDFView != null)
  103. {
  104. int nextIndex = PDFView.CurrentIndex + 1;
  105. if (nextIndex < PDFView.Document.PageCount)
  106. {
  107. PDFView.GoToPage(nextIndex);
  108. }
  109. }
  110. break;
  111. case "PrevPage":
  112. if (PDFView != null)
  113. {
  114. int prevIndex = PDFView.CurrentIndex - 1;
  115. if (prevIndex >= 0)
  116. {
  117. PDFView.GoToPage(prevIndex);
  118. }
  119. }
  120. break;
  121. default:
  122. break;
  123. }
  124. break;
  125. }
  126. case C_ACTION_TYPE.ACTION_TYPE_GOTO:
  127. if (PDFView != null)
  128. {
  129. CPDFGoToAction gotoAction = pdfAction as CPDFGoToAction;
  130. CPDFDestination dest = gotoAction.GetDestination(PDFView.Document);
  131. if (dest != null)
  132. PDFView.GoToPage(dest.PageIndex, new Point(0, 0));
  133. }
  134. break;
  135. case C_ACTION_TYPE.ACTION_TYPE_GOTOR:
  136. if (PDFView != null)
  137. {
  138. CPDFGoToRAction gotorAction = pdfAction as CPDFGoToRAction;
  139. CPDFDestination dest = gotorAction.GetDestination(PDFView.Document);
  140. if (dest != null)
  141. {
  142. PDFView.GoToPage(dest.PageIndex, new Point(0, 0));
  143. }
  144. }
  145. break;
  146. case C_ACTION_TYPE.ACTION_TYPE_URI:
  147. {
  148. CPDFUriAction uriAction = pdfAction as CPDFUriAction;
  149. string uri = uriAction.GetUri();
  150. try
  151. {
  152. if (!string.IsNullOrEmpty(uri))
  153. {
  154. Process.Start(uri);
  155. }
  156. }
  157. catch (Exception ex)
  158. {
  159. }
  160. }
  161. break;
  162. default:
  163. break;
  164. }
  165. }
  166. }
  167. private void PDFView_MouseWheelZoomHandler(object sender, bool e)
  168. {
  169. double newZoom = CheckZoomLevel(PDFView.ZoomFactor + (e ? 0.01 : -0.01), e);
  170. PDFView.Zoom(newZoom);
  171. }
  172. private double CheckZoomLevel(double zoom, bool IsGrowth)
  173. {
  174. double standardZoom = 100;
  175. if (zoom <= 0.01)
  176. {
  177. return 0.01;
  178. }
  179. if (zoom >= 10)
  180. {
  181. return 10;
  182. }
  183. zoom *= 100;
  184. for (int i = 0; i < zoomLevelList.Length - 1; i++)
  185. {
  186. if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth)
  187. {
  188. standardZoom = zoomLevelList[i + 1];
  189. break;
  190. }
  191. if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth)
  192. {
  193. standardZoom = zoomLevelList[i];
  194. break;
  195. }
  196. }
  197. return standardZoom / 100;
  198. }
  199. }
  200. }