AnnotationControl.xaml.cs 30 KB

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