AnnotationControl.xaml.cs 34 KB

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