AnnotationControl.xaml.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. using System.Windows.Controls;
  2. using Compdfkit_Tools.Data;
  3. using ComPDFKitViewer;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Diagnostics;
  8. using System.Drawing;
  9. using System.IO;
  10. using System.Runtime.CompilerServices;
  11. using System.Windows;
  12. using System.Windows.Controls.Primitives;
  13. using System.Windows.Input;
  14. using System.Windows.Media.Imaging;
  15. using Compdfkit_Tools.Annotation.PDFAnnotationPanel.PDFAnnotationUI;
  16. using Compdfkit_Tools.Helper;
  17. using ComPDFKit.DigitalSign;
  18. using ComPDFKit.PDFAnnotation.Form;
  19. using ComPDFKit.PDFAnnotation;
  20. using ComPDFKit.Tool;
  21. namespace Compdfkit_Tools.PDFControl
  22. {
  23. public partial class AnnotationControl : UserControl, INotifyPropertyChanged
  24. {
  25. #region Property
  26. private bool isFirstLoad = true;
  27. public PDFViewControl PDFViewControl;
  28. public CPDFAnnotationControl PDFAnnotationControl = null;
  29. private CPDFDisplaySettingsControl displaySettingsControl = null;
  30. private PanelState panelState = PanelState.GetInstance();
  31. private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  32. public event PropertyChangedEventHandler PropertyChanged;
  33. public ICommand CloseTabCommand;
  34. public ICommand ExpandPropertyPanelCommand;
  35. public bool CanUndo
  36. {
  37. get
  38. {
  39. if (PDFViewControl != null && PDFViewControl.PDFViewTool.GetCPDFViewer() != null)
  40. {
  41. return PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager.CanUndo;
  42. }
  43. return false;
  44. }
  45. }
  46. public bool CanRedo
  47. {
  48. get
  49. {
  50. if (PDFViewControl != null && PDFViewControl.PDFViewTool.GetCPDFViewer() != null)
  51. {
  52. return PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager.CanRedo;
  53. }
  54. return false;
  55. }
  56. }
  57. private bool CanSave
  58. {
  59. get
  60. {
  61. if (PDFViewControl != null && PDFViewControl.PDFViewTool.GetCPDFViewer() != null)
  62. {
  63. if (PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager.CanRedo ||
  64. PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager.CanUndo)
  65. {
  66. return true;
  67. }
  68. }
  69. return false;
  70. }
  71. }
  72. public event EventHandler<bool> OnCanSaveChanged;
  73. public event EventHandler OnAnnotEditHandler;
  74. #endregion
  75. public AnnotationControl()
  76. {
  77. InitializeComponent();
  78. DataContext = this;
  79. PDFAnnotationControl = new CPDFAnnotationControl();
  80. CPDFAnnotationType[] annotationProperties =
  81. {
  82. CPDFAnnotationType.Highlight, CPDFAnnotationType.Underline, CPDFAnnotationType.Strikeout,
  83. CPDFAnnotationType.Squiggly, CPDFAnnotationType.Freehand, CPDFAnnotationType.FreeText,
  84. CPDFAnnotationType.Note, CPDFAnnotationType.Circle, CPDFAnnotationType.Square,
  85. CPDFAnnotationType.Arrow, CPDFAnnotationType.Line, CPDFAnnotationType.Image,
  86. CPDFAnnotationType.Stamp, CPDFAnnotationType.Signature, CPDFAnnotationType.Link,
  87. CPDFAnnotationType.Audio
  88. };
  89. AnnotationBarControl.InitAnnotationBar(annotationProperties);
  90. panelState.PropertyChanged -= PanelState_PropertyChanged;
  91. panelState.PropertyChanged += PanelState_PropertyChanged;
  92. }
  93. #region Init PDFViewer
  94. public void InitWithPDFViewer(PDFViewControl pdfViewer)
  95. {
  96. PDFViewControl = pdfViewer;
  97. PDFGrid.Child = PDFViewControl;
  98. FloatPageTool.InitWithPDFViewer(PDFViewControl);
  99. InitialPDFViewControl(PDFViewControl);
  100. }
  101. #endregion
  102. #region Public Method
  103. public void ClearViewerControl()
  104. {
  105. PDFGrid.Child = null;
  106. BotaContainer.Child = null;
  107. PropertyContainer.Child = null;
  108. displaySettingsControl = null;
  109. PDFViewControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_NONE);
  110. }
  111. public void SetBOTAContainer(CPDFBOTABarControl botaControl)
  112. {
  113. this.BotaContainer.Child = botaControl;
  114. }
  115. public void SetDisplaySettingsControl(CPDFDisplaySettingsControl displaySettingsControl)
  116. {
  117. this.displaySettingsControl = displaySettingsControl;
  118. }
  119. public void ClearAllToolState()
  120. {
  121. this.AnnotationBarControl.ClearAllToolState();
  122. }
  123. public void SetToolBarContainerVisibility(Visibility visibility)
  124. {
  125. this.ToolBarContainer.Visibility = visibility;
  126. }
  127. #endregion
  128. #region Load Unload custom control
  129. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  130. {
  131. InitialPDFViewControl(PDFViewControl);
  132. }
  133. private void UserControl_Unloaded(object sender, RoutedEventArgs e)
  134. {
  135. //PDFViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  136. //PDFViewControl.PDFView.WidgetClickHandler -= PDFView_WidgetClickHandler;
  137. }
  138. private void AnnotationBarControl_Loaded(object sender, RoutedEventArgs e)
  139. {
  140. AnnotationBarControl.AnnotationPropertyChanged -= AnnotationBarControl_AnnotationPropertyChanged;
  141. AnnotationBarControl.AnnotationCancel -= AnnotationBarControl_AnnotationCancel;
  142. AnnotationBarControl.AnnotationPropertyChanged += AnnotationBarControl_AnnotationPropertyChanged;
  143. AnnotationBarControl.AnnotationCancel += AnnotationBarControl_AnnotationCancel;
  144. }
  145. private void AnnotationBarControl_Unloaded(object sender, RoutedEventArgs e)
  146. {
  147. AnnotationBarControl.AnnotationPropertyChanged -= AnnotationBarControl_AnnotationPropertyChanged;
  148. AnnotationBarControl.AnnotationCancel -= AnnotationBarControl_AnnotationCancel;
  149. }
  150. #endregion
  151. #region Annotation
  152. public void InitialPDFViewControl(PDFViewControl newPDFViewer)
  153. {
  154. PDFAnnotationControl.SetPDFViewer(newPDFViewer);
  155. PDFAnnotationControl.AnnotationCancel();
  156. AnnotationBarControl.ClearAllToolState();
  157. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  158. PDFAnnotationControl.ClearAnnotationBar -= PdfAnnotationControl_ClearAnnotationBar;
  159. PDFAnnotationControl.ClearAnnotationBar += PdfAnnotationControl_ClearAnnotationBar;
  160. //PDFViewControl.PDFView.AnnotEditHandler -= PDFView_AnnotEditHandler;
  161. //PDFViewControl.PDFView.AnnotEditHandler += PDFView_AnnotEditHandler;
  162. PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  163. PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  164. //PDFViewControl.PDFView.AnnotActiveHandler -= PDFView_AnnotActiveHandler;
  165. //PDFViewControl.PDFView.AnnotActiveHandler += PDFView_AnnotActiveHandler;
  166. PDFViewControl.MouseRightButtonDownHandler -= PDFViewControl_MouseRightButtonDownHandler;
  167. PDFViewControl.MouseRightButtonDownHandler += PDFViewControl_MouseRightButtonDownHandler;
  168. //PDFViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
  169. //PDFViewControl.PDFView.WidgetClickHandler += PDFView_WidgetClickHandler;
  170. }
  171. private void PDFViewControl_MouseRightButtonDownHandler(object sender, ComPDFKit.Tool.MouseEventObject e)
  172. {
  173. ContextMenu ContextMenu = PDFViewControl.GetRightMenu();
  174. if (ContextMenu == null)
  175. {
  176. ContextMenu = new ContextMenu();
  177. }
  178. switch (e.hitTestType)
  179. {
  180. case MouseHitTestType.kAnnot:
  181. case MouseHitTestType.kSelectRect:
  182. CreateAnnotContextMenu(sender, ref ContextMenu, e.annotType);
  183. break;
  184. case MouseHitTestType.kText:
  185. CreateSelectTextContextMenu(sender, ref ContextMenu);
  186. break;
  187. default:
  188. ContextMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  189. break;
  190. }
  191. PDFViewControl.SetRightMenu(ContextMenu);
  192. }
  193. private void CreateSelectTextContextMenu(object sender, ref ContextMenu menu)
  194. {
  195. menu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  196. }
  197. private void CreateAnnotContextMenu(object sender, ref ContextMenu menu, C_ANNOTATION_TYPE annotType)
  198. {
  199. switch (annotType)
  200. {
  201. case C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET:
  202. break;
  203. default:
  204. menu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  205. menu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  206. menu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  207. menu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  208. break;
  209. }
  210. }
  211. public void UnloadEvent()
  212. {
  213. //PDFViewControl.PDFView.AnnotEditHandler -= PDFView_AnnotEditHandler;
  214. //PDFViewControl.PDFView.AnnotActiveHandler -= PDFView_AnnotActiveHandler;
  215. //panelState.PropertyChanged -= PanelState_PropertyChanged;
  216. }
  217. private void PdfAnnotationControl_ClearAnnotationBar(object sender, EventArgs e)
  218. {
  219. AnnotationBarControl.ClearAllToolState();
  220. }
  221. public void SetViewSettings(Visibility visibility, CPDFDisplaySettingsControl displaySettingsControl = null)
  222. {
  223. this.PropertyContainer.Child = displaySettingsControl;
  224. this.PropertyContainer.Visibility = visibility;
  225. }
  226. #endregion
  227. #region Expand and collapse Panel
  228. public void ExpandRightPropertyPanel(UIElement propertytPanel, Visibility visible)
  229. {
  230. PropertyContainer.Width = 260;
  231. PropertyContainer.Child = propertytPanel;
  232. PropertyContainer.Visibility = visible;
  233. }
  234. public void ExpandLeftPanel(bool isExpand)
  235. {
  236. BotaContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  237. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  238. if (isExpand)
  239. {
  240. BodyGrid.ColumnDefinitions[0].Width = new GridLength(320);
  241. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  242. }
  243. else
  244. {
  245. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  246. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  247. }
  248. }
  249. #endregion
  250. //#region Context menu
  251. //private void PDFView_AnnotCommandHandler(object sender, AnnotCommandArgs e)
  252. //{
  253. // switch (e.CommandType)
  254. // {
  255. // case CommandType.Context:
  256. // e.Handle = true;
  257. // if (e.CommandTarget == TargetType.Annot)
  258. // {
  259. // e.Handle = true;
  260. // e.PopupMenu = new ContextMenu();
  261. // if (e.PressOnLink && AnnotationBarControl.CurrentMode == "Link")
  262. // {
  263. // e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  264. // MenuItem propertyMenu = new MenuItem();
  265. // propertyMenu = new MenuItem();
  266. // propertyMenu.Header = "Edit";
  267. // WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(propertyMenu, "Click", EditLink_Click);
  268. // propertyMenu.CommandParameter = e;
  269. // e.PopupMenu.Items.Add(propertyMenu);
  270. // }
  271. // else if (e.PressOnAnnot)
  272. // {
  273. // e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  274. // e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  275. // e.PopupMenu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  276. // }
  277. // else if (e.PressOnMedia || e.PressOnSound)
  278. // {
  279. // e.Handle = true;
  280. // e.PopupMenu.Items.Add(new MenuItem() { Header = "Play", Command = MediaCommands.Play, CommandTarget = (UIElement)sender, CommandParameter = e });
  281. // e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  282. // }
  283. // else if (e.PressOnSelectedText)
  284. // {
  285. // e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  286. // MenuItem highLightMenu = new MenuItem();
  287. // highLightMenu.Header = "HighLight";
  288. // highLightMenu.Click += (o, p) =>
  289. // {
  290. // TextHighlightAnnotArgs highLightArgs = new TextHighlightAnnotArgs();
  291. // MouseModes oldMode = PDFViewControl.PDFView.MouseMode;
  292. // if (PDFAnnotationControl != null)
  293. // {
  294. // highLightArgs.Color = System.Windows.Media.Colors.Red;
  295. // highLightArgs.Transparency = 1;
  296. // PDFViewControl.PDFView.SetMouseMode(MouseModes.AnnotCreate);
  297. // PDFViewControl.PDFView.SetToolParam(highLightArgs);
  298. // PDFViewControl.PDFView.SetMouseMode(oldMode);
  299. // }
  300. // };
  301. // e.PopupMenu.Items.Add(highLightMenu);
  302. // MenuItem underlineMenu = new MenuItem();
  303. // underlineMenu.Header = "UnderLine";
  304. // underlineMenu.Click += (o, p) =>
  305. // {
  306. // TextUnderlineAnnotArgs underlineArgs = new TextUnderlineAnnotArgs();
  307. // MouseModes oldMode = PDFViewControl.PDFView.MouseMode;
  308. // if (PDFAnnotationControl != null)
  309. // {
  310. // underlineArgs.Color = System.Windows.Media.Colors.Red;
  311. // underlineArgs.Transparency = 1;
  312. // PDFViewControl.PDFView.SetMouseMode(MouseModes.AnnotCreate);
  313. // PDFViewControl.PDFView.SetToolParam(underlineArgs);
  314. // PDFViewControl.PDFView.SetMouseMode(oldMode);
  315. // }
  316. // };
  317. // e.PopupMenu.Items.Add(underlineMenu);
  318. // MenuItem strikeOutMenu = new MenuItem();
  319. // strikeOutMenu.Header = "StrikeOut";
  320. // strikeOutMenu.Click += (o, p) =>
  321. // {
  322. // TextStrikeoutAnnotArgs strikeoutAnnotArgs = new TextStrikeoutAnnotArgs();
  323. // MouseModes oldMode = PDFViewControl.PDFView.MouseMode;
  324. // if (PDFAnnotationControl != null)
  325. // {
  326. // strikeoutAnnotArgs.Color = System.Windows.Media.Colors.Red;
  327. // strikeoutAnnotArgs.Transparency = 1;
  328. // PDFViewControl.PDFView.SetMouseMode(MouseModes.AnnotCreate);
  329. // PDFViewControl.PDFView.SetToolParam(strikeoutAnnotArgs);
  330. // PDFViewControl.PDFView.SetMouseMode(oldMode);
  331. // }
  332. // };
  333. // e.PopupMenu.Items.Add(strikeOutMenu);
  334. // MenuItem SquiggleMenu = new MenuItem();
  335. // SquiggleMenu.Header = "Squiggle";
  336. // SquiggleMenu.Click += (o, p) =>
  337. // {
  338. // TextSquigglyAnnotArgs squigglyAnnotArgs = new TextSquigglyAnnotArgs();
  339. // MouseModes oldMode = PDFViewControl.PDFView.MouseMode;
  340. // if (PDFAnnotationControl != null)
  341. // {
  342. // squigglyAnnotArgs.Color = System.Windows.Media.Colors.Red;
  343. // squigglyAnnotArgs.Transparency = 1;
  344. // PDFViewControl.PDFView.SetMouseMode(MouseModes.AnnotCreate);
  345. // PDFViewControl.PDFView.SetToolParam(squigglyAnnotArgs);
  346. // PDFViewControl.PDFView.SetMouseMode(oldMode);
  347. // }
  348. // };
  349. // e.PopupMenu.Items.Add(SquiggleMenu);
  350. // }
  351. // else
  352. // {
  353. // e.Handle = true;
  354. // e.PopupMenu = new ContextMenu();
  355. // e.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  356. // e.PopupMenu.Items.Add(new Separator());
  357. // MenuItem fitWidthMenu = new MenuItem();
  358. // fitWidthMenu.Header = "Automatically Resize";
  359. // fitWidthMenu.Click += (o, p) =>
  360. // {
  361. // if (PDFViewControl != null)
  362. // {
  363. // PDFViewControl.PDFView?.ChangeFitMode(FitMode.FitWidth);
  364. // }
  365. // };
  366. // e.PopupMenu.Items.Add(fitWidthMenu);
  367. // MenuItem fitSizeMenu = new MenuItem();
  368. // fitSizeMenu.Header = "Actual Size";
  369. // fitSizeMenu.Click += (o, p) =>
  370. // {
  371. // if (PDFViewControl != null)
  372. // {
  373. // PDFViewControl.PDFView?.ChangeFitMode(FitMode.FitSize);
  374. // }
  375. // };
  376. // e.PopupMenu.Items.Add(fitSizeMenu);
  377. // MenuItem zoomInMenu = new MenuItem();
  378. // zoomInMenu.Header = "Zoom In";
  379. // zoomInMenu.Click += (o, p) =>
  380. // {
  381. // if (PDFViewControl != null)
  382. // {
  383. // double newZoom = CheckZoomLevel(PDFViewControl.PDFView.ZoomFactor + 0.01, true);
  384. // PDFViewControl.PDFView?.Zoom(newZoom);
  385. // }
  386. // };
  387. // e.PopupMenu.Items.Add(zoomInMenu);
  388. // MenuItem zoomOutMenu = new MenuItem();
  389. // zoomOutMenu.Header = "Zoom Out";
  390. // zoomOutMenu.Click += (o, p) =>
  391. // {
  392. // if (PDFViewControl != null)
  393. // {
  394. // double newZoom = CheckZoomLevel(PDFViewControl.PDFView.ZoomFactor - 0.01, false);
  395. // PDFViewControl.PDFView?.Zoom(newZoom);
  396. // }
  397. // };
  398. // e.PopupMenu.Items.Add(zoomOutMenu);
  399. // e.PopupMenu.Items.Add(new Separator());
  400. // MenuItem singleView = new MenuItem();
  401. // singleView.Header = "Single Page";
  402. // singleView.Click += (o, p) =>
  403. // {
  404. // if (PDFViewControl != null)
  405. // {
  406. // PDFViewControl.PDFView?.ChangeViewMode(ViewMode.Single);
  407. // }
  408. // };
  409. // e.PopupMenu.Items.Add(singleView);
  410. // MenuItem singleContinuousView = new MenuItem();
  411. // singleContinuousView.Header = "Single Page Continuous";
  412. // singleContinuousView.Click += (o, p) =>
  413. // {
  414. // if (PDFViewControl != null)
  415. // {
  416. // PDFViewControl.PDFView?.ChangeViewMode(ViewMode.SingleContinuous);
  417. // }
  418. // };
  419. // e.PopupMenu.Items.Add(singleContinuousView);
  420. // MenuItem doubleView = new MenuItem();
  421. // doubleView.Header = "Two Pages";
  422. // doubleView.Click += (o, p) =>
  423. // {
  424. // if (PDFViewControl != null)
  425. // {
  426. // PDFViewControl.PDFView?.ChangeViewMode(ViewMode.Double);
  427. // }
  428. // };
  429. // e.PopupMenu.Items.Add(doubleView);
  430. // MenuItem doubleContinuousView = new MenuItem();
  431. // doubleContinuousView.Header = "Two Pages Continuous";
  432. // doubleContinuousView.Click += (o, p) =>
  433. // {
  434. // if (PDFViewControl != null)
  435. // {
  436. // PDFViewControl.PDFView?.ChangeViewMode(ViewMode.DoubleContinuous);
  437. // }
  438. // };
  439. // e.PopupMenu.Items.Add(doubleContinuousView);
  440. // MenuItem resetFormMenu = new MenuItem();
  441. // resetFormMenu.Header = "Reset Forms";
  442. // resetFormMenu.Click += (o, p) =>
  443. // {
  444. // if (PDFViewControl != null)
  445. // {
  446. // PDFViewControl.PDFView?.ResetForm(null);
  447. // }
  448. // };
  449. // e.PopupMenu.Items.Add(new Separator());
  450. // e.PopupMenu.Items.Add(resetFormMenu);
  451. // }
  452. // }
  453. // else if (e.CommandTarget == TargetType.ImageSelection)
  454. // {
  455. // if (PDFViewControl != null && PDFViewControl.PDFView != null && PDFViewControl.PDFView.GetSelectImageCount() > 0)
  456. // {
  457. // e.Handle = true;
  458. // e.PopupMenu = new ContextMenu();
  459. // MenuItem imageCopyMenu = new MenuItem();
  460. // imageCopyMenu = new MenuItem();
  461. // imageCopyMenu.Header = "Copy Images";
  462. // WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageCopyMenu, "Click", CopyImage_Click);
  463. // imageCopyMenu.CommandParameter = e;
  464. // e.PopupMenu.Items.Add(imageCopyMenu);
  465. // MenuItem imageExtraMenu = new MenuItem();
  466. // imageExtraMenu = new MenuItem();
  467. // imageExtraMenu.Header = "Extract Images";
  468. // WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageExtraMenu, "Click", ExtraImage_Click);
  469. // imageExtraMenu.CommandParameter = e;
  470. // e.PopupMenu.Items.Add(imageExtraMenu);
  471. // }
  472. // }
  473. // break;
  474. // case CommandType.Copy:
  475. // e.DoCommand();
  476. // break;
  477. // case CommandType.Cut:
  478. // case CommandType.Paste:
  479. // case CommandType.Delete:
  480. // e.DoCommand();
  481. // break;
  482. // default:
  483. // break;
  484. // }
  485. //}
  486. //private void CopyImage_Click(object sender, RoutedEventArgs e)
  487. //{
  488. // try
  489. // {
  490. // Dictionary<int, List<Bitmap>> imageDict = PDFViewControl.PDFView?.GetSelectedImages();
  491. // if (imageDict != null && imageDict.Count > 0)
  492. // {
  493. // foreach (int pageIndex in imageDict.Keys)
  494. // {
  495. // List<Bitmap> imageList = imageDict[pageIndex];
  496. // foreach (Bitmap image in imageList)
  497. // {
  498. // MemoryStream ms = new MemoryStream();
  499. // image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  500. // BitmapImage imageData = new BitmapImage();
  501. // imageData.BeginInit();
  502. // imageData.StreamSource = ms;
  503. // imageData.CacheOption = BitmapCacheOption.OnLoad;
  504. // imageData.EndInit();
  505. // imageData.Freeze();
  506. // Clipboard.SetImage(imageData);
  507. // break;
  508. // }
  509. // }
  510. // }
  511. // }
  512. // catch (Exception ex)
  513. // {
  514. // }
  515. //}
  516. //private void ExtraImage_Click(object sender, RoutedEventArgs e)
  517. //{
  518. // System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();
  519. // if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  520. // {
  521. // string choosePath = folderDialog.SelectedPath;
  522. // string openPath = choosePath;
  523. // try
  524. // {
  525. // Dictionary<int, List<Bitmap>> imageDict = PDFViewControl.PDFView?.GetSelectedImages();
  526. // if (imageDict != null && imageDict.Count > 0)
  527. // {
  528. // foreach (int pageIndex in imageDict.Keys)
  529. // {
  530. // List<Bitmap> imageList = imageDict[pageIndex];
  531. // foreach (Bitmap image in imageList)
  532. // {
  533. // string savePath = Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
  534. // image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  535. // openPath = savePath;
  536. // }
  537. // }
  538. // }
  539. // Process.Start("explorer", "/select,\"" + openPath + "\"");
  540. // }
  541. // catch (Exception ex)
  542. // {
  543. // }
  544. // }
  545. //}
  546. //#endregion
  547. #region UI
  548. private double CheckZoomLevel(double zoom, bool IsGrowth)
  549. {
  550. double standardZoom = 100;
  551. if (zoom <= 0.01)
  552. {
  553. return 0.01;
  554. }
  555. if (zoom >= 10)
  556. {
  557. return 10;
  558. }
  559. zoom *= 100;
  560. for (int i = 0; i < zoomLevelList.Length - 1; i++)
  561. {
  562. if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth)
  563. {
  564. standardZoom = zoomLevelList[i + 1];
  565. break;
  566. }
  567. if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth)
  568. {
  569. standardZoom = zoomLevelList[i];
  570. break;
  571. }
  572. }
  573. return standardZoom / 100;
  574. }
  575. private void ToolExpand_Click(object sender, RoutedEventArgs e)
  576. {
  577. ToggleButton expandBtn = sender as ToggleButton;
  578. if (expandBtn != null)
  579. {
  580. bool isExpand = expandBtn.IsChecked == true;
  581. ExpandLeftPanel(isExpand);
  582. }
  583. }
  584. private void EditLink_Click(object sender, RoutedEventArgs e)
  585. {
  586. PropertyContainer.Visibility = Visibility.Visible;
  587. }
  588. private void UndoButton_Click(object sender, RoutedEventArgs e)
  589. {
  590. if (PDFViewControl != null && PDFViewControl.PDFViewTool.GetCPDFViewer() != null)
  591. {
  592. PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager?.Undo();
  593. PDFViewControl.PDFToolManager.ClearSelect();
  594. PDFViewControl.PDFViewTool.GetCPDFViewer().UpDateAnnotFrame();
  595. }
  596. }
  597. private void RedoButton_Click(object sender, RoutedEventArgs e)
  598. {
  599. if (PDFViewControl != null && PDFViewControl.PDFViewTool.GetCPDFViewer() != null)
  600. {
  601. PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager?.Redo();
  602. PDFViewControl.PDFToolManager.ClearSelect();
  603. PDFViewControl.PDFViewTool.GetCPDFViewer().UpDateAnnotFrame();
  604. }
  605. }
  606. #endregion
  607. #region Property changed
  608. protected void OnPropertyChanged([CallerMemberName] string name = null)
  609. {
  610. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  611. }
  612. private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  613. {
  614. OnPropertyChanged(e.PropertyName);
  615. if (e.PropertyName == "CanSave")
  616. {
  617. OnCanSaveChanged?.Invoke(this, CanSave);
  618. }
  619. }
  620. private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
  621. {
  622. if (e.PropertyName == nameof(PanelState.IsLeftPanelExpand))
  623. {
  624. ExpandLeftPanel(panelState.IsLeftPanelExpand);
  625. }
  626. else if (e.PropertyName == nameof(PanelState.RightPanel))
  627. {
  628. if (panelState.RightPanel == PanelState.RightPanelState.PropertyPanel)
  629. {
  630. ExpandRightPropertyPanel(PDFAnnotationControl, Visibility.Visible);
  631. }
  632. else if (panelState.RightPanel == PanelState.RightPanelState.ViewSettings)
  633. {
  634. ExpandRightPropertyPanel(displaySettingsControl, Visibility.Visible);
  635. }
  636. else
  637. {
  638. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  639. }
  640. }
  641. }
  642. #endregion
  643. #region Event handle
  644. private void AnnotationBarControl_AnnotationCancel(object sender, EventArgs e)
  645. {
  646. PDFAnnotationControl.AnnotationCancel();
  647. if (panelState.RightPanel == PanelState.RightPanelState.PropertyPanel)
  648. {
  649. panelState.RightPanel = PanelState.RightPanelState.None;
  650. }
  651. }
  652. private void AnnotationBarControl_AnnotationPropertyChanged(object sender, CPDFAnnotationType e)
  653. {
  654. PDFAnnotationControl.LoadAnnotationPanel(e);
  655. if (e != CPDFAnnotationType.Audio && e != CPDFAnnotationType.Image)
  656. {
  657. panelState.RightPanel = PanelState.RightPanelState.PropertyPanel;
  658. }
  659. }
  660. //private void PDFView_AnnotActiveHandler(object sender, AnnotAttribEvent e)
  661. //{
  662. // PropertyContainer.Child = PDFAnnotationControl;
  663. // PDFAnnotationControl.SetAnnotEventData(e);
  664. //}
  665. //private void PDFView_WidgetClickHandler(object sender, WidgetArgs e)
  666. //{
  667. // if ((e is WidgetSignArgs args))
  668. // {
  669. // var signatureWidget = args.Sign;
  670. // if(signatureWidget != null)
  671. // {
  672. // CPDFSignature sig = signatureWidget.GetSignature(PDFViewControl.PDFView.Document);
  673. // if (signatureWidget.IsSigned() && sig!=null && sig?.SignerList.Count > 0)
  674. // {
  675. // return;
  676. // }
  677. // }
  678. // if (args.WidgetType == C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS)
  679. // {
  680. // panelState.RightPanel = PanelState.RightPanelState.PropertyPanel;
  681. // CPDFSignatureUI signatureProperty = new CPDFSignatureUI();
  682. // signatureProperty.SetFormProperty(args, PDFViewControl.PDFView);
  683. // PropertyContainer.Child = signatureProperty;
  684. // }
  685. // }
  686. //}
  687. //private void PDFView_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
  688. //{
  689. // OnAnnotEditHandler.Invoke(this, null);
  690. //}
  691. private void CommandBinding_Executed_Undo(object sender, ExecutedRoutedEventArgs e)
  692. {
  693. if (PDFViewControl != null && PDFViewControl.PDFViewTool.GetCPDFViewer() != null && CanUndo)
  694. {
  695. PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager?.Undo();
  696. }
  697. }
  698. private void CommandBinding_Executed_Redo(object sender, ExecutedRoutedEventArgs e)
  699. {
  700. if (PDFViewControl != null && PDFViewControl.PDFViewTool.GetCPDFViewer() != null && CanRedo)
  701. {
  702. PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager?.Redo();
  703. }
  704. }
  705. private void CommandBinding_Executed_Highlight(object sender, ExecutedRoutedEventArgs e)
  706. {
  707. AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Highlight);
  708. }
  709. private void CommandBinding_Executed_Underline(object sender, ExecutedRoutedEventArgs e)
  710. {
  711. AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Underline);
  712. }
  713. private void CommandBinding_Executed_Strikeout(object sender, ExecutedRoutedEventArgs e)
  714. {
  715. AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Strikeout);
  716. }
  717. private void CommandBinding_Executed_Squiggly(object sender, ExecutedRoutedEventArgs e)
  718. {
  719. AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Squiggly);
  720. }
  721. #endregion
  722. }
  723. }