PDFViewControl.xaml.cs 8.0 KB

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