AnnotationControl.xaml.cs 40 KB

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