ContentEditControl.xaml.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  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. if(PdfViewControl.PDFView.Document.GetPermissionsInfo().AllowsCopying)
  536. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Copy"), Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  537. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Cut"), Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  538. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Delete"), Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  539. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Paste"), Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  540. }
  541. else
  542. {
  543. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Paste"), Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  544. if (editCommand.TextAreaCopied)
  545. {
  546. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_MatchPaste"), Command = CustomCommands.PasteMatchStyle, CommandTarget = (UIElement)sender });
  547. }
  548. }
  549. }
  550. private void PDFEditImageContextMenu(object sender, PDFEditCommand editCommand)
  551. {
  552. editCommand.PopupMenu = new ContextMenu();
  553. if (lastPDFEditEvent != null)
  554. {
  555. MenuItem rotateLeftMenu = new MenuItem();
  556. rotateLeftMenu.Header = LanguageHelper.CommonManager.GetString("Menu_RotateLeft");
  557. rotateLeftMenu.Click += (o, p) =>
  558. {
  559. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  560. {
  561. lastPDFEditEvent.Rotate = -90;
  562. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  563. pdfContentEditControl.RefreshThumb();
  564. }
  565. };
  566. editCommand.PopupMenu.Items.Add(rotateLeftMenu);
  567. MenuItem rotateRightMenu = new MenuItem();
  568. rotateRightMenu.Header = LanguageHelper.CommonManager.GetString("Menu_RotateRight");
  569. rotateRightMenu.Click += (o, p) =>
  570. {
  571. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  572. {
  573. lastPDFEditEvent.Rotate = 90;
  574. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  575. pdfContentEditControl.RefreshThumb();
  576. }
  577. };
  578. editCommand.PopupMenu.Items.Add(rotateRightMenu);
  579. MenuItem replaceMenu = new MenuItem();
  580. replaceMenu.Header = LanguageHelper.CommonManager.GetString("Menu_Replace");
  581. replaceMenu.Click += (o, p) =>
  582. {
  583. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  584. {
  585. OpenFileDialog openFileDialog = new OpenFileDialog();
  586. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  587. if (openFileDialog.ShowDialog() == true)
  588. {
  589. lastPDFEditEvent.ReplaceImagePath = openFileDialog.FileName;
  590. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  591. PdfViewControl.PDFView?.ClearSelectPDFEdit();
  592. pdfContentEditControl.RefreshThumb();
  593. }
  594. }
  595. };
  596. editCommand.PopupMenu.Items.Add(replaceMenu);
  597. MenuItem exportMenu = new MenuItem();
  598. exportMenu.Header = LanguageHelper.CommonManager.GetString("Menu_Export");
  599. exportMenu.Click += (o, p) =>
  600. {
  601. if (PdfViewControl != null && PdfViewControl.PDFView != null)
  602. {
  603. Dictionary<int, List<System.Drawing.Bitmap>> imageDict = PdfViewControl.PDFView.GetSelectedImages();
  604. if (imageDict != null && imageDict.Count > 0)
  605. {
  606. System.Windows.Forms.FolderBrowserDialog folderBrowser = new System.Windows.Forms.FolderBrowserDialog();
  607. if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  608. {
  609. string choosePath = folderBrowser.SelectedPath;
  610. string openPath = choosePath;
  611. foreach (int pageIndex in imageDict.Keys)
  612. {
  613. List<System.Drawing.Bitmap> imageList = imageDict[pageIndex];
  614. foreach (System.Drawing.Bitmap image in imageList)
  615. {
  616. string savePath = System.IO.Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
  617. image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  618. openPath = savePath;
  619. }
  620. }
  621. Process.Start("explorer", "/select,\"" + openPath + "\"");
  622. }
  623. }
  624. }
  625. };
  626. editCommand.PopupMenu.Items.Add(exportMenu);
  627. MenuItem opacityMenu = new MenuItem();
  628. opacityMenu.Header = LanguageHelper.CommonManager.GetString("Menu_Opacity");
  629. editCommand.PopupMenu.Items.Add(opacityMenu);
  630. AppendOpacityMenu(opacityMenu);
  631. MenuItem horizonMirror = new MenuItem();
  632. horizonMirror.Header = LanguageHelper.CommonManager.GetString("Menu_HFlip");
  633. horizonMirror.Click += (o, p) =>
  634. {
  635. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  636. {
  637. lastPDFEditEvent.HorizontalMirror = true;
  638. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  639. }
  640. };
  641. editCommand.PopupMenu.Items.Add(horizonMirror);
  642. MenuItem verticalMirror = new MenuItem();
  643. verticalMirror.Header = LanguageHelper.CommonManager.GetString("Menu_VFlip");
  644. verticalMirror.Click += (o, p) =>
  645. {
  646. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  647. {
  648. lastPDFEditEvent.VerticalMirror = true;
  649. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  650. }
  651. };
  652. editCommand.PopupMenu.Items.Add(verticalMirror);
  653. MenuItem cropMenu = new MenuItem();
  654. cropMenu.Header = LanguageHelper.CommonManager.GetString("Menu_Crop");
  655. cropMenu.Click += (o, p) =>
  656. {
  657. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  658. {
  659. lastPDFEditEvent.ClipImage = true;
  660. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  661. }
  662. };
  663. editCommand.PopupMenu.Items.Add(cropMenu);
  664. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Copy"), Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  665. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Cut"), Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  666. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Delete"), Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  667. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Paste"), Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  668. if (editCommand.TextAreaCopied)
  669. {
  670. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_MatchPaste"), Command = CustomCommands.PasteMatchStyle, CommandTarget = (UIElement)sender });
  671. }
  672. }
  673. else
  674. {
  675. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Paste"), Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  676. }
  677. }
  678. private void PDFEditMultiContextMenu(object sender, PDFEditCommand editCommand)
  679. {
  680. if(editCommand!=null)
  681. {
  682. editCommand.PopupMenu = new ContextMenu();
  683. if (lastPDFEditMultiEvents!=null)
  684. {
  685. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Copy"), Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  686. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Cut"), Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  687. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Delete"), Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  688. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Paste"), Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  689. if(CustomCommands.PasteMatchStyle.CanExecute(null, (UIElement)sender))
  690. {
  691. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_MatchPaste"), Command = CustomCommands.PasteMatchStyle, CommandTarget = (UIElement)sender });
  692. }
  693. }
  694. else
  695. {
  696. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Paste"), Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  697. if (CustomCommands.PasteMatchStyle.CanExecute(null, (UIElement)sender))
  698. {
  699. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_MatchPaste"), Command = CustomCommands.PasteMatchStyle, CommandTarget = (UIElement)sender });
  700. }
  701. }
  702. }
  703. }
  704. private void AppendOpacityMenu(MenuItem parentMenu)
  705. {
  706. List<int> opacityList = new List<int>()
  707. {
  708. 25,50,75,100
  709. };
  710. foreach (int opacity in opacityList)
  711. {
  712. MenuItem opacityMenu = new MenuItem();
  713. opacityMenu.Header = string.Format("{0}%", opacity);
  714. opacityMenu.Click += (o, p) =>
  715. {
  716. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  717. {
  718. lastPDFEditEvent.Transparency = (int)Math.Ceiling(opacity * 255 / 100D);
  719. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  720. }
  721. };
  722. parentMenu.Items.Add(opacityMenu);
  723. }
  724. }
  725. #endregion
  726. private void CommandBinding_Executed_Undo(object sender, ExecutedRoutedEventArgs e)
  727. {
  728. if (PdfViewControl != null && PdfViewControl.PDFView != null && CanUndo)
  729. {
  730. PdfViewControl.PDFView.UndoManager?.Undo();
  731. }
  732. }
  733. private void CommandBinding_Executed_Redo(object sender, ExecutedRoutedEventArgs e)
  734. {
  735. if (PdfViewControl != null && PdfViewControl.PDFView != null && CanRedo)
  736. {
  737. PdfViewControl.PDFView.UndoManager?.Redo();
  738. }
  739. }
  740. }
  741. }