ContentEditControl.xaml.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFPage;
  3. using ComPDFKit.PDFPage.Edit;
  4. using Compdfkit_Tools.Edit;
  5. using Compdfkit_Tools.Helper;
  6. using ComPDFKitViewer;
  7. using ComPDFKitViewer.PdfViewer;
  8. using Microsoft.Win32;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.ComponentModel;
  12. using System.Diagnostics;
  13. using System.Linq;
  14. using System.Runtime.CompilerServices;
  15. using System.Windows;
  16. using System.Windows.Controls;
  17. using System.Windows.Controls.Primitives;
  18. using System.Windows.Input;
  19. using System.Windows.Media;
  20. namespace Compdfkit_Tools.PDFControl
  21. {
  22. public partial class ContentEditControl : UserControl, INotifyPropertyChanged
  23. {
  24. #region Property
  25. public PDFViewControl PdfViewControl = new PDFViewControl();
  26. public PDFContentEditControl pdfContentEditControl = new PDFContentEditControl();
  27. private CPDFDisplaySettingsControl displaySettingsControl = null;
  28. private PDFEditEvent pdfTextCreateParam;
  29. private PDFEditEvent lastPDFEditEvent = null;
  30. private List<PDFEditEvent> lastPDFEditMultiEvents = null;
  31. private PanelState panelState = PanelState.GetInstance();
  32. private KeyEventHandler KeyDownHandler;
  33. public event PropertyChangedEventHandler PropertyChanged;
  34. public ICommand CloseTabCommand;
  35. public ICommand ExpandPropertyPanelCommand;
  36. public bool CanUndo
  37. {
  38. get
  39. {
  40. if (PdfViewControl != null && PdfViewControl.PDFView != null)
  41. {
  42. return PdfViewControl.PDFView.UndoManager.CanUndo;
  43. }
  44. return false;
  45. }
  46. }
  47. public bool CanRedo
  48. {
  49. get
  50. {
  51. if (PdfViewControl != null && PdfViewControl.PDFView != null)
  52. {
  53. return PdfViewControl.PDFView.UndoManager.CanRedo;
  54. }
  55. return false;
  56. }
  57. }
  58. private bool CanSave
  59. {
  60. get
  61. {
  62. if (PdfViewControl != null && PdfViewControl.PDFView != null)
  63. {
  64. return PdfViewControl.PDFView.UndoManager.CanSave;
  65. }
  66. return false;
  67. }
  68. }
  69. public event EventHandler<bool> OnCanSaveChanged;
  70. public event EventHandler OnAnnotEditHandler;
  71. #endregion
  72. public ContentEditControl()
  73. {
  74. InitializeComponent();
  75. panelState.PropertyChanged += PanelState_PropertyChanged;
  76. }
  77. private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
  78. {
  79. if (e.PropertyName == nameof(PanelState.IsLeftPanelExpand))
  80. {
  81. ExpandLeftPanel(panelState.IsLeftPanelExpand);
  82. }
  83. else if (e.PropertyName == nameof(PanelState.RightPanel))
  84. {
  85. if (panelState.RightPanel == PanelState.RightPanelState.PropertyPanel)
  86. {
  87. ExpandRightPropertyPanel(pdfContentEditControl, Visibility.Visible);
  88. }
  89. else if (panelState.RightPanel == PanelState.RightPanelState.ViewSettings)
  90. {
  91. ExpandRightPropertyPanel(displaySettingsControl, Visibility.Visible);
  92. }
  93. else
  94. {
  95. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  96. }
  97. }
  98. }
  99. public void ClearPDFEditState(ToggleButton ignoreBtn = null)
  100. {
  101. List<ToggleButton> clearBtnList = new List<ToggleButton>()
  102. {
  103. PDFTextEditButton,
  104. PDFImageEditButton
  105. };
  106. foreach (ToggleButton item in clearBtnList)
  107. {
  108. if (ignoreBtn == item)
  109. {
  110. continue;
  111. }
  112. item.IsChecked = false;
  113. }
  114. }
  115. private void PDFTextEditButton_Click(object sender, RoutedEventArgs e)
  116. {
  117. ToggleButton senderBtn = sender as ToggleButton;
  118. if (senderBtn != null && PdfViewControl != null)
  119. {
  120. ClearPDFEditState(senderBtn);
  121. if (senderBtn.IsChecked == true)
  122. {
  123. PDFEditEvent createParam = new PDFEditEvent();
  124. createParam.EditType = CPDFEditType.EditText;
  125. createParam.IsBold = false;
  126. createParam.IsItalic = false;
  127. createParam.FontSize = 14;
  128. createParam.FontName = "Helvetica";
  129. createParam.FontColor = Colors.Black;
  130. createParam.TextAlign = TextAlignType.AlignLeft;
  131. createParam.Transparency = 255;
  132. if (PdfViewControl.PDFView != null && PdfViewControl.PDFView.Document != null)
  133. {
  134. CPDFDocument pdfDoc = PdfViewControl.PDFView.Document;
  135. PdfViewControl.PDFView.ToolManager.EnableClickCreate = true;
  136. PdfViewControl.PDFView.ToolManager.ClickCreateWidth = 100;
  137. if (pdfDoc.PageCount > 0)
  138. {
  139. CPDFPage pdfPage = pdfDoc.PageAtIndex(0);
  140. CPDFEditPage editPage = pdfPage.GetEditPage();
  141. editPage.BeginEdit(CPDFEditType.EditText);
  142. createParam.SystemFontNameList.AddRange(editPage.GetFontList());
  143. editPage.EndEdit();
  144. }
  145. }
  146. PdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditText);
  147. PdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.EditText);
  148. PdfViewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit);
  149. PdfViewControl.PDFView?.ReloadDocument();
  150. PdfViewControl.PDFView?.SetPDFEditParam(createParam);
  151. pdfContentEditControl.SetPDFTextEditData(createParam);
  152. panelState.RightPanel = PanelState.RightPanelState.PropertyPanel;
  153. pdfTextCreateParam = createParam;
  154. }
  155. else
  156. {
  157. PdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.None);
  158. PdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditImage | CPDFEditType.EditText);
  159. PdfViewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit);
  160. PdfViewControl.PDFView?.ReloadDocument();
  161. pdfContentEditControl.ClearContentControl();
  162. panelState.RightPanel = PanelState.RightPanelState.None;
  163. }
  164. }
  165. }
  166. private void PDFImageEditButton_Click(object sender, RoutedEventArgs e)
  167. {
  168. ToggleButton senderBtn = sender as ToggleButton;
  169. if (senderBtn != null && PdfViewControl != null)
  170. {
  171. panelState.RightPanel = PanelState.RightPanelState.None;
  172. senderBtn.IsChecked = false;
  173. OpenFileDialog openFileDialog = new OpenFileDialog();
  174. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  175. if (openFileDialog.ShowDialog() == true)
  176. {
  177. ClearPDFEditState(senderBtn);
  178. PdfViewControl.PDFView?.ClearSelectPDFEdit();
  179. PdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditImage | CPDFEditType.EditText);
  180. PdfViewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit);
  181. PdfViewControl.PDFView?.ReloadDocument();
  182. PdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.EditImage);
  183. PdfViewControl.PDFView?.AddPDFEditImage(openFileDialog.FileName);
  184. }
  185. }
  186. }
  187. public void ExpandLeftPanel(bool isExpand)
  188. {
  189. BotaContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  190. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  191. if (isExpand)
  192. {
  193. BodyGrid.ColumnDefinitions[0].Width = new GridLength(320);
  194. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  195. }
  196. else
  197. {
  198. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  199. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  200. }
  201. }
  202. private void UndoBtn_Click(object sender, RoutedEventArgs e)
  203. {
  204. if (PdfViewControl != null && PdfViewControl.PDFView != null)
  205. {
  206. PdfViewControl.PDFView.UndoManager?.Undo();
  207. }
  208. }
  209. private void RedoBtn_Click(object sender, RoutedEventArgs e)
  210. {
  211. if (PdfViewControl != null && PdfViewControl.PDFView != null)
  212. {
  213. PdfViewControl.PDFView.UndoManager?.Redo();
  214. }
  215. }
  216. public void SetViewSettings(Visibility visibility, CPDFDisplaySettingsControl displaySettingsControl = null)
  217. {
  218. this.PropertyContainer.Child = displaySettingsControl;
  219. this.PropertyContainer.Visibility = visibility;
  220. }
  221. public void SetBOTAContainer(CPDFBOTABarControl botaControl)
  222. {
  223. this.BotaContainer.Child = botaControl;
  224. }
  225. public void SetDisplaySettingsControl(CPDFDisplaySettingsControl displaySettingsControl)
  226. {
  227. this.displaySettingsControl = displaySettingsControl;
  228. }
  229. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  230. {
  231. PdfViewControl.PDFView.PDFEditCommandHandler += PDFView_PDFEditCommandHandler;
  232. }
  233. private void UserControl_UnLoaded(object sender, RoutedEventArgs e)
  234. {
  235. PdfViewControl.PDFView.PDFEditCommandHandler -= PDFView_PDFEditCommandHandler;
  236. }
  237. public void ExpandRightPropertyPanel(UIElement propertytPanel, Visibility visible)
  238. {
  239. PropertyContainer.Width = 260;
  240. PropertyContainer.Child = propertytPanel;
  241. PropertyContainer.Visibility = visible;
  242. }
  243. public void InitWithPDFViewer(CPDFViewer pdfViewer)
  244. {
  245. PdfViewControl.PDFView = pdfViewer;
  246. PDFGrid.Child = PdfViewControl;
  247. FloatPageTool.InitWithPDFViewer(pdfViewer);
  248. pdfContentEditControl.InitWithPDFViewer(pdfViewer);
  249. PdfViewControl.PDFView.PDFEditActiveHandler -= PDFView_PDFEditActiveHandler;
  250. PdfViewControl.PDFView.PDFEditActiveHandler += PDFView_PDFEditActiveHandler;
  251. PdfViewControl.PDFView.UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  252. PdfViewControl.PDFView.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  253. DataContext = this;
  254. if(pdfViewer!=null)
  255. {
  256. pdfViewer.EnableMultiSelectEdit = true;
  257. pdfViewer.PDFEditMultiActiveHandler -= PDFEditMultiActiveHandler;
  258. pdfViewer.PDFEditMultiActiveHandler += PDFEditMultiActiveHandler;
  259. if (KeyDownHandler != null)
  260. {
  261. pdfViewer.RemoveHandler(KeyDownEvent, KeyDownHandler);
  262. }
  263. KeyDownHandler = new KeyEventHandler(PDFView_KeyDown);
  264. pdfViewer.AddHandler(KeyDownEvent, KeyDownHandler, false, true);
  265. }
  266. }
  267. private void PDFEditEmptyPanel()
  268. {
  269. PropertyContainer.Child = pdfContentEditControl;
  270. if (pdfTextCreateParam != null && PdfViewControl != null && PdfViewControl.PDFView != null)
  271. {
  272. if (PdfViewControl.PDFView.GetPDFEditCreateType() == CPDFEditType.EditText)
  273. {
  274. pdfContentEditControl.SetPDFTextEditData(pdfTextCreateParam);
  275. }
  276. else if (PdfViewControl.PDFView.GetPDFEditCreateType() == CPDFEditType.None)
  277. {
  278. pdfContentEditControl.ClearContentControl();
  279. }
  280. }
  281. else
  282. {
  283. pdfContentEditControl.ClearContentControl();
  284. }
  285. }
  286. private void PDFEditMultiActiveHandler(object sender, List<PDFEditEvent> e)
  287. {
  288. lastPDFEditEvent = null;
  289. lastPDFEditMultiEvents = e;
  290. if(e==null)
  291. {
  292. PDFEditEmptyPanel();
  293. return;
  294. }
  295. if(e.Count>1)
  296. {
  297. List<CPDFEditType> editList= e.AsEnumerable().Select(x=>x.EditType).Distinct().ToList();
  298. if(editList.Count>1)
  299. {
  300. PDFEditEmptyPanel();
  301. return;
  302. }
  303. if (editList[0]==CPDFEditType.EditText)
  304. {
  305. pdfContentEditControl.SetPDFTextMultiEditData(e);
  306. return;
  307. }
  308. if (editList[0]==CPDFEditType.EditImage)
  309. {
  310. UIElement pageView = sender as UIElement;
  311. if (pageView != null)
  312. {
  313. pageView.MouseLeftButtonUp += PageView_MouseLeftButtonUp;
  314. }
  315. pdfContentEditControl.SetPDFImageMultiEditData(e);
  316. }
  317. }
  318. }
  319. public void PDFView_KeyDown(object sender, KeyEventArgs e)
  320. {
  321. if (PdfViewControl.PDFView.MouseMode != MouseModes.PDFEdit)
  322. {
  323. return;
  324. }
  325. if (Keyboard.Modifiers == ModifierKeys.Control)
  326. {
  327. if (e.Key == Key.Left)
  328. {
  329. PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypePreWord, false);
  330. e.Handled = true;
  331. }
  332. if (e.Key == Key.Right)
  333. {
  334. PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeNextWord, false);
  335. e.Handled = true;
  336. }
  337. if (e.Key == Key.Up)
  338. {
  339. PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeSectionBegin, false);
  340. e.Handled = true;
  341. }
  342. if (e.Key == Key.Down)
  343. {
  344. PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeSectionEnd, false);
  345. e.Handled = true;
  346. }
  347. }
  348. if (Keyboard.Modifiers == ModifierKeys.Shift)
  349. {
  350. if (e.Key == Key.Left)
  351. {
  352. PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypePreCharPlace, true);
  353. e.Handled = true;
  354. }
  355. if (e.Key == Key.Right)
  356. {
  357. PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeNextCharPlace, true);
  358. e.Handled = true;
  359. }
  360. if (e.Key == Key.Up)
  361. {
  362. PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeUpCharPlace, true);
  363. e.Handled = true;
  364. }
  365. if (e.Key == Key.Down)
  366. {
  367. PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeDownCharPlace, true);
  368. e.Handled = true;
  369. }
  370. }
  371. if (Keyboard.Modifiers == ModifierKeys.Alt)
  372. {
  373. if (e.SystemKey == Key.Up)
  374. {
  375. PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLocationLineBegin, false);
  376. e.Handled = true;
  377. }
  378. if (e.SystemKey == Key.Down)
  379. {
  380. PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeLineEnd, false);
  381. e.Handled = true;
  382. }
  383. }
  384. if (Keyboard.Modifiers == ModifierKeys.None)
  385. {
  386. if (e.Key == Key.Left)
  387. {
  388. PdfViewControl.PDFView.MoveEditArea(new Point(-5, 0));
  389. e.Handled = true;
  390. }
  391. if (e.Key == Key.Right)
  392. {
  393. PdfViewControl.PDFView.MoveEditArea(new Point(5, 0));
  394. e.Handled = true;
  395. }
  396. if (e.Key == Key.Up)
  397. {
  398. PdfViewControl.PDFView.MoveEditArea(new Point(0, -5));
  399. e.Handled = true;
  400. }
  401. if (e.Key == Key.Down)
  402. {
  403. PdfViewControl.PDFView.MoveEditArea(new Point(0, 5));
  404. e.Handled = true;
  405. }
  406. }
  407. if (Keyboard.Modifiers == (ModifierKeys.Control | ModifierKeys.Shift))
  408. {
  409. if (e.Key == Key.Left)
  410. {
  411. PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypePreWord, true);
  412. e.Handled = true;
  413. }
  414. if (e.Key == Key.Right)
  415. {
  416. PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeNextWord, true);
  417. e.Handled = true;
  418. }
  419. if (e.Key == Key.Up)
  420. {
  421. PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeSectionBegin, true);
  422. e.Handled = true;
  423. }
  424. if (e.Key == Key.Down)
  425. {
  426. PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeSectionEnd, true);
  427. e.Handled = true;
  428. }
  429. }
  430. }
  431. /// <summary>
  432. /// Text and Image Selected Event
  433. /// </summary>
  434. private void PDFView_PDFEditActiveHandler(object sender, PDFEditEvent e)
  435. {
  436. lastPDFEditEvent = e;
  437. lastPDFEditMultiEvents = null;
  438. if (e == null)
  439. {
  440. PropertyContainer.Child = pdfContentEditControl;
  441. if (pdfTextCreateParam != null && PdfViewControl != null && PdfViewControl.PDFView != null)
  442. {
  443. if (PdfViewControl.PDFView.GetPDFEditCreateType() == CPDFEditType.EditText)
  444. {
  445. pdfContentEditControl.SetPDFTextEditData(pdfTextCreateParam);
  446. }
  447. else if (PdfViewControl.PDFView.GetPDFEditCreateType() == CPDFEditType.None)
  448. {
  449. pdfContentEditControl.ClearContentControl();
  450. }
  451. }
  452. else
  453. {
  454. pdfContentEditControl.ClearContentControl();
  455. }
  456. return;
  457. }
  458. if (e.EditType == CPDFEditType.EditText)
  459. {
  460. pdfContentEditControl.SetPDFTextEditData(e, true);
  461. return;
  462. }
  463. if (e.EditType == CPDFEditType.EditImage && PdfViewControl != null)
  464. {
  465. UIElement pageView = sender as UIElement;
  466. if (pageView != null)
  467. {
  468. pageView.MouseLeftButtonUp += PageView_MouseLeftButtonUp;
  469. }
  470. pdfContentEditControl.SetPDFImageEditData(e);
  471. return;
  472. }
  473. }
  474. private void PageView_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  475. {
  476. UIElement pageView = sender as UIElement;
  477. if (pageView != null)
  478. {
  479. pageView.MouseLeftButtonUp -= PageView_MouseLeftButtonUp;
  480. }
  481. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  482. {
  483. pdfContentEditControl.SetPDFImageEditData(lastPDFEditEvent);
  484. }
  485. }
  486. public void ClearViewerControl()
  487. {
  488. PDFGrid.Child = null;
  489. BotaContainer.Child = null;
  490. PropertyContainer.Child = null;
  491. displaySettingsControl = null;
  492. }
  493. #region Property changed
  494. protected void OnPropertyChanged([CallerMemberName] string name = null)
  495. {
  496. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  497. }
  498. public void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  499. {
  500. OnPropertyChanged(e.PropertyName);
  501. if (e.PropertyName == "CanSave")
  502. {
  503. OnCanSaveChanged?.Invoke(this, CanSave);
  504. }
  505. }
  506. #endregion
  507. #region Context menu
  508. private void PDFView_PDFEditCommandHandler(object sender, PDFEditCommand e)
  509. {
  510. if (e == null)
  511. {
  512. return;
  513. }
  514. if (e.EditType == CPDFEditType.EditText)
  515. {
  516. e.Handle = true;
  517. PDFEditTextContextMenu(sender, e);
  518. }
  519. if (e.EditType == CPDFEditType.EditImage)
  520. {
  521. e.Handle = true;
  522. PDFEditImageContextMenu(sender, e);
  523. }
  524. if(e.EditType== (CPDFEditType.EditText | CPDFEditType.EditImage))
  525. {
  526. e.Handle |= true;
  527. PDFEditMultiContextMenu(sender, e);
  528. }
  529. }
  530. private void PDFEditTextContextMenu(object sender, PDFEditCommand editCommand)
  531. {
  532. editCommand.PopupMenu = new ContextMenu();
  533. if (lastPDFEditEvent != null || lastPDFEditMultiEvents!=null)
  534. {
  535. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  536. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  537. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  538. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  539. }
  540. else
  541. {
  542. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  543. if (editCommand.TextAreaCopied)
  544. {
  545. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste And Match Style", Command = CustomCommands.PasteMatchStyle, CommandTarget = (UIElement)sender });
  546. }
  547. }
  548. }
  549. private void PDFEditImageContextMenu(object sender, PDFEditCommand editCommand)
  550. {
  551. editCommand.PopupMenu = new ContextMenu();
  552. if (lastPDFEditEvent != null)
  553. {
  554. MenuItem rotateLeftMenu = new MenuItem();
  555. rotateLeftMenu.Header = "Left Rotate";
  556. rotateLeftMenu.Click += (o, p) =>
  557. {
  558. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  559. {
  560. lastPDFEditEvent.Rotate = -90;
  561. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  562. pdfContentEditControl.RefreshThumb();
  563. }
  564. };
  565. editCommand.PopupMenu.Items.Add(rotateLeftMenu);
  566. MenuItem rotateRightMenu = new MenuItem();
  567. rotateRightMenu.Header = "Right Rotate";
  568. rotateRightMenu.Click += (o, p) =>
  569. {
  570. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  571. {
  572. lastPDFEditEvent.Rotate = 90;
  573. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  574. pdfContentEditControl.RefreshThumb();
  575. }
  576. };
  577. editCommand.PopupMenu.Items.Add(rotateRightMenu);
  578. MenuItem replaceMenu = new MenuItem();
  579. replaceMenu.Header = "Replace";
  580. replaceMenu.Click += (o, p) =>
  581. {
  582. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  583. {
  584. OpenFileDialog openFileDialog = new OpenFileDialog();
  585. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  586. if (openFileDialog.ShowDialog() == true)
  587. {
  588. lastPDFEditEvent.ReplaceImagePath = openFileDialog.FileName;
  589. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  590. PdfViewControl.PDFView?.ClearSelectPDFEdit();
  591. pdfContentEditControl.RefreshThumb();
  592. }
  593. }
  594. };
  595. editCommand.PopupMenu.Items.Add(replaceMenu);
  596. MenuItem exportMenu = new MenuItem();
  597. exportMenu.Header = "Export";
  598. exportMenu.Click += (o, p) =>
  599. {
  600. if (PdfViewControl != null && PdfViewControl.PDFView != null)
  601. {
  602. Dictionary<int, List<System.Drawing.Bitmap>> imageDict = PdfViewControl.PDFView.GetSelectedImages();
  603. if (imageDict != null && imageDict.Count > 0)
  604. {
  605. System.Windows.Forms.FolderBrowserDialog folderBrowser = new System.Windows.Forms.FolderBrowserDialog();
  606. if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  607. {
  608. string choosePath = folderBrowser.SelectedPath;
  609. string openPath = choosePath;
  610. foreach (int pageIndex in imageDict.Keys)
  611. {
  612. List<System.Drawing.Bitmap> imageList = imageDict[pageIndex];
  613. foreach (System.Drawing.Bitmap image in imageList)
  614. {
  615. string savePath = System.IO.Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
  616. image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  617. openPath = savePath;
  618. }
  619. }
  620. Process.Start("explorer", "/select,\"" + openPath + "\"");
  621. }
  622. }
  623. }
  624. };
  625. editCommand.PopupMenu.Items.Add(exportMenu);
  626. MenuItem opacityMenu = new MenuItem();
  627. opacityMenu.Header = "Opacity";
  628. editCommand.PopupMenu.Items.Add(opacityMenu);
  629. AppendOpacityMenu(opacityMenu);
  630. MenuItem horizonMirror = new MenuItem();
  631. horizonMirror.Header = "HMirror";
  632. horizonMirror.Click += (o, p) =>
  633. {
  634. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  635. {
  636. lastPDFEditEvent.HorizontalMirror = true;
  637. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  638. }
  639. };
  640. editCommand.PopupMenu.Items.Add(horizonMirror);
  641. MenuItem verticalMirror = new MenuItem();
  642. verticalMirror.Header = "VMirror";
  643. verticalMirror.Click += (o, p) =>
  644. {
  645. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  646. {
  647. lastPDFEditEvent.VerticalMirror = true;
  648. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  649. }
  650. };
  651. editCommand.PopupMenu.Items.Add(verticalMirror);
  652. MenuItem cropMenu = new MenuItem();
  653. cropMenu.Header = "Crop";
  654. cropMenu.Click += (o, p) =>
  655. {
  656. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  657. {
  658. lastPDFEditEvent.ClipImage = true;
  659. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  660. }
  661. };
  662. editCommand.PopupMenu.Items.Add(cropMenu);
  663. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  664. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  665. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  666. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  667. if (editCommand.TextAreaCopied)
  668. {
  669. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste And Match Style", Command = CustomCommands.PasteMatchStyle, CommandTarget = (UIElement)sender });
  670. }
  671. }
  672. else
  673. {
  674. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  675. }
  676. }
  677. private void PDFEditMultiContextMenu(object sender, PDFEditCommand editCommand)
  678. {
  679. if(editCommand!=null)
  680. {
  681. editCommand.PopupMenu = new ContextMenu();
  682. if (lastPDFEditMultiEvents!=null)
  683. {
  684. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  685. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  686. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  687. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  688. if(CustomCommands.PasteMatchStyle.CanExecute(null, (UIElement)sender))
  689. {
  690. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste and Match Style", Command = CustomCommands.PasteMatchStyle, CommandTarget = (UIElement)sender });
  691. }
  692. }
  693. else
  694. {
  695. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  696. if (CustomCommands.PasteMatchStyle.CanExecute(null, (UIElement)sender))
  697. {
  698. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste and Match Style", Command = CustomCommands.PasteMatchStyle, CommandTarget = (UIElement)sender });
  699. }
  700. }
  701. }
  702. }
  703. private void AppendOpacityMenu(MenuItem parentMenu)
  704. {
  705. List<int> opacityList = new List<int>()
  706. {
  707. 25,50,75,100
  708. };
  709. foreach (int opacity in opacityList)
  710. {
  711. MenuItem opacityMenu = new MenuItem();
  712. opacityMenu.Header = string.Format("{0}%", opacity);
  713. opacityMenu.Click += (o, p) =>
  714. {
  715. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  716. {
  717. lastPDFEditEvent.Transparency = (int)Math.Ceiling(opacity * 255 / 100D);
  718. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  719. }
  720. };
  721. parentMenu.Items.Add(opacityMenu);
  722. }
  723. }
  724. #endregion
  725. private void CommandBinding_Executed_Undo(object sender, ExecutedRoutedEventArgs e)
  726. {
  727. if (PdfViewControl != null && PdfViewControl.PDFView != null && CanUndo)
  728. {
  729. PdfViewControl.PDFView.UndoManager?.Undo();
  730. }
  731. }
  732. private void CommandBinding_Executed_Redo(object sender, ExecutedRoutedEventArgs e)
  733. {
  734. if (PdfViewControl != null && PdfViewControl.PDFView != null && CanRedo)
  735. {
  736. PdfViewControl.PDFView.UndoManager?.Redo();
  737. }
  738. }
  739. }
  740. }