AnnotationControl.xaml.cs 35 KB

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