AnnotationControl.xaml.cs 37 KB

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