AnnotationControl.xaml.cs 36 KB

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