AnnotationControl.xaml.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. using System.Windows.Controls;
  2. using Compdfkit_Tools.Data;
  3. using ComPDFKitViewer;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Diagnostics;
  8. using System.Drawing;
  9. using System.IO;
  10. using System.Runtime.CompilerServices;
  11. using System.Windows;
  12. using System.Windows.Controls.Primitives;
  13. using System.Windows.Input;
  14. using System.Windows.Media.Imaging;
  15. using Compdfkit_Tools.Annotation.PDFAnnotationPanel.PDFAnnotationUI;
  16. using Compdfkit_Tools.Helper;
  17. using ComPDFKit.DigitalSign;
  18. using ComPDFKit.PDFAnnotation.Form;
  19. using ComPDFKit.PDFAnnotation;
  20. namespace Compdfkit_Tools.PDFControl
  21. {
  22. public partial class AnnotationControl : UserControl, INotifyPropertyChanged
  23. {
  24. #region Property
  25. private bool isFirstLoad = true;
  26. public PDFViewControl PDFViewControl;
  27. public CPDFAnnotationControl PDFAnnotationControl = null;
  28. private CPDFDisplaySettingsControl displaySettingsControl = null;
  29. private PanelState panelState = PanelState.GetInstance();
  30. private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  31. public event PropertyChangedEventHandler PropertyChanged;
  32. public ICommand CloseTabCommand;
  33. public ICommand ExpandPropertyPanelCommand;
  34. public bool CanUndo
  35. {
  36. get
  37. {
  38. if (PDFViewControl != null && PDFViewControl.PDFViewTool.GetCPDFViewer() != null)
  39. {
  40. return PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager.CanUndo;
  41. }
  42. return false;
  43. }
  44. }
  45. public bool CanRedo
  46. {
  47. get
  48. {
  49. if (PDFViewControl != null && PDFViewControl.PDFViewTool.GetCPDFViewer() != null)
  50. {
  51. return PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager.CanRedo;
  52. }
  53. return false;
  54. }
  55. }
  56. private bool CanSave
  57. {
  58. get
  59. {
  60. if (PDFViewControl != null && PDFViewControl.PDFViewTool.GetCPDFViewer() != null)
  61. {
  62. if (PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager.CanRedo ||
  63. PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager.CanUndo)
  64. {
  65. return true;
  66. }
  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. }
  92. #region Init PDFViewer
  93. public void InitWithPDFViewer(PDFViewControl pdfViewer)
  94. {
  95. PDFViewControl = pdfViewer;
  96. PDFGrid.Child = PDFViewControl;
  97. FloatPageTool.InitWithPDFViewer(PDFViewControl);
  98. InitialPDFViewControl(PDFViewControl);
  99. }
  100. #endregion
  101. #region Public Method
  102. public void ClearViewerControl()
  103. {
  104. PDFGrid.Child = null;
  105. BotaContainer.Child = null;
  106. PropertyContainer.Child = null;
  107. displaySettingsControl = null;
  108. PDFViewControl.SetCreateAnnotType(C_ANNOTATION_TYPE.C_ANNOTATION_NONE);
  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);
  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 = "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 = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  231. // e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  232. // e.PopupMenu.Items.Add(new MenuItem() { Header = "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 = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  239. // }
  240. // else if (e.PressOnSelectedText)
  241. // {
  242. // e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  243. // MenuItem highLightMenu = new MenuItem();
  244. // highLightMenu.Header = "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 = "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 = "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 = "Squiggle";
  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 = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  313. // e.PopupMenu.Items.Add(new Separator());
  314. // MenuItem fitWidthMenu = new MenuItem();
  315. // fitWidthMenu.Header = "Automatically Resize";
  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 = "Actual Size";
  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 = "Zoom In";
  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 = "Zoom Out";
  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 = "Single Page";
  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 = "Single Page Continuous";
  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 = "Two Pages";
  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 = "Two Pages Continuous";
  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 = "Reset Forms";
  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. // e.DoCommand();
  433. // break;
  434. // case CommandType.Cut:
  435. // case CommandType.Paste:
  436. // case CommandType.Delete:
  437. // e.DoCommand();
  438. // break;
  439. // default:
  440. // break;
  441. // }
  442. //}
  443. //private void CopyImage_Click(object sender, RoutedEventArgs e)
  444. //{
  445. // try
  446. // {
  447. // Dictionary<int, List<Bitmap>> imageDict = PDFViewControl.PDFView?.GetSelectedImages();
  448. // if (imageDict != null && imageDict.Count > 0)
  449. // {
  450. // foreach (int pageIndex in imageDict.Keys)
  451. // {
  452. // List<Bitmap> imageList = imageDict[pageIndex];
  453. // foreach (Bitmap image in imageList)
  454. // {
  455. // MemoryStream ms = new MemoryStream();
  456. // image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  457. // BitmapImage imageData = new BitmapImage();
  458. // imageData.BeginInit();
  459. // imageData.StreamSource = ms;
  460. // imageData.CacheOption = BitmapCacheOption.OnLoad;
  461. // imageData.EndInit();
  462. // imageData.Freeze();
  463. // Clipboard.SetImage(imageData);
  464. // break;
  465. // }
  466. // }
  467. // }
  468. // }
  469. // catch (Exception ex)
  470. // {
  471. // }
  472. //}
  473. //private void ExtraImage_Click(object sender, RoutedEventArgs e)
  474. //{
  475. // System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();
  476. // if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  477. // {
  478. // string choosePath = folderDialog.SelectedPath;
  479. // string openPath = choosePath;
  480. // try
  481. // {
  482. // Dictionary<int, List<Bitmap>> imageDict = PDFViewControl.PDFView?.GetSelectedImages();
  483. // if (imageDict != null && imageDict.Count > 0)
  484. // {
  485. // foreach (int pageIndex in imageDict.Keys)
  486. // {
  487. // List<Bitmap> imageList = imageDict[pageIndex];
  488. // foreach (Bitmap image in imageList)
  489. // {
  490. // string savePath = Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
  491. // image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  492. // openPath = savePath;
  493. // }
  494. // }
  495. // }
  496. // Process.Start("explorer", "/select,\"" + openPath + "\"");
  497. // }
  498. // catch (Exception ex)
  499. // {
  500. // }
  501. // }
  502. //}
  503. //#endregion
  504. #region UI
  505. private double CheckZoomLevel(double zoom, bool IsGrowth)
  506. {
  507. double standardZoom = 100;
  508. if (zoom <= 0.01)
  509. {
  510. return 0.01;
  511. }
  512. if (zoom >= 10)
  513. {
  514. return 10;
  515. }
  516. zoom *= 100;
  517. for (int i = 0; i < zoomLevelList.Length - 1; i++)
  518. {
  519. if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth)
  520. {
  521. standardZoom = zoomLevelList[i + 1];
  522. break;
  523. }
  524. if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth)
  525. {
  526. standardZoom = zoomLevelList[i];
  527. break;
  528. }
  529. }
  530. return standardZoom / 100;
  531. }
  532. private void ToolExpand_Click(object sender, RoutedEventArgs e)
  533. {
  534. ToggleButton expandBtn = sender as ToggleButton;
  535. if (expandBtn != null)
  536. {
  537. bool isExpand = expandBtn.IsChecked == true;
  538. ExpandLeftPanel(isExpand);
  539. }
  540. }
  541. private void EditLink_Click(object sender, RoutedEventArgs e)
  542. {
  543. PropertyContainer.Visibility = Visibility.Visible;
  544. }
  545. private void UndoButton_Click(object sender, RoutedEventArgs e)
  546. {
  547. if (PDFViewControl != null && PDFViewControl.PDFViewTool.GetCPDFViewer() != null)
  548. {
  549. PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager?.Undo();
  550. }
  551. }
  552. private void RedoButton_Click(object sender, RoutedEventArgs e)
  553. {
  554. if (PDFViewControl != null && PDFViewControl.PDFViewTool.GetCPDFViewer() != null)
  555. {
  556. PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager?.Redo();
  557. }
  558. }
  559. #endregion
  560. #region Property changed
  561. protected void OnPropertyChanged([CallerMemberName] string name = null)
  562. {
  563. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  564. }
  565. private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  566. {
  567. OnPropertyChanged(e.PropertyName);
  568. if (e.PropertyName == "CanSave")
  569. {
  570. OnCanSaveChanged?.Invoke(this, CanSave);
  571. }
  572. }
  573. private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
  574. {
  575. if (e.PropertyName == nameof(PanelState.IsLeftPanelExpand))
  576. {
  577. ExpandLeftPanel(panelState.IsLeftPanelExpand);
  578. }
  579. else if (e.PropertyName == nameof(PanelState.RightPanel))
  580. {
  581. if (panelState.RightPanel == PanelState.RightPanelState.PropertyPanel)
  582. {
  583. ExpandRightPropertyPanel(PDFAnnotationControl, Visibility.Visible);
  584. }
  585. else if (panelState.RightPanel == PanelState.RightPanelState.ViewSettings)
  586. {
  587. ExpandRightPropertyPanel(displaySettingsControl, Visibility.Visible);
  588. }
  589. else
  590. {
  591. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  592. }
  593. }
  594. }
  595. #endregion
  596. #region Event handle
  597. private void AnnotationBarControl_AnnotationCancel(object sender, EventArgs e)
  598. {
  599. PDFAnnotationControl.AnnotationCancel();
  600. if (panelState.RightPanel == PanelState.RightPanelState.PropertyPanel)
  601. {
  602. panelState.RightPanel = PanelState.RightPanelState.None;
  603. }
  604. }
  605. private void AnnotationBarControl_AnnotationPropertyChanged(object sender, CPDFAnnotationType e)
  606. {
  607. PDFAnnotationControl.LoadAnnotationPanel(e);
  608. if (e != CPDFAnnotationType.Audio && e != CPDFAnnotationType.Image)
  609. {
  610. panelState.RightPanel = PanelState.RightPanelState.PropertyPanel;
  611. }
  612. }
  613. //private void PDFView_AnnotActiveHandler(object sender, AnnotAttribEvent e)
  614. //{
  615. // PropertyContainer.Child = PDFAnnotationControl;
  616. // PDFAnnotationControl.SetAnnotEventData(e);
  617. //}
  618. //private void PDFView_WidgetClickHandler(object sender, WidgetArgs e)
  619. //{
  620. // if ((e is WidgetSignArgs args))
  621. // {
  622. // var signatureWidget = args.Sign;
  623. // if(signatureWidget != null)
  624. // {
  625. // CPDFSignature sig = signatureWidget.GetSignature(PDFViewControl.PDFView.Document);
  626. // if (signatureWidget.IsSigned() && sig!=null && sig?.SignerList.Count > 0)
  627. // {
  628. // return;
  629. // }
  630. // }
  631. // if (args.WidgetType == C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS)
  632. // {
  633. // panelState.RightPanel = PanelState.RightPanelState.PropertyPanel;
  634. // CPDFSignatureUI signatureProperty = new CPDFSignatureUI();
  635. // signatureProperty.SetFormProperty(args, PDFViewControl.PDFView);
  636. // PropertyContainer.Child = signatureProperty;
  637. // }
  638. // }
  639. //}
  640. //private void PDFView_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
  641. //{
  642. // OnAnnotEditHandler.Invoke(this, null);
  643. //}
  644. private void CommandBinding_Executed_Undo(object sender, ExecutedRoutedEventArgs e)
  645. {
  646. if (PDFViewControl != null && PDFViewControl.PDFViewTool.GetCPDFViewer() != null && CanUndo)
  647. {
  648. PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager?.Undo();
  649. }
  650. }
  651. private void CommandBinding_Executed_Redo(object sender, ExecutedRoutedEventArgs e)
  652. {
  653. if (PDFViewControl != null && PDFViewControl.PDFViewTool.GetCPDFViewer() != null && CanRedo)
  654. {
  655. PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager?.Redo();
  656. }
  657. }
  658. private void CommandBinding_Executed_Highlight(object sender, ExecutedRoutedEventArgs e)
  659. {
  660. AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Highlight);
  661. }
  662. private void CommandBinding_Executed_Underline(object sender, ExecutedRoutedEventArgs e)
  663. {
  664. AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Underline);
  665. }
  666. private void CommandBinding_Executed_Strikeout(object sender, ExecutedRoutedEventArgs e)
  667. {
  668. AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Strikeout);
  669. }
  670. private void CommandBinding_Executed_Squiggly(object sender, ExecutedRoutedEventArgs e)
  671. {
  672. AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Squiggly);
  673. }
  674. #endregion
  675. }
  676. }