PDFViewControl.xaml.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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; private 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. PDFView.WidgetClickHandler += PDFView_WidgetClickHandler;
  33. }
  34. private void PDFView_WidgetClickHandler(object sender, WidgetArgs e)
  35. {
  36. if ((e is WidgetSignArgs) && CustomSignHandle == false)
  37. {
  38. try
  39. {
  40. WidgetSignArgs signArgs = (WidgetSignArgs)e;
  41. OpenFileDialog openFileDialog = new OpenFileDialog();
  42. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  43. if (openFileDialog.ShowDialog() == true)
  44. {
  45. string ImagePath = openFileDialog.FileName;
  46. using (FileStream fileData = File.OpenRead(ImagePath))
  47. {
  48. string fileExt = System.IO.Path.GetExtension(ImagePath).ToLower();
  49. BitmapFrame frame = null;
  50. BitmapDecoder decoder = BitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
  51. if (decoder != null && decoder.Frames.Count > 0)
  52. {
  53. frame = decoder.Frames[0];
  54. }
  55. if (frame != null)
  56. {
  57. byte[] ImageArray = new byte[frame.PixelWidth * frame.PixelHeight * 4];
  58. int ImageWidth = frame.PixelWidth;
  59. int ImageHeight = frame.PixelHeight;
  60. frame.CopyPixels(ImageArray, frame.PixelWidth * 4, 0);
  61. signArgs.UpdateApWithImage(ImageArray, ImageWidth, ImageHeight, C_Scale_Type.fitCenter, 0);
  62. }
  63. }
  64. }
  65. }
  66. catch (Exception ex)
  67. {
  68. }
  69. }
  70. }
  71. private void PDFView_PDFActionHandler(object sender, CPDFAction pdfAction)
  72. {
  73. if (pdfAction != null)
  74. {
  75. switch (pdfAction.ActionType)
  76. {
  77. case C_ACTION_TYPE.ACTION_TYPE_NAMED:
  78. {
  79. CPDFNamedAction namedAction = pdfAction as CPDFNamedAction;
  80. string namedStr = namedAction.GetName();
  81. switch (namedStr)
  82. {
  83. case "FirstPage":
  84. {
  85. PDFView?.GoToPage(0);
  86. break;
  87. }
  88. case "LastPage":
  89. {
  90. PDFView?.GoToPage(PDFView.Document.PageCount - 1);
  91. break;
  92. }
  93. case "NextPage":
  94. if (PDFView != null)
  95. {
  96. int nextIndex = PDFView.CurrentIndex + 1;
  97. if (nextIndex < PDFView.Document.PageCount)
  98. {
  99. PDFView.GoToPage(nextIndex);
  100. }
  101. }
  102. break;
  103. case "PrevPage":
  104. if (PDFView != null)
  105. {
  106. int prevIndex = PDFView.CurrentIndex - 1;
  107. if (prevIndex >= 0)
  108. {
  109. PDFView.GoToPage(prevIndex);
  110. }
  111. }
  112. break;
  113. default:
  114. break;
  115. }
  116. break;
  117. }
  118. case C_ACTION_TYPE.ACTION_TYPE_GOTO:
  119. if (PDFView != null)
  120. {
  121. CPDFGoToAction gotoAction = pdfAction as CPDFGoToAction;
  122. CPDFDestination dest = gotoAction.GetDestination(PDFView.Document);
  123. if (dest != null)
  124. PDFView.GoToPage(dest.PageIndex, new Point(0, 0));
  125. }
  126. break;
  127. case C_ACTION_TYPE.ACTION_TYPE_GOTOR:
  128. if (PDFView != null)
  129. {
  130. CPDFGoToRAction gotorAction = pdfAction as CPDFGoToRAction;
  131. CPDFDestination dest = gotorAction.GetDestination(PDFView.Document);
  132. if (dest != null)
  133. {
  134. PDFView.GoToPage(dest.PageIndex, new Point(0, 0));
  135. }
  136. }
  137. break;
  138. case C_ACTION_TYPE.ACTION_TYPE_URI:
  139. {
  140. CPDFUriAction uriAction = pdfAction as CPDFUriAction;
  141. string uri = uriAction.GetUri();
  142. try
  143. {
  144. if (!string.IsNullOrEmpty(uri))
  145. {
  146. Process.Start(uri);
  147. }
  148. }
  149. catch (Exception ex)
  150. {
  151. }
  152. }
  153. break;
  154. default:
  155. break;
  156. }
  157. }
  158. }
  159. private void PDFView_MouseWheelZoomHandler(object sender, bool e)
  160. {
  161. double newZoom = CheckZoomLevel(PDFView.ZoomFactor + (e ? 0.01 : -0.01), e);
  162. PDFView.Zoom(newZoom);
  163. }
  164. private double CheckZoomLevel(double zoom, bool IsGrowth)
  165. {
  166. double standardZoom = 100;
  167. if (zoom <= 0.01)
  168. {
  169. return 0.01;
  170. }
  171. if (zoom >= 10)
  172. {
  173. return 10;
  174. }
  175. zoom *= 100;
  176. for (int i = 0; i < zoomLevelList.Length - 1; i++)
  177. {
  178. if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth)
  179. {
  180. standardZoom = zoomLevelList[i + 1];
  181. break;
  182. }
  183. if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth)
  184. {
  185. standardZoom = zoomLevelList[i];
  186. break;
  187. }
  188. }
  189. return standardZoom / 100;
  190. }
  191. public bool CheckHasForm()
  192. {
  193. if (PDFView == null || PDFView.Document == null)
  194. return false;
  195. var document = PDFView.Document;
  196. for (int i = 0; i < document.PageCount; i++)
  197. {
  198. CPDFPage page = document.PageAtIndex(i, false);
  199. List<CPDFAnnotation> annotList = page.GetAnnotations();
  200. if (annotList == null || annotList.Count < 1)
  201. continue;
  202. List<CPDFWidget> formList = annotList.AsEnumerable().
  203. Where(x => x.Type == C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET)
  204. .Cast<CPDFWidget>()
  205. .ToList();
  206. if (formList.Count > 0)
  207. return true;
  208. }
  209. return false;
  210. }
  211. }
  212. }