AnnotationControl.xaml.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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. private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
  91. {
  92. if (e.PropertyName == nameof(PanelState.IsLeftPanelExpand))
  93. {
  94. ExpandLeftPanel(panelState.IsLeftPanelExpand);
  95. }
  96. else if (e.PropertyName == nameof(PanelState.RightPanel))
  97. {
  98. if (panelState.RightPanel == PanelState.RightPanelState.PropertyPanel)
  99. {
  100. ExpandRightPropertyPanel(PDFAnnotationControl, Visibility.Visible);
  101. }
  102. else if (panelState.RightPanel == PanelState.RightPanelState.ViewSettings)
  103. {
  104. ExpandRightPropertyPanel(displaySettingsControl, Visibility.Visible);
  105. }
  106. else
  107. {
  108. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  109. }
  110. }
  111. }
  112. #region Init PDFViewer
  113. public void InitWithPDFViewer(CPDFViewer pdfViewer)
  114. {
  115. PDFViewControl.PDFView = pdfViewer;
  116. PDFGrid.Child = PDFViewControl;
  117. FloatPageTool.InitWithPDFViewer(pdfViewer);
  118. InitialPDFViewControl(PDFViewControl);
  119. }
  120. public void SetBOTAContainer(CPDFBOTABarControl botaControl)
  121. {
  122. this.BotaContainer.Child = botaControl;
  123. }
  124. public void SetDisplaySettingsControl(CPDFDisplaySettingsControl displaySettingsControl)
  125. {
  126. this.displaySettingsControl = displaySettingsControl;
  127. }
  128. public void ClearAllToolState()
  129. {
  130. this.AnnotationBarControl.ClearAllToolState();
  131. }
  132. public void SetToolBarContainerVisibility(Visibility visibility)
  133. {
  134. this.ToolBarContainer.Visibility = visibility;
  135. }
  136. #endregion
  137. #region Load Unload custom control
  138. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  139. {
  140. InitialPDFViewControl(PDFViewControl);
  141. }
  142. private void UserControl_Unloaded(object sender, RoutedEventArgs e)
  143. {
  144. PDFViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  145. PDFViewControl.PDFView.WidgetClickHandler -= PDFView_WidgetClickHandler;
  146. }
  147. private void AnnotationBarControl_Loaded(object sender, RoutedEventArgs e)
  148. {
  149. AnnotationBarControl.AnnotationPropertyChanged += AnnotationBarControl_AnnotationPropertyChanged;
  150. AnnotationBarControl.AnnotationCancel += AnnotationBarControl_AnnotationCancel;
  151. }
  152. private void AnnotationBarControl_Unloaded(object sender, RoutedEventArgs e)
  153. {
  154. AnnotationBarControl.AnnotationPropertyChanged -= AnnotationBarControl_AnnotationPropertyChanged;
  155. AnnotationBarControl.AnnotationCancel -= AnnotationBarControl_AnnotationCancel;
  156. }
  157. #endregion
  158. #region Annotation
  159. public void InitialPDFViewControl(PDFViewControl newPDFViewer)
  160. {
  161. PDFAnnotationControl.SetPDFViewer(newPDFViewer.PDFView);
  162. PDFAnnotationControl.AnnotationCancel();
  163. AnnotationBarControl.ClearAllToolState();
  164. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  165. PDFAnnotationControl.ClearAnnotationBar -= PdfAnnotationControl_ClearAnnotationBar;
  166. PDFAnnotationControl.ClearAnnotationBar += PdfAnnotationControl_ClearAnnotationBar;
  167. PDFViewControl.PDFView.AnnotEditHandler -= PDFView_AnnotEditHandler;
  168. PDFViewControl.PDFView.AnnotEditHandler += PDFView_AnnotEditHandler;
  169. PDFViewControl.PDFView.UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  170. PDFViewControl.PDFView.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  171. PDFViewControl.PDFView.AnnotActiveHandler -= PDFView_AnnotActiveHandler;
  172. PDFViewControl.PDFView.AnnotActiveHandler += PDFView_AnnotActiveHandler;
  173. PDFViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  174. PDFViewControl.PDFView.WidgetClickHandler -= PDFView_WidgetClickHandler;
  175. PDFViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
  176. PDFViewControl.PDFView.WidgetClickHandler += PDFView_WidgetClickHandler;
  177. }
  178. public void UnloadEvent()
  179. {
  180. PDFViewControl.PDFView.AnnotEditHandler -= PDFView_AnnotEditHandler;
  181. PDFViewControl.PDFView.AnnotActiveHandler -= PDFView_AnnotActiveHandler;
  182. //panelState.PropertyChanged -= PanelState_PropertyChanged;
  183. }
  184. private void PdfAnnotationControl_ClearAnnotationBar(object sender, EventArgs e)
  185. {
  186. AnnotationBarControl.ClearAllToolState();
  187. }
  188. public void SetViewSettings(Visibility visibility, CPDFDisplaySettingsControl displaySettingsControl = null)
  189. {
  190. this.PropertyContainer.Child = displaySettingsControl;
  191. this.PropertyContainer.Visibility = visibility;
  192. }
  193. #endregion
  194. #region Expand and collapse Panel
  195. public void ExpandRightPropertyPanel(UIElement propertytPanel, Visibility visible)
  196. {
  197. PropertyContainer.Width = 260;
  198. PropertyContainer.Child = propertytPanel;
  199. PropertyContainer.Visibility = visible;
  200. }
  201. public void ExpandLeftPanel(bool isExpand)
  202. {
  203. BotaContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  204. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  205. if (isExpand)
  206. {
  207. BodyGrid.ColumnDefinitions[0].Width = new GridLength(320);
  208. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  209. }
  210. else
  211. {
  212. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  213. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  214. }
  215. }
  216. #endregion
  217. #region Context menu
  218. private void PDFView_AnnotCommandHandler(object sender, AnnotCommandArgs e)
  219. {
  220. switch (e.CommandType)
  221. {
  222. case CommandType.Context:
  223. e.Handle = true;
  224. if (e.CommandTarget == TargetType.Annot)
  225. {
  226. e.Handle = true;
  227. e.PopupMenu = new ContextMenu();
  228. if (e.PressOnLink && AnnotationBarControl.CurrentMode == "Link")
  229. {
  230. e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  231. MenuItem propertyMenu = new MenuItem();
  232. propertyMenu = new MenuItem();
  233. propertyMenu.Header = "Edit";
  234. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(propertyMenu, "Click", EditLink_Click);
  235. propertyMenu.CommandParameter = e;
  236. e.PopupMenu.Items.Add(propertyMenu);
  237. }
  238. else if (e.PressOnAnnot)
  239. {
  240. e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  241. e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  242. e.PopupMenu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  243. }
  244. else if (e.PressOnMedia || e.PressOnSound)
  245. {
  246. e.Handle = true;
  247. e.PopupMenu.Items.Add(new MenuItem() { Header = "Play", Command = MediaCommands.Play, CommandTarget = (UIElement)sender, CommandParameter = e });
  248. e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  249. }
  250. else if (e.PressOnSelectedText)
  251. {
  252. e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  253. MenuItem highLightMenu = new MenuItem();
  254. highLightMenu.Header = "HighLight";
  255. highLightMenu.Click += (o, p) =>
  256. {
  257. TextHighlightAnnotArgs highLightArgs = new TextHighlightAnnotArgs();
  258. MouseModes oldMode = PDFViewControl.PDFView.MouseMode;
  259. if (PDFAnnotationControl != null)
  260. {
  261. highLightArgs.Color = System.Windows.Media.Colors.Red;
  262. highLightArgs.Transparency = 1;
  263. PDFViewControl.PDFView.SetMouseMode(MouseModes.AnnotCreate);
  264. PDFViewControl.PDFView.SetToolParam(highLightArgs);
  265. PDFViewControl.PDFView.SetMouseMode(oldMode);
  266. }
  267. };
  268. e.PopupMenu.Items.Add(highLightMenu);
  269. MenuItem underlineMenu = new MenuItem();
  270. underlineMenu.Header = "UnderLine";
  271. underlineMenu.Click += (o, p) =>
  272. {
  273. TextUnderlineAnnotArgs underlineArgs = new TextUnderlineAnnotArgs();
  274. MouseModes oldMode = PDFViewControl.PDFView.MouseMode;
  275. if (PDFAnnotationControl != null)
  276. {
  277. underlineArgs.Color = System.Windows.Media.Colors.Red;
  278. underlineArgs.Transparency = 1;
  279. PDFViewControl.PDFView.SetMouseMode(MouseModes.AnnotCreate);
  280. PDFViewControl.PDFView.SetToolParam(underlineArgs);
  281. PDFViewControl.PDFView.SetMouseMode(oldMode);
  282. }
  283. };
  284. e.PopupMenu.Items.Add(underlineMenu);
  285. MenuItem strikeOutMenu = new MenuItem();
  286. strikeOutMenu.Header = "StrikeOut";
  287. strikeOutMenu.Click += (o, p) =>
  288. {
  289. TextStrikeoutAnnotArgs strikeoutAnnotArgs = new TextStrikeoutAnnotArgs();
  290. MouseModes oldMode = PDFViewControl.PDFView.MouseMode;
  291. if (PDFAnnotationControl != null)
  292. {
  293. strikeoutAnnotArgs.Color = System.Windows.Media.Colors.Red;
  294. strikeoutAnnotArgs.Transparency = 1;
  295. PDFViewControl.PDFView.SetMouseMode(MouseModes.AnnotCreate);
  296. PDFViewControl.PDFView.SetToolParam(strikeoutAnnotArgs);
  297. PDFViewControl.PDFView.SetMouseMode(oldMode);
  298. }
  299. };
  300. e.PopupMenu.Items.Add(strikeOutMenu);
  301. MenuItem SquiggleMenu = new MenuItem();
  302. SquiggleMenu.Header = "Squiggle";
  303. SquiggleMenu.Click += (o, p) =>
  304. {
  305. TextSquigglyAnnotArgs squigglyAnnotArgs = new TextSquigglyAnnotArgs();
  306. MouseModes oldMode = PDFViewControl.PDFView.MouseMode;
  307. if (PDFAnnotationControl != null)
  308. {
  309. squigglyAnnotArgs.Color = System.Windows.Media.Colors.Red;
  310. squigglyAnnotArgs.Transparency = 1;
  311. PDFViewControl.PDFView.SetMouseMode(MouseModes.AnnotCreate);
  312. PDFViewControl.PDFView.SetToolParam(squigglyAnnotArgs);
  313. PDFViewControl.PDFView.SetMouseMode(oldMode);
  314. }
  315. };
  316. e.PopupMenu.Items.Add(SquiggleMenu);
  317. }
  318. else
  319. {
  320. e.Handle = true;
  321. e.PopupMenu = new ContextMenu();
  322. e.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  323. e.PopupMenu.Items.Add(new Separator());
  324. MenuItem fitWidthMenu = new MenuItem();
  325. fitWidthMenu.Header = "Automatically Resize";
  326. fitWidthMenu.Click += (o, p) =>
  327. {
  328. if (PDFViewControl != null)
  329. {
  330. PDFViewControl.PDFView?.ChangeFitMode(FitMode.FitWidth);
  331. }
  332. };
  333. e.PopupMenu.Items.Add(fitWidthMenu);
  334. MenuItem fitSizeMenu = new MenuItem();
  335. fitSizeMenu.Header = "Actual Size";
  336. fitSizeMenu.Click += (o, p) =>
  337. {
  338. if (PDFViewControl != null)
  339. {
  340. PDFViewControl.PDFView?.ChangeFitMode(FitMode.FitSize);
  341. }
  342. };
  343. e.PopupMenu.Items.Add(fitSizeMenu);
  344. MenuItem zoomInMenu = new MenuItem();
  345. zoomInMenu.Header = "Zoom In";
  346. zoomInMenu.Click += (o, p) =>
  347. {
  348. if (PDFViewControl != null)
  349. {
  350. double newZoom = CheckZoomLevel(PDFViewControl.PDFView.ZoomFactor + 0.01, true);
  351. PDFViewControl.PDFView?.Zoom(newZoom);
  352. }
  353. };
  354. e.PopupMenu.Items.Add(zoomInMenu);
  355. MenuItem zoomOutMenu = new MenuItem();
  356. zoomOutMenu.Header = "Zoom Out";
  357. zoomOutMenu.Click += (o, p) =>
  358. {
  359. if (PDFViewControl != null)
  360. {
  361. double newZoom = CheckZoomLevel(PDFViewControl.PDFView.ZoomFactor - 0.01, false);
  362. PDFViewControl.PDFView?.Zoom(newZoom);
  363. }
  364. };
  365. e.PopupMenu.Items.Add(zoomOutMenu);
  366. e.PopupMenu.Items.Add(new Separator());
  367. MenuItem singleView = new MenuItem();
  368. singleView.Header = "Single Page";
  369. singleView.Click += (o, p) =>
  370. {
  371. if (PDFViewControl != null)
  372. {
  373. PDFViewControl.PDFView?.ChangeViewMode(ViewMode.Single);
  374. }
  375. };
  376. e.PopupMenu.Items.Add(singleView);
  377. MenuItem singleContinuousView = new MenuItem();
  378. singleContinuousView.Header = "Single Page Continuous";
  379. singleContinuousView.Click += (o, p) =>
  380. {
  381. if (PDFViewControl != null)
  382. {
  383. PDFViewControl.PDFView?.ChangeViewMode(ViewMode.SingleContinuous);
  384. }
  385. };
  386. e.PopupMenu.Items.Add(singleContinuousView);
  387. MenuItem doubleView = new MenuItem();
  388. doubleView.Header = "Two Pages";
  389. doubleView.Click += (o, p) =>
  390. {
  391. if (PDFViewControl != null)
  392. {
  393. PDFViewControl.PDFView?.ChangeViewMode(ViewMode.Double);
  394. }
  395. };
  396. e.PopupMenu.Items.Add(doubleView);
  397. MenuItem doubleContinuousView = new MenuItem();
  398. doubleContinuousView.Header = "Two Pages Continuous";
  399. doubleContinuousView.Click += (o, p) =>
  400. {
  401. if (PDFViewControl != null)
  402. {
  403. PDFViewControl.PDFView?.ChangeViewMode(ViewMode.DoubleContinuous);
  404. }
  405. };
  406. e.PopupMenu.Items.Add(doubleContinuousView);
  407. MenuItem resetFormMenu = new MenuItem();
  408. resetFormMenu.Header = "Reset Forms";
  409. resetFormMenu.Click += (o, p) =>
  410. {
  411. if (PDFViewControl != null)
  412. {
  413. PDFViewControl.PDFView?.ResetForm(null);
  414. }
  415. };
  416. e.PopupMenu.Items.Add(new Separator());
  417. e.PopupMenu.Items.Add(resetFormMenu);
  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_WidgetClickHandler(object sender, WidgetArgs e)
  607. {
  608. if ((e is WidgetSignArgs args))
  609. {
  610. var signatureWidget = args.Sign;
  611. if(signatureWidget != null)
  612. {
  613. CPDFSignature sig = signatureWidget.GetSignature(PDFViewControl.PDFView.Document);
  614. if (signatureWidget.IsSigned() && sig!=null && sig?.SignerList.Count > 0)
  615. {
  616. return;
  617. }
  618. }
  619. if (args.WidgetType == C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS)
  620. {
  621. panelState.RightPanel = PanelState.RightPanelState.PropertyPanel;
  622. CPDFSignatureUI signatureProperty = new CPDFSignatureUI();
  623. signatureProperty.SetFormProperty(args, PDFViewControl.PDFView);
  624. PropertyContainer.Child = signatureProperty;
  625. }
  626. }
  627. }
  628. private void PDFView_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
  629. {
  630. OnAnnotEditHandler.Invoke(this, null);
  631. }
  632. private void CommandBinding_Executed_Undo(object sender, ExecutedRoutedEventArgs e)
  633. {
  634. if (PDFViewControl != null && PDFViewControl.PDFView != null && CanUndo)
  635. {
  636. PDFViewControl.PDFView.UndoManager?.Undo();
  637. }
  638. }
  639. private void CommandBinding_Executed_Redo(object sender, ExecutedRoutedEventArgs e)
  640. {
  641. if (PDFViewControl != null && PDFViewControl.PDFView != null && CanRedo)
  642. {
  643. PDFViewControl.PDFView.UndoManager?.Redo();
  644. }
  645. }
  646. private void CommandBinding_Executed_Highlight(object sender, ExecutedRoutedEventArgs e)
  647. {
  648. AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Highlight);
  649. }
  650. private void CommandBinding_Executed_Underline(object sender, ExecutedRoutedEventArgs e)
  651. {
  652. AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Underline);
  653. }
  654. private void CommandBinding_Executed_Strikeout(object sender, ExecutedRoutedEventArgs e)
  655. {
  656. AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Strikeout);
  657. }
  658. private void CommandBinding_Executed_Squiggly(object sender, ExecutedRoutedEventArgs e)
  659. {
  660. AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Squiggly);
  661. }
  662. #endregion
  663. public void ClearViewerControl()
  664. {
  665. PDFGrid.Child = null;
  666. BotaContainer.Child = null;
  667. PropertyContainer.Child = null;
  668. displaySettingsControl = null;
  669. }
  670. }
  671. }