AnnotationControl.xaml.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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. namespace Compdfkit_Tools.PDFControl
  24. {
  25. public partial class AnnotationControl : UserControl, INotifyPropertyChanged
  26. {
  27. #region Property
  28. private bool isFirstLoad = true;
  29. public PDFViewControl PDFViewControl = new PDFViewControl();
  30. public CPDFAnnotationControl PDFAnnotationControl = null;
  31. private CPDFDisplaySettingsControl displaySettingsControl = null;
  32. private PanelState panelState = PanelState.GetInstance();
  33. private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  34. public event PropertyChangedEventHandler PropertyChanged;
  35. public ICommand CloseTabCommand;
  36. public ICommand ExpandPropertyPanelCommand;
  37. public bool CanUndo
  38. {
  39. get
  40. {
  41. if (PDFViewControl != null && PDFViewControl.PDFView != null)
  42. {
  43. return PDFViewControl.PDFView.UndoManager.CanUndo;
  44. }
  45. return false;
  46. }
  47. }
  48. public bool CanRedo
  49. {
  50. get
  51. {
  52. if (PDFViewControl != null && PDFViewControl.PDFView != null)
  53. {
  54. return PDFViewControl.PDFView.UndoManager.CanRedo;
  55. }
  56. return false;
  57. }
  58. }
  59. private bool CanSave
  60. {
  61. get
  62. {
  63. if (PDFViewControl != null && PDFViewControl.PDFView != null)
  64. {
  65. return PDFViewControl.PDFView.UndoManager.CanSave;
  66. }
  67. return false;
  68. }
  69. }
  70. public event EventHandler<bool> OnCanSaveChanged;
  71. public event EventHandler OnAnnotEditHandler;
  72. #endregion
  73. public AnnotationControl()
  74. {
  75. InitializeComponent();
  76. DataContext = this;
  77. PDFAnnotationControl = new CPDFAnnotationControl();
  78. CPDFAnnotationType[] annotationProperties =
  79. {
  80. CPDFAnnotationType.Highlight, CPDFAnnotationType.Underline, CPDFAnnotationType.Strikeout,
  81. CPDFAnnotationType.Squiggly, CPDFAnnotationType.Freehand, CPDFAnnotationType.FreeText,
  82. CPDFAnnotationType.Note, CPDFAnnotationType.Circle, CPDFAnnotationType.Square,
  83. CPDFAnnotationType.Arrow, CPDFAnnotationType.Line, CPDFAnnotationType.Image,
  84. CPDFAnnotationType.Stamp, CPDFAnnotationType.Signature, CPDFAnnotationType.Link,
  85. CPDFAnnotationType.Audio
  86. };
  87. AnnotationBarControl.InitAnnotationBar(annotationProperties);
  88. panelState.PropertyChanged -= PanelState_PropertyChanged;
  89. panelState.PropertyChanged += PanelState_PropertyChanged;
  90. UndoBtn.ToolTip = LanguageHelper.CommonManager.GetString("Tooltip_Undo");
  91. RedoBtn.ToolTip = LanguageHelper.CommonManager.GetString("Tooltip_Redo");
  92. }
  93. #region Init PDFViewer
  94. public void InitWithPDFViewer(CPDFViewer pdfViewer)
  95. {
  96. PDFViewControl.PDFView = pdfViewer;
  97. PDFGrid.Child = PDFViewControl;
  98. FloatPageTool.InitWithPDFViewer(pdfViewer);
  99. InitialPDFViewControl(PDFViewControl);
  100. }
  101. #endregion
  102. #region Public Method
  103. public void ClearViewerControl()
  104. {
  105. PDFGrid.Child = null;
  106. BotaContainer.Child = null;
  107. PropertyContainer.Child = null;
  108. displaySettingsControl = null;
  109. }
  110. public void SetBOTAContainer(CPDFBOTABarControl botaControl)
  111. {
  112. this.BotaContainer.Child = botaControl;
  113. }
  114. public void SetDisplaySettingsControl(CPDFDisplaySettingsControl displaySettingsControl)
  115. {
  116. this.displaySettingsControl = displaySettingsControl;
  117. }
  118. public void ClearAllToolState()
  119. {
  120. this.AnnotationBarControl.ClearAllToolState();
  121. }
  122. public void SetToolBarContainerVisibility(Visibility visibility)
  123. {
  124. this.ToolBarContainer.Visibility = visibility;
  125. }
  126. #endregion
  127. #region Load Unload custom control
  128. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  129. {
  130. InitialPDFViewControl(PDFViewControl);
  131. }
  132. private void UserControl_Unloaded(object sender, RoutedEventArgs e)
  133. {
  134. PDFViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  135. PDFViewControl.PDFView.WidgetClickHandler -= PDFView_WidgetClickHandler;
  136. }
  137. private void AnnotationBarControl_Loaded(object sender, RoutedEventArgs e)
  138. {
  139. AnnotationBarControl.AnnotationPropertyChanged += AnnotationBarControl_AnnotationPropertyChanged;
  140. AnnotationBarControl.AnnotationCancel += AnnotationBarControl_AnnotationCancel;
  141. }
  142. private void AnnotationBarControl_Unloaded(object sender, RoutedEventArgs e)
  143. {
  144. AnnotationBarControl.AnnotationPropertyChanged -= AnnotationBarControl_AnnotationPropertyChanged;
  145. AnnotationBarControl.AnnotationCancel -= AnnotationBarControl_AnnotationCancel;
  146. }
  147. #endregion
  148. #region Annotation
  149. public void InitialPDFViewControl(PDFViewControl newPDFViewer)
  150. {
  151. PDFAnnotationControl.SetPDFViewer(newPDFViewer.PDFView);
  152. PDFAnnotationControl.AnnotationCancel();
  153. AnnotationBarControl.ClearAllToolState();
  154. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  155. PDFAnnotationControl.ClearAnnotationBar -= PdfAnnotationControl_ClearAnnotationBar;
  156. PDFAnnotationControl.ClearAnnotationBar += PdfAnnotationControl_ClearAnnotationBar;
  157. PDFViewControl.PDFView.AnnotEditHandler -= PDFView_AnnotEditHandler;
  158. PDFViewControl.PDFView.AnnotEditHandler += PDFView_AnnotEditHandler;
  159. PDFViewControl.PDFView.UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  160. PDFViewControl.PDFView.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  161. PDFViewControl.PDFView.AnnotActiveHandler -= PDFView_AnnotActiveHandler;
  162. PDFViewControl.PDFView.AnnotActiveHandler += PDFView_AnnotActiveHandler;
  163. PDFViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  164. PDFViewControl.PDFView.WidgetClickHandler -= PDFView_WidgetClickHandler;
  165. PDFViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
  166. PDFViewControl.PDFView.WidgetClickHandler += PDFView_WidgetClickHandler;
  167. }
  168. public void UnloadEvent()
  169. {
  170. PDFViewControl.PDFView.AnnotEditHandler -= PDFView_AnnotEditHandler;
  171. PDFViewControl.PDFView.AnnotActiveHandler -= PDFView_AnnotActiveHandler;
  172. //panelState.PropertyChanged -= PanelState_PropertyChanged;
  173. }
  174. private void PdfAnnotationControl_ClearAnnotationBar(object sender, EventArgs e)
  175. {
  176. AnnotationBarControl.ClearAllToolState();
  177. }
  178. public void SetViewSettings(Visibility visibility, CPDFDisplaySettingsControl displaySettingsControl = null)
  179. {
  180. this.PropertyContainer.Child = displaySettingsControl;
  181. this.PropertyContainer.Visibility = visibility;
  182. }
  183. #endregion
  184. #region Expand and collapse Panel
  185. public void ExpandRightPropertyPanel(UIElement propertytPanel, Visibility visible)
  186. {
  187. PropertyContainer.Width = 260;
  188. PropertyContainer.Child = propertytPanel;
  189. PropertyContainer.Visibility = visible;
  190. }
  191. public void ExpandLeftPanel(bool isExpand)
  192. {
  193. BotaContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  194. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  195. if (isExpand)
  196. {
  197. BodyGrid.ColumnDefinitions[0].Width = new GridLength(320);
  198. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  199. }
  200. else
  201. {
  202. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  203. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  204. }
  205. }
  206. #endregion
  207. #region Context menu
  208. private void PDFView_AnnotCommandHandler(object sender, AnnotCommandArgs e)
  209. {
  210. switch (e.CommandType)
  211. {
  212. case CommandType.Context:
  213. e.Handle = true;
  214. if (e.CommandTarget == TargetType.Annot)
  215. {
  216. e.Handle = true;
  217. e.PopupMenu = new ContextMenu();
  218. if (e.PressOnLink && AnnotationBarControl.CurrentMode == "Link")
  219. {
  220. e.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Delete"), Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  221. MenuItem propertyMenu = new MenuItem();
  222. propertyMenu = new MenuItem();
  223. propertyMenu.Header = "Edit";
  224. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(propertyMenu, "Click", EditLink_Click);
  225. propertyMenu.CommandParameter = e;
  226. e.PopupMenu.Items.Add(propertyMenu);
  227. }
  228. else if (e.PressOnAnnot)
  229. {
  230. e.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Delete"), Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  231. e.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Copy"), Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  232. e.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Cut"), Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  233. }
  234. else if (e.PressOnMedia || e.PressOnSound)
  235. {
  236. e.Handle = true;
  237. e.PopupMenu.Items.Add(new MenuItem() { Header = "Play", Command = MediaCommands.Play, CommandTarget = (UIElement)sender, CommandParameter = e });
  238. e.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Delete"), Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  239. }
  240. else if (e.PressOnSelectedText)
  241. {
  242. e.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Copy"), Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  243. MenuItem highLightMenu = new MenuItem();
  244. highLightMenu.Header = LanguageHelper.CommonManager.GetString("Menu_Highlight");
  245. highLightMenu.Click += (o, p) =>
  246. {
  247. TextHighlightAnnotArgs highLightArgs = new TextHighlightAnnotArgs();
  248. MouseModes oldMode = PDFViewControl.PDFView.MouseMode;
  249. if (PDFAnnotationControl != null)
  250. {
  251. highLightArgs.Color = System.Windows.Media.Colors.Red;
  252. highLightArgs.Transparency = 1;
  253. PDFViewControl.PDFView.SetMouseMode(MouseModes.AnnotCreate);
  254. PDFViewControl.PDFView.SetToolParam(highLightArgs);
  255. PDFViewControl.PDFView.SetMouseMode(oldMode);
  256. }
  257. };
  258. e.PopupMenu.Items.Add(highLightMenu);
  259. MenuItem underlineMenu = new MenuItem();
  260. underlineMenu.Header = LanguageHelper.CommonManager.GetString("Menu_Underline");
  261. underlineMenu.Click += (o, p) =>
  262. {
  263. TextUnderlineAnnotArgs underlineArgs = new TextUnderlineAnnotArgs();
  264. MouseModes oldMode = PDFViewControl.PDFView.MouseMode;
  265. if (PDFAnnotationControl != null)
  266. {
  267. underlineArgs.Color = System.Windows.Media.Colors.Red;
  268. underlineArgs.Transparency = 1;
  269. PDFViewControl.PDFView.SetMouseMode(MouseModes.AnnotCreate);
  270. PDFViewControl.PDFView.SetToolParam(underlineArgs);
  271. PDFViewControl.PDFView.SetMouseMode(oldMode);
  272. }
  273. };
  274. e.PopupMenu.Items.Add(underlineMenu);
  275. MenuItem strikeOutMenu = new MenuItem();
  276. strikeOutMenu.Header = LanguageHelper.CommonManager.GetString("Menu_Strikeout");
  277. strikeOutMenu.Click += (o, p) =>
  278. {
  279. TextStrikeoutAnnotArgs strikeoutAnnotArgs = new TextStrikeoutAnnotArgs();
  280. MouseModes oldMode = PDFViewControl.PDFView.MouseMode;
  281. if (PDFAnnotationControl != null)
  282. {
  283. strikeoutAnnotArgs.Color = System.Windows.Media.Colors.Red;
  284. strikeoutAnnotArgs.Transparency = 1;
  285. PDFViewControl.PDFView.SetMouseMode(MouseModes.AnnotCreate);
  286. PDFViewControl.PDFView.SetToolParam(strikeoutAnnotArgs);
  287. PDFViewControl.PDFView.SetMouseMode(oldMode);
  288. }
  289. };
  290. e.PopupMenu.Items.Add(strikeOutMenu);
  291. MenuItem SquiggleMenu = new MenuItem();
  292. SquiggleMenu.Header = LanguageHelper.CommonManager.GetString("Menu_Squiggly");
  293. SquiggleMenu.Click += (o, p) =>
  294. {
  295. TextSquigglyAnnotArgs squigglyAnnotArgs = new TextSquigglyAnnotArgs();
  296. MouseModes oldMode = PDFViewControl.PDFView.MouseMode;
  297. if (PDFAnnotationControl != null)
  298. {
  299. squigglyAnnotArgs.Color = System.Windows.Media.Colors.Red;
  300. squigglyAnnotArgs.Transparency = 1;
  301. PDFViewControl.PDFView.SetMouseMode(MouseModes.AnnotCreate);
  302. PDFViewControl.PDFView.SetToolParam(squigglyAnnotArgs);
  303. PDFViewControl.PDFView.SetMouseMode(oldMode);
  304. }
  305. };
  306. e.PopupMenu.Items.Add(SquiggleMenu);
  307. }
  308. else
  309. {
  310. e.Handle = true;
  311. e.PopupMenu = new ContextMenu();
  312. e.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Paste"), Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  313. e.PopupMenu.Items.Add(new Separator());
  314. MenuItem fitWidthMenu = new MenuItem();
  315. fitWidthMenu.Header = LanguageHelper.CommonManager.GetString("Menu_AutoSize");
  316. fitWidthMenu.Click += (o, p) =>
  317. {
  318. if (PDFViewControl != null)
  319. {
  320. PDFViewControl.PDFView?.ChangeFitMode(FitMode.FitWidth);
  321. }
  322. };
  323. e.PopupMenu.Items.Add(fitWidthMenu);
  324. MenuItem fitSizeMenu = new MenuItem();
  325. fitSizeMenu.Header = LanguageHelper.CommonManager.GetString("Menu_RealSize");
  326. fitSizeMenu.Click += (o, p) =>
  327. {
  328. if (PDFViewControl != null)
  329. {
  330. PDFViewControl.PDFView?.ChangeFitMode(FitMode.FitSize);
  331. }
  332. };
  333. e.PopupMenu.Items.Add(fitSizeMenu);
  334. MenuItem zoomInMenu = new MenuItem();
  335. zoomInMenu.Header = LanguageHelper.CommonManager.GetString("Menu_ZoomIn");
  336. zoomInMenu.Click += (o, p) =>
  337. {
  338. if (PDFViewControl != null)
  339. {
  340. double newZoom = CheckZoomLevel(PDFViewControl.PDFView.ZoomFactor + 0.01, true);
  341. PDFViewControl.PDFView?.Zoom(newZoom);
  342. }
  343. };
  344. e.PopupMenu.Items.Add(zoomInMenu);
  345. MenuItem zoomOutMenu = new MenuItem();
  346. zoomOutMenu.Header = LanguageHelper.CommonManager.GetString("Menu_ZoomOut");
  347. zoomOutMenu.Click += (o, p) =>
  348. {
  349. if (PDFViewControl != null)
  350. {
  351. double newZoom = CheckZoomLevel(PDFViewControl.PDFView.ZoomFactor - 0.01, false);
  352. PDFViewControl.PDFView?.Zoom(newZoom);
  353. }
  354. };
  355. e.PopupMenu.Items.Add(zoomOutMenu);
  356. e.PopupMenu.Items.Add(new Separator());
  357. MenuItem singleView = new MenuItem();
  358. singleView.Header = LanguageHelper.CommonManager.GetString("Menu_SinglePage");
  359. singleView.Click += (o, p) =>
  360. {
  361. if (PDFViewControl != null)
  362. {
  363. PDFViewControl.PDFView?.ChangeViewMode(ViewMode.Single);
  364. }
  365. };
  366. e.PopupMenu.Items.Add(singleView);
  367. MenuItem singleContinuousView = new MenuItem();
  368. singleContinuousView.Header = LanguageHelper.CommonManager.GetString("Menu_SingleContinuous");
  369. singleContinuousView.Click += (o, p) =>
  370. {
  371. if (PDFViewControl != null)
  372. {
  373. PDFViewControl.PDFView?.ChangeViewMode(ViewMode.SingleContinuous);
  374. }
  375. };
  376. e.PopupMenu.Items.Add(singleContinuousView);
  377. MenuItem doubleView = new MenuItem();
  378. doubleView.Header = LanguageHelper.CommonManager.GetString("Menu_DoublePage");
  379. doubleView.Click += (o, p) =>
  380. {
  381. if (PDFViewControl != null)
  382. {
  383. PDFViewControl.PDFView?.ChangeViewMode(ViewMode.Double);
  384. }
  385. };
  386. e.PopupMenu.Items.Add(doubleView);
  387. MenuItem doubleContinuousView = new MenuItem();
  388. doubleContinuousView.Header = LanguageHelper.CommonManager.GetString("Menu_DoubleContinuous");
  389. doubleContinuousView.Click += (o, p) =>
  390. {
  391. if (PDFViewControl != null)
  392. {
  393. PDFViewControl.PDFView?.ChangeViewMode(ViewMode.DoubleContinuous);
  394. }
  395. };
  396. e.PopupMenu.Items.Add(doubleContinuousView);
  397. MenuItem resetFormMenu = new MenuItem();
  398. resetFormMenu.Header = LanguageHelper.CommonManager.GetString("Menu_Reset");
  399. resetFormMenu.Click += (o, p) =>
  400. {
  401. if (PDFViewControl != null)
  402. {
  403. PDFViewControl.PDFView?.ResetForm(null);
  404. }
  405. };
  406. e.PopupMenu.Items.Add(new Separator());
  407. e.PopupMenu.Items.Add(resetFormMenu);
  408. }
  409. }
  410. else if (e.CommandTarget == TargetType.ImageSelection)
  411. {
  412. if (PDFViewControl != null && PDFViewControl.PDFView != null && PDFViewControl.PDFView.GetSelectImageCount() > 0)
  413. {
  414. e.Handle = true;
  415. e.PopupMenu = new ContextMenu();
  416. MenuItem imageCopyMenu = new MenuItem();
  417. imageCopyMenu = new MenuItem();
  418. imageCopyMenu.Header = "Copy Images";
  419. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageCopyMenu, "Click", CopyImage_Click);
  420. imageCopyMenu.CommandParameter = e;
  421. e.PopupMenu.Items.Add(imageCopyMenu);
  422. MenuItem imageExtraMenu = new MenuItem();
  423. imageExtraMenu = new MenuItem();
  424. imageExtraMenu.Header = "Extract Images";
  425. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageExtraMenu, "Click", ExtraImage_Click);
  426. imageExtraMenu.CommandParameter = e;
  427. e.PopupMenu.Items.Add(imageExtraMenu);
  428. }
  429. }
  430. break;
  431. case CommandType.Copy:
  432. {
  433. if (PDFViewControl == null) return;
  434. if (!PDFViewControl.PDFView.Document.GetPermissionsInfo().AllowsCopying)
  435. {
  436. if(!PasswordHelper.UnlockWithOwnerPassword(PDFViewControl.PDFView.Document))
  437. {
  438. return;
  439. }
  440. }
  441. e.DoCommand();
  442. break;
  443. }
  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. private 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. private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
  584. {
  585. if (e.PropertyName == nameof(PanelState.IsLeftPanelExpand))
  586. {
  587. ExpandLeftPanel(panelState.IsLeftPanelExpand);
  588. }
  589. else if (e.PropertyName == nameof(PanelState.RightPanel))
  590. {
  591. if (panelState.RightPanel == PanelState.RightPanelState.PropertyPanel)
  592. {
  593. ExpandRightPropertyPanel(PDFAnnotationControl, Visibility.Visible);
  594. }
  595. else if (panelState.RightPanel == PanelState.RightPanelState.ViewSettings)
  596. {
  597. ExpandRightPropertyPanel(displaySettingsControl, Visibility.Visible);
  598. }
  599. else
  600. {
  601. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  602. }
  603. }
  604. }
  605. #endregion
  606. #region Event handle
  607. private void AnnotationBarControl_AnnotationCancel(object sender, EventArgs e)
  608. {
  609. PDFAnnotationControl.AnnotationCancel();
  610. if (panelState.RightPanel == PanelState.RightPanelState.PropertyPanel)
  611. {
  612. panelState.RightPanel = PanelState.RightPanelState.None;
  613. }
  614. }
  615. private void AnnotationBarControl_AnnotationPropertyChanged(object sender, CPDFAnnotationType e)
  616. {
  617. PDFAnnotationControl.LoadAnnotationPanel(e);
  618. if (e != CPDFAnnotationType.Audio && e != CPDFAnnotationType.Image)
  619. {
  620. panelState.RightPanel = PanelState.RightPanelState.PropertyPanel;
  621. }
  622. }
  623. private void PDFView_AnnotActiveHandler(object sender, AnnotAttribEvent e)
  624. {
  625. PropertyContainer.Child = PDFAnnotationControl;
  626. PDFAnnotationControl.SetAnnotEventData(e);
  627. }
  628. private void PDFView_WidgetClickHandler(object sender, WidgetArgs e)
  629. {
  630. if ((e is WidgetSignArgs args))
  631. {
  632. var signatureWidget = args.Sign;
  633. if(signatureWidget != null)
  634. {
  635. CPDFSignature sig = signatureWidget.GetSignature(PDFViewControl.PDFView.Document);
  636. if (signatureWidget.IsSigned() && sig!=null && sig?.SignerList.Count > 0)
  637. {
  638. return;
  639. }
  640. }
  641. if (args.WidgetType == C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS)
  642. {
  643. panelState.RightPanel = PanelState.RightPanelState.PropertyPanel;
  644. CPDFSignatureUI signatureProperty = new CPDFSignatureUI();
  645. signatureProperty.SetFormProperty(args, PDFViewControl.PDFView);
  646. PropertyContainer.Child = signatureProperty;
  647. }
  648. }
  649. }
  650. private void PDFView_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
  651. {
  652. OnAnnotEditHandler.Invoke(this, null);
  653. }
  654. private void CommandBinding_Executed_Undo(object sender, ExecutedRoutedEventArgs e)
  655. {
  656. if (PDFViewControl != null && PDFViewControl.PDFView != null && CanUndo)
  657. {
  658. PDFViewControl.PDFView.UndoManager?.Undo();
  659. }
  660. }
  661. private void CommandBinding_Executed_Redo(object sender, ExecutedRoutedEventArgs e)
  662. {
  663. if (PDFViewControl != null && PDFViewControl.PDFView != null && CanRedo)
  664. {
  665. PDFViewControl.PDFView.UndoManager?.Redo();
  666. }
  667. }
  668. private void CommandBinding_Executed_Highlight(object sender, ExecutedRoutedEventArgs e)
  669. {
  670. AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Highlight);
  671. }
  672. private void CommandBinding_Executed_Underline(object sender, ExecutedRoutedEventArgs e)
  673. {
  674. AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Underline);
  675. }
  676. private void CommandBinding_Executed_Strikeout(object sender, ExecutedRoutedEventArgs e)
  677. {
  678. AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Strikeout);
  679. }
  680. private void CommandBinding_Executed_Squiggly(object sender, ExecutedRoutedEventArgs e)
  681. {
  682. AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Squiggly);
  683. }
  684. #endregion
  685. }
  686. }