AnnotationControl.xaml.cs 32 KB

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