AnnotationControl.xaml.cs 33 KB

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