AnnotationControl.xaml.cs 33 KB

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