RegularViewerControl.xaml.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. using System;
  2. using System.ComponentModel;
  3. using System.Runtime.CompilerServices;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Input;
  7. using Compdfkit_Tools.Annotation.PDFAnnotationPanel.PDFAnnotationUI;
  8. using Compdfkit_Tools.Helper;
  9. using Compdfkit_Tools.PDFControl;
  10. using ComPDFKit.DigitalSign;
  11. using ComPDFKit.PDFAnnotation.Form;
  12. using ComPDFKitViewer;
  13. using ComPDFKit.Tool;
  14. using ComPDFKit.PDFAnnotation;
  15. namespace Compdfkit_Tools.PDFView
  16. {
  17. public partial class RegularViewerControl : UserControl, INotifyPropertyChanged
  18. {
  19. public PDFViewControl PdfViewControl;
  20. public CPDFAnnotationControl PDFAnnotationControl = new CPDFAnnotationControl();
  21. private SignatureStatusBarControl signatureStatusBarControl;
  22. private CPDFDisplaySettingsControl displaySettingsControl = null;
  23. private PanelState panelState = PanelState.GetInstance();
  24. private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  25. public event PropertyChangedEventHandler PropertyChanged;
  26. public event EventHandler<bool> OnCanSaveChanged;
  27. private bool CanSave
  28. {
  29. get
  30. {
  31. if (PdfViewControl != null && PdfViewControl.PDFViewTool.GetCPDFViewer() != null)
  32. {
  33. if (PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.CanRedo ||
  34. PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.CanUndo)
  35. {
  36. return true;
  37. }
  38. }
  39. return false;
  40. }
  41. }
  42. public RegularViewerControl()
  43. {
  44. InitializeComponent();
  45. panelState.PropertyChanged -= PanelState_PropertyChanged;
  46. panelState.PropertyChanged += PanelState_PropertyChanged;
  47. }
  48. private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
  49. {
  50. if (e.PropertyName == nameof(PanelState.IsLeftPanelExpand))
  51. {
  52. ExpandLeftPanel(panelState.IsLeftPanelExpand);
  53. }
  54. else if (e.PropertyName == nameof(PanelState.RightPanel))
  55. {
  56. if (panelState.RightPanel == PanelState.RightPanelState.PropertyPanel)
  57. {
  58. ExpandRightPropertyPanel(PDFAnnotationControl, Visibility.Visible);
  59. }
  60. else if (panelState.RightPanel == PanelState.RightPanelState.ViewSettings)
  61. {
  62. ExpandRightPropertyPanel(displaySettingsControl, Visibility.Visible);
  63. }
  64. else
  65. {
  66. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  67. }
  68. }
  69. }
  70. public void ExpandLeftPanel(bool isExpand)
  71. {
  72. BotaContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  73. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  74. if (isExpand)
  75. {
  76. BodyGrid.ColumnDefinitions[0].Width = new GridLength(320);
  77. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  78. }
  79. else
  80. {
  81. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  82. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  83. }
  84. }
  85. public void ExpandRightPropertyPanel(UIElement propertytPanel, Visibility visible)
  86. {
  87. PropertyContainer.Width = 260;
  88. PropertyContainer.Child = propertytPanel;
  89. PropertyContainer.Visibility = visible;
  90. }
  91. #region Init PDFViewer
  92. private void InitialControl()
  93. {
  94. PdfViewControl.SetToolType(ComPDFKit.Tool.CPDFToolManager.ToolType.Pan);
  95. //PdfViewControl.PDFView?.SetShowLink(true);
  96. PDFGrid.Child = PdfViewControl;
  97. PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  98. PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  99. PdfViewControl.MouseRightButtonDownHandler -= PdfViewControl_MouseRightButtonDownHandler;
  100. PdfViewControl.MouseRightButtonDownHandler += PdfViewControl_MouseRightButtonDownHandler;
  101. //PdfViewControl.PDFView.SetFormFieldHighlight(true);
  102. }
  103. private void PdfViewControl_MouseRightButtonDownHandler(object sender, ComPDFKit.Tool.MouseEventObject e)
  104. {
  105. ContextMenu ContextMenu = PdfViewControl.GetRightMenu();
  106. if (ContextMenu == null)
  107. {
  108. ContextMenu = new ContextMenu();
  109. }
  110. switch (e.hitTestType)
  111. {
  112. case MouseHitTestType.kAnnot:
  113. case MouseHitTestType.kSelectRect:
  114. break;
  115. case MouseHitTestType.kText:
  116. CreateSelectTextContextMenu(sender, ref ContextMenu);
  117. break;
  118. default:
  119. ContextMenu = null;
  120. break;
  121. }
  122. PdfViewControl.SetRightMenu(ContextMenu);
  123. }
  124. private void CreateSelectTextContextMenu(object sender, ref ContextMenu menu)
  125. {
  126. menu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  127. }
  128. public void InitWithPDFViewer(PDFViewControl pdfViewer)
  129. {
  130. PdfViewControl = pdfViewer;
  131. PDFGrid.Child = PdfViewControl;
  132. FloatPageTool.InitWithPDFViewer(pdfViewer);
  133. InitialControl();
  134. DataContext = this;
  135. if (PdfViewControl != null && PdfViewControl.PDFViewTool.GetCPDFViewer() != null)
  136. {
  137. //PdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  138. //PdfViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
  139. //PdfViewControl.PDFView.WidgetClickHandler -= PDFView_WidgetClickHandler;
  140. //PdfViewControl.PDFView.WidgetClickHandler += PDFView_WidgetClickHandler;
  141. }
  142. }
  143. //private void PDFView_WidgetClickHandler(object sender, WidgetArgs e)
  144. //{
  145. // if ((e is WidgetSignArgs args))
  146. // {
  147. // var signatureWidget = args.Sign;
  148. // if(signatureWidget != null)
  149. // {
  150. // CPDFSignature sig = signatureWidget.GetSignature(PdfViewControl.PDFView.Document);
  151. // if (signatureWidget.IsSigned() && sig!=null && sig?.SignerList.Count > 0)
  152. // {
  153. // return;
  154. // }
  155. // }
  156. // if (args.WidgetType == C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS)
  157. // {
  158. // panelState.RightPanel = PanelState.RightPanelState.PropertyPanel;
  159. // CPDFSignatureUI signatureProperty = new CPDFSignatureUI();
  160. // signatureProperty.SetFormProperty(args, PdfViewControl.PDFView);
  161. // PropertyContainer.Child = signatureProperty;
  162. // }
  163. // }
  164. //}
  165. public void CancelWidgetClickHandler()
  166. {
  167. //if (PdfViewControl != null && PdfViewControl.PDFView != null)
  168. //{
  169. // PdfViewControl.PDFView.WidgetClickHandler -= PDFView_WidgetClickHandler;
  170. //}
  171. }
  172. public void SetBOTAContainer(CPDFBOTABarControl botaControl)
  173. {
  174. this.BotaContainer.Child = botaControl;
  175. }
  176. public void SetDisplaySettingsControl(CPDFDisplaySettingsControl displaySettingsControl)
  177. {
  178. this.displaySettingsControl = displaySettingsControl;
  179. }
  180. public void SetSignatureStatusBarControl(SignatureStatusBarControl signatureStatusBarControl)
  181. {
  182. this.signatureStatusBarControl = signatureStatusBarControl;
  183. SignatureStatusBorder.Child = this.signatureStatusBarControl;
  184. if (signatureStatusBarControl.Status != SignatureStatus.None)
  185. {
  186. SignatureStatusBorder.Visibility = Visibility.Visible;
  187. }
  188. else
  189. {
  190. SignatureStatusBorder.Visibility = Visibility.Collapsed;
  191. }
  192. }
  193. #endregion
  194. public void ClearViewerControl()
  195. {
  196. PDFGrid.Child = null;
  197. BotaContainer.Child = null;
  198. PropertyContainer.Child = null;
  199. SignatureStatusBorder.Child = null;
  200. displaySettingsControl = null;
  201. }
  202. #region PropertyChanged
  203. /// <summary>
  204. /// Undo Redo Event Noitfy
  205. /// </summary>
  206. private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  207. {
  208. OnPropertyChanged(e.PropertyName);
  209. if (e.PropertyName == "CanSave")
  210. {
  211. OnCanSaveChanged?.Invoke(this, CanSave);
  212. }
  213. }
  214. protected void OnPropertyChanged([CallerMemberName] string name = null)
  215. {
  216. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  217. }
  218. #endregion
  219. #region Context Menu
  220. private void ExtraImage_Click(object sender, RoutedEventArgs e)
  221. {
  222. //CommandHelper.ExtraImage_Click(PdfViewControl.PDFView.GetSelectedImages());
  223. }
  224. private void CopyImage_Click(object sender, RoutedEventArgs e)
  225. {
  226. //CommandHelper.CopyImage_Click(PdfViewControl.PDFView.GetSelectedImages());
  227. }
  228. //private void PDFView_AnnotCommandHandler(object sender, AnnotCommandArgs e)
  229. //{
  230. // if (e != null && e.CommandType == CommandType.Context)
  231. // {
  232. // if (e.PressOnSelectedText)
  233. // {
  234. // e.Handle = true;
  235. // e.PopupMenu = new ContextMenu();
  236. // e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  237. // }
  238. // else if (e.CommandTarget == TargetType.ImageSelection)
  239. // {
  240. // if (PdfViewControl != null && PdfViewControl.PDFView != null && PdfViewControl.PDFView.GetSelectImageCount() > 0)
  241. // {
  242. // e.Handle = true;
  243. // e.PopupMenu = new ContextMenu();
  244. // MenuItem imageCopyMenu = new MenuItem();
  245. // imageCopyMenu = new MenuItem();
  246. // imageCopyMenu.Header = "Copy Images";
  247. // WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageCopyMenu, "Click", CopyImage_Click);
  248. // imageCopyMenu.CommandParameter = e;
  249. // e.PopupMenu.Items.Add(imageCopyMenu);
  250. // MenuItem imageExtraMenu = new MenuItem();
  251. // imageExtraMenu = new MenuItem();
  252. // imageExtraMenu.Header = "Extract Images";
  253. // WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageExtraMenu, "Click", ExtraImage_Click);
  254. // imageExtraMenu.CommandParameter = e;
  255. // e.PopupMenu.Items.Add(imageExtraMenu);
  256. // }
  257. // }
  258. // else
  259. // {
  260. // e.Handle = true;
  261. // e.PopupMenu = new ContextMenu();
  262. // //if (PdfViewControl.CheckHasForm())
  263. // MenuItem fitWidthMenu = new MenuItem();
  264. // fitWidthMenu.Header = "Automatically Resize";
  265. // fitWidthMenu.Click += (o, p) =>
  266. // {
  267. // if (PdfViewControl != null)
  268. // {
  269. // PdfViewControl.PDFView?.ChangeFitMode(FitMode.FitWidth);
  270. // }
  271. // };
  272. // e.PopupMenu.Items.Add(fitWidthMenu);
  273. // MenuItem fitSizeMenu = new MenuItem();
  274. // fitSizeMenu.Header = "Actual Size";
  275. // fitSizeMenu.Click += (o, p) =>
  276. // {
  277. // if (PdfViewControl != null)
  278. // {
  279. // PdfViewControl.PDFView?.ChangeFitMode(FitMode.FitSize);
  280. // }
  281. // };
  282. // e.PopupMenu.Items.Add(fitSizeMenu);
  283. // MenuItem zoomInMenu = new MenuItem();
  284. // zoomInMenu.Header = "Zoom In";
  285. // zoomInMenu.Click += (o, p) =>
  286. // {
  287. // if (PdfViewControl != null)
  288. // {
  289. // double newZoom = CommandHelper.CheckZoomLevel(zoomLevelList,PdfViewControl.PDFView.ZoomFactor + 0.01, true);
  290. // PdfViewControl.PDFView?.Zoom(newZoom);
  291. // }
  292. // };
  293. // e.PopupMenu.Items.Add(zoomInMenu);
  294. // MenuItem zoomOutMenu = new MenuItem();
  295. // zoomOutMenu.Header = "Zoom Out";
  296. // zoomOutMenu.Click += (o, p) =>
  297. // {
  298. // if (PdfViewControl != null)
  299. // {
  300. // double newZoom = CommandHelper.CheckZoomLevel(zoomLevelList,PdfViewControl.PDFView.ZoomFactor - 0.01, false);
  301. // PdfViewControl.PDFView?.Zoom(newZoom);
  302. // }
  303. // };
  304. // e.PopupMenu.Items.Add(zoomOutMenu);
  305. // e.PopupMenu.Items.Add(new Separator());
  306. // MenuItem singleView = new MenuItem();
  307. // singleView.Header = "Single Page";
  308. // singleView.Click += (o, p) =>
  309. // {
  310. // if (PdfViewControl != null)
  311. // {
  312. // PdfViewControl.PDFView?.ChangeViewMode(ViewMode.Single);
  313. // }
  314. // };
  315. // e.PopupMenu.Items.Add(singleView);
  316. // MenuItem singleContinuousView = new MenuItem();
  317. // singleContinuousView.Header = "Single Page Continuous";
  318. // singleContinuousView.Click += (o, p) =>
  319. // {
  320. // if (PdfViewControl != null)
  321. // {
  322. // PdfViewControl.PDFView?.ChangeViewMode(ViewMode.SingleContinuous);
  323. // }
  324. // };
  325. // e.PopupMenu.Items.Add(singleContinuousView);
  326. // MenuItem doubleView = new MenuItem();
  327. // doubleView.Header = "Two Pages";
  328. // doubleView.Click += (o, p) =>
  329. // {
  330. // if (PdfViewControl != null)
  331. // {
  332. // PdfViewControl.PDFView?.ChangeViewMode(ViewMode.Double);
  333. // }
  334. // };
  335. // e.PopupMenu.Items.Add(doubleView);
  336. // MenuItem doubleContinuousView = new MenuItem();
  337. // doubleContinuousView.Header = "Two Pages Continuous";
  338. // doubleContinuousView.Click += (o, p) =>
  339. // {
  340. // if (PdfViewControl != null)
  341. // {
  342. // PdfViewControl.PDFView?.ChangeViewMode(ViewMode.DoubleContinuous);
  343. // }
  344. // };
  345. // e.PopupMenu.Items.Add(doubleContinuousView);
  346. // {
  347. // MenuItem resetForms = new MenuItem();
  348. // resetForms.Header = "Reset Forms";
  349. // resetForms.Click += (o, p) =>
  350. // {
  351. // if (PdfViewControl != null)
  352. // {
  353. // PdfViewControl.PDFView?.ResetForm(null);
  354. // }
  355. // };
  356. // e.PopupMenu.Items.Add(new Separator());
  357. // e.PopupMenu.Items.Add(resetForms);
  358. // }
  359. // }
  360. // }
  361. // if (e != null && e.CommandType == CommandType.Copy)
  362. // {
  363. // e.DoCommand();
  364. // }
  365. //}
  366. #endregion
  367. private void UserControl_Unloaded(object sender, RoutedEventArgs e)
  368. {
  369. //PdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  370. //PdfViewControl.PDFView.WidgetClickHandler -= PDFView_WidgetClickHandler;
  371. }
  372. }
  373. }