AnnotationControl.xaml.cs 33 KB

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