AnnotationControl.xaml.cs 37 KB

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