PDFViewControl.xaml.cs 9.1 KB

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