AnnotationControl.xaml.cs 37 KB

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