AnnotationControl.xaml.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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. MenuItem resetFormMenu= new MenuItem();
  324. resetFormMenu.Header = "Reset Forms";
  325. resetFormMenu.Click += (o, p) =>
  326. {
  327. if (PdfViewControl != null)
  328. {
  329. PdfViewControl.PDFView?.ResetForm(null);
  330. }
  331. };
  332. e.PopupMenu.Items.Add(resetFormMenu);
  333. e.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  334. e.PopupMenu.Items.Add(new Separator());
  335. MenuItem fitWidthMenu = new MenuItem();
  336. fitWidthMenu.Header = "Automatically Resize";
  337. fitWidthMenu.Click += (o, p) =>
  338. {
  339. if (PdfViewControl != null)
  340. {
  341. PdfViewControl.PDFView?.ChangeFitMode(FitMode.FitWidth);
  342. }
  343. };
  344. e.PopupMenu.Items.Add(fitWidthMenu);
  345. MenuItem fitSizeMenu = new MenuItem();
  346. fitSizeMenu.Header = "Actual Size";
  347. fitSizeMenu.Click += (o, p) =>
  348. {
  349. if (PdfViewControl != null)
  350. {
  351. PdfViewControl.PDFView?.ChangeFitMode(FitMode.FitSize);
  352. }
  353. };
  354. e.PopupMenu.Items.Add(fitSizeMenu);
  355. MenuItem zoomInMenu = new MenuItem();
  356. zoomInMenu.Header = "Zoom In";
  357. zoomInMenu.Click += (o, p) =>
  358. {
  359. if (PdfViewControl != null)
  360. {
  361. double newZoom = CheckZoomLevel(PdfViewControl.PDFView.ZoomFactor + 0.01, true);
  362. PdfViewControl.PDFView?.Zoom(newZoom);
  363. }
  364. };
  365. e.PopupMenu.Items.Add(zoomInMenu);
  366. MenuItem zoomOutMenu = new MenuItem();
  367. zoomOutMenu.Header = "Zoom Out";
  368. zoomOutMenu.Click += (o, p) =>
  369. {
  370. if (PdfViewControl != null)
  371. {
  372. double newZoom = CheckZoomLevel(PdfViewControl.PDFView.ZoomFactor - 0.01, false);
  373. PdfViewControl.PDFView?.Zoom(newZoom);
  374. }
  375. };
  376. e.PopupMenu.Items.Add(zoomOutMenu);
  377. e.PopupMenu.Items.Add(new Separator());
  378. MenuItem singleView = new MenuItem();
  379. singleView.Header = "Single Page";
  380. singleView.Click += (o, p) =>
  381. {
  382. if (PdfViewControl != null)
  383. {
  384. PdfViewControl.PDFView?.ChangeViewMode(ViewMode.Single);
  385. }
  386. };
  387. e.PopupMenu.Items.Add(singleView);
  388. MenuItem singleContinuousView = new MenuItem();
  389. singleContinuousView.Header = "Single Page Continuous";
  390. singleContinuousView.Click += (o, p) =>
  391. {
  392. if (PdfViewControl != null)
  393. {
  394. PdfViewControl.PDFView?.ChangeViewMode(ViewMode.SingleContinuous);
  395. }
  396. };
  397. e.PopupMenu.Items.Add(singleContinuousView);
  398. MenuItem doubleView = new MenuItem();
  399. doubleView.Header = "Two Pages";
  400. doubleView.Click += (o, p) =>
  401. {
  402. if (PdfViewControl != null)
  403. {
  404. PdfViewControl.PDFView?.ChangeViewMode(ViewMode.Double);
  405. }
  406. };
  407. e.PopupMenu.Items.Add(doubleView);
  408. MenuItem doubleContinuousView = new MenuItem();
  409. doubleContinuousView.Header = "Two Pages Continuous";
  410. doubleContinuousView.Click += (o, p) =>
  411. {
  412. if (PdfViewControl != null)
  413. {
  414. PdfViewControl.PDFView?.ChangeViewMode(ViewMode.DoubleContinuous);
  415. }
  416. };
  417. e.PopupMenu.Items.Add(doubleContinuousView);
  418. }
  419. }
  420. else if (e.CommandTarget == TargetType.ImageSelection)
  421. {
  422. if (PdfViewControl != null && PdfViewControl.PDFView != null && PdfViewControl.PDFView.GetSelectImageCount() > 0)
  423. {
  424. e.Handle = true;
  425. e.PopupMenu = new ContextMenu();
  426. MenuItem imageCopyMenu = new MenuItem();
  427. imageCopyMenu = new MenuItem();
  428. imageCopyMenu.Header = "Copy Images";
  429. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageCopyMenu, "Click", CopyImage_Click);
  430. imageCopyMenu.CommandParameter = e;
  431. e.PopupMenu.Items.Add(imageCopyMenu);
  432. MenuItem imageExtraMenu = new MenuItem();
  433. imageExtraMenu = new MenuItem();
  434. imageExtraMenu.Header = "Extract Images";
  435. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageExtraMenu, "Click", ExtraImage_Click);
  436. imageExtraMenu.CommandParameter = e;
  437. e.PopupMenu.Items.Add(imageExtraMenu);
  438. }
  439. }
  440. break;
  441. case CommandType.Copy:
  442. e.DoCommand();
  443. break;
  444. case CommandType.Cut:
  445. case CommandType.Paste:
  446. case CommandType.Delete:
  447. e.DoCommand();
  448. break;
  449. default:
  450. break;
  451. }
  452. }
  453. private void CopyImage_Click(object sender, RoutedEventArgs e)
  454. {
  455. try
  456. {
  457. Dictionary<int, List<Bitmap>> imageDict = PdfViewControl.PDFView?.GetSelectedImages();
  458. if (imageDict != null && imageDict.Count > 0)
  459. {
  460. foreach (int pageIndex in imageDict.Keys)
  461. {
  462. List<Bitmap> imageList = imageDict[pageIndex];
  463. foreach (Bitmap image in imageList)
  464. {
  465. MemoryStream ms = new MemoryStream();
  466. image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  467. BitmapImage imageData = new BitmapImage();
  468. imageData.BeginInit();
  469. imageData.StreamSource = ms;
  470. imageData.CacheOption = BitmapCacheOption.OnLoad;
  471. imageData.EndInit();
  472. imageData.Freeze();
  473. Clipboard.SetImage(imageData);
  474. break;
  475. }
  476. }
  477. }
  478. }
  479. catch (Exception ex)
  480. {
  481. }
  482. }
  483. private void ExtraImage_Click(object sender, RoutedEventArgs e)
  484. {
  485. System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();
  486. if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  487. {
  488. string choosePath = folderDialog.SelectedPath;
  489. string openPath = choosePath;
  490. try
  491. {
  492. Dictionary<int, List<Bitmap>> imageDict = PdfViewControl.PDFView?.GetSelectedImages();
  493. if (imageDict != null && imageDict.Count > 0)
  494. {
  495. foreach (int pageIndex in imageDict.Keys)
  496. {
  497. List<Bitmap> imageList = imageDict[pageIndex];
  498. foreach (Bitmap image in imageList)
  499. {
  500. string savePath = Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
  501. image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  502. openPath = savePath;
  503. }
  504. }
  505. }
  506. Process.Start("explorer", "/select,\"" + openPath + "\"");
  507. }
  508. catch (Exception ex)
  509. {
  510. }
  511. }
  512. }
  513. #endregion
  514. #region UI
  515. private double CheckZoomLevel(double zoom, bool IsGrowth)
  516. {
  517. double standardZoom = 100;
  518. if (zoom <= 0.01)
  519. {
  520. return 0.01;
  521. }
  522. if (zoom >= 10)
  523. {
  524. return 10;
  525. }
  526. zoom *= 100;
  527. for (int i = 0; i < zoomLevelList.Length - 1; i++)
  528. {
  529. if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth)
  530. {
  531. standardZoom = zoomLevelList[i + 1];
  532. break;
  533. }
  534. if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth)
  535. {
  536. standardZoom = zoomLevelList[i];
  537. break;
  538. }
  539. }
  540. return standardZoom / 100;
  541. }
  542. private void ToolExpand_Click(object sender, RoutedEventArgs e)
  543. {
  544. ToggleButton expandBtn = sender as ToggleButton;
  545. if (expandBtn != null)
  546. {
  547. bool isExpand = expandBtn.IsChecked == true;
  548. ExpandLeftPanel(isExpand);
  549. }
  550. }
  551. private void EditLink_Click(object sender, RoutedEventArgs e)
  552. {
  553. PropertyContainer.Visibility = Visibility.Visible;
  554. }
  555. private void UndoButton_Click(object sender, RoutedEventArgs e)
  556. {
  557. if (PdfViewControl != null && PdfViewControl.PDFView != null)
  558. {
  559. PdfViewControl.PDFView.UndoManager?.Undo();
  560. }
  561. }
  562. private void RedoButton_Click(object sender, RoutedEventArgs e)
  563. {
  564. if (PdfViewControl != null && PdfViewControl.PDFView != null)
  565. {
  566. PdfViewControl.PDFView.UndoManager?.Redo();
  567. }
  568. }
  569. #endregion
  570. #region Property changed
  571. protected void OnPropertyChanged([CallerMemberName] string name = null)
  572. {
  573. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  574. }
  575. public void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  576. {
  577. OnPropertyChanged(e.PropertyName);
  578. if (e.PropertyName == "CanSave")
  579. {
  580. OnCanSaveChanged?.Invoke(this, CanSave);
  581. }
  582. }
  583. #endregion
  584. #region Event handle
  585. private void AnnotationBarControl_AnnotationCancel(object sender, EventArgs e)
  586. {
  587. PDFAnnotationControl.AnnotationCancel();
  588. if (panelState.RightPanel == PanelState.RightPanelState.PropertyPanel)
  589. {
  590. panelState.RightPanel = PanelState.RightPanelState.None;
  591. }
  592. }
  593. private void AnnotationBarControl_AnnotationPropertyChanged(object sender, CPDFAnnotationType e)
  594. {
  595. PDFAnnotationControl.LoadAnnotationPanel(e);
  596. if (e != CPDFAnnotationType.Audio && e != CPDFAnnotationType.Image)
  597. {
  598. panelState.RightPanel = PanelState.RightPanelState.PropertyPanel;
  599. }
  600. }
  601. private void PDFView_AnnotActiveHandler(object sender, AnnotAttribEvent e)
  602. {
  603. PropertyContainer.Child = PDFAnnotationControl;
  604. PDFAnnotationControl.SetAnnotEventData(e);
  605. }
  606. private void PDFView_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
  607. {
  608. OnAnnotEditHandler.Invoke(this, null);
  609. }
  610. private void CommandBinding_Executed_Undo(object sender, ExecutedRoutedEventArgs e)
  611. {
  612. if (PdfViewControl != null && PdfViewControl.PDFView != null && CanUndo)
  613. {
  614. PdfViewControl.PDFView.UndoManager?.Undo();
  615. }
  616. }
  617. private void CommandBinding_Executed_Redo(object sender, ExecutedRoutedEventArgs e)
  618. {
  619. if (PdfViewControl != null && PdfViewControl.PDFView != null && CanRedo)
  620. {
  621. PdfViewControl.PDFView.UndoManager?.Redo();
  622. }
  623. }
  624. private void CommandBinding_Executed_Highlight(object sender, ExecutedRoutedEventArgs e)
  625. {
  626. AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Highlight);
  627. }
  628. private void CommandBinding_Executed_Underline(object sender, ExecutedRoutedEventArgs e)
  629. {
  630. AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Underline);
  631. }
  632. private void CommandBinding_Executed_Strikeout(object sender, ExecutedRoutedEventArgs e)
  633. {
  634. AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Strikeout);
  635. }
  636. private void CommandBinding_Executed_Squiggly(object sender, ExecutedRoutedEventArgs e)
  637. {
  638. AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Squiggly);
  639. }
  640. #endregion
  641. public void ClearViewerControl()
  642. {
  643. PDFGrid.Child = null;
  644. BotaContainer.Child = null;
  645. PropertyContainer.Child = null;
  646. }
  647. }
  648. }