AnnotationControl.xaml.cs 31 KB

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