ContentEditControl.xaml.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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. botaControl.ReplaceFunctionEnabled = true;
  225. }
  226. public void SetDisplaySettingsControl(CPDFDisplaySettingsControl displaySettingsControl)
  227. {
  228. this.displaySettingsControl = displaySettingsControl;
  229. }
  230. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  231. {
  232. PdfViewControl.PDFView.PDFEditCommandHandler += PDFView_PDFEditCommandHandler;
  233. }
  234. private void UserControl_UnLoaded(object sender, RoutedEventArgs e)
  235. {
  236. PdfViewControl.PDFView.PDFEditCommandHandler -= PDFView_PDFEditCommandHandler;
  237. }
  238. public void ExpandRightPropertyPanel(UIElement propertytPanel, Visibility visible)
  239. {
  240. PropertyContainer.Width = 260;
  241. PropertyContainer.Child = propertytPanel;
  242. PropertyContainer.Visibility = visible;
  243. }
  244. public void InitWithPDFViewer(CPDFViewer pdfViewer)
  245. {
  246. PdfViewControl.PDFView = pdfViewer;
  247. PDFGrid.Child = PdfViewControl;
  248. FloatPageTool.InitWithPDFViewer(pdfViewer);
  249. pdfContentEditControl.InitWithPDFViewer(pdfViewer);
  250. PdfViewControl.PDFView.PDFEditActiveHandler -= PDFView_PDFEditActiveHandler;
  251. PdfViewControl.PDFView.PDFEditActiveHandler += PDFView_PDFEditActiveHandler;
  252. PdfViewControl.PDFView.UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  253. PdfViewControl.PDFView.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  254. DataContext = this;
  255. if(pdfViewer!=null)
  256. {
  257. pdfViewer.EnableMultiSelectEdit = true;
  258. pdfViewer.PDFEditMultiActiveHandler -= PDFEditMultiActiveHandler;
  259. pdfViewer.PDFEditMultiActiveHandler += PDFEditMultiActiveHandler;
  260. if (KeyDownHandler != null)
  261. {
  262. pdfViewer.RemoveHandler(KeyDownEvent, KeyDownHandler);
  263. }
  264. KeyDownHandler = new KeyEventHandler(PDFView_KeyDown);
  265. pdfViewer.AddHandler(KeyDownEvent, KeyDownHandler, false, true);
  266. }
  267. }
  268. private void PDFEditEmptyPanel()
  269. {
  270. PropertyContainer.Child = pdfContentEditControl;
  271. if (pdfTextCreateParam != null && PdfViewControl != null && PdfViewControl.PDFView != null)
  272. {
  273. if (PdfViewControl.PDFView.GetPDFEditCreateType() == CPDFEditType.EditText)
  274. {
  275. pdfContentEditControl.SetPDFTextEditData(pdfTextCreateParam);
  276. }
  277. else if (PdfViewControl.PDFView.GetPDFEditCreateType() == CPDFEditType.None)
  278. {
  279. pdfContentEditControl.ClearContentControl();
  280. }
  281. }
  282. else
  283. {
  284. pdfContentEditControl.ClearContentControl();
  285. }
  286. }
  287. private void PDFEditMultiActiveHandler(object sender, List<PDFEditEvent> e)
  288. {
  289. lastPDFEditEvent = null;
  290. lastPDFEditMultiEvents = e;
  291. if(e==null)
  292. {
  293. PDFEditEmptyPanel();
  294. return;
  295. }
  296. if(e.Count>1)
  297. {
  298. List<CPDFEditType> editList= e.AsEnumerable().Select(x=>x.EditType).Distinct().ToList();
  299. if(editList.Count>1)
  300. {
  301. PDFEditEmptyPanel();
  302. return;
  303. }
  304. if (editList[0]==CPDFEditType.EditText)
  305. {
  306. pdfContentEditControl.SetPDFTextMultiEditData(e);
  307. return;
  308. }
  309. if (editList[0]==CPDFEditType.EditImage)
  310. {
  311. UIElement pageView = sender as UIElement;
  312. if (pageView != null)
  313. {
  314. pageView.MouseLeftButtonUp += PageView_MouseLeftButtonUp;
  315. }
  316. pdfContentEditControl.SetPDFImageMultiEditData(e);
  317. }
  318. }
  319. }
  320. public void PDFView_KeyDown(object sender, KeyEventArgs e)
  321. {
  322. if (PdfViewControl.PDFView.MouseMode != MouseModes.PDFEdit)
  323. {
  324. return;
  325. }
  326. if (Keyboard.Modifiers == ModifierKeys.Control)
  327. {
  328. if (e.Key == Key.Left)
  329. {
  330. PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypePreWord, false);
  331. e.Handled = true;
  332. }
  333. if (e.Key == Key.Right)
  334. {
  335. PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeNextWord, false);
  336. e.Handled = true;
  337. }
  338. if (e.Key == Key.Up)
  339. {
  340. PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeSectionBegin, false);
  341. e.Handled = true;
  342. }
  343. if (e.Key == Key.Down)
  344. {
  345. PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeSectionEnd, false);
  346. e.Handled = true;
  347. }
  348. }
  349. if (Keyboard.Modifiers == ModifierKeys.Shift)
  350. {
  351. if (e.Key == Key.Left)
  352. {
  353. PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypePreCharPlace, true);
  354. e.Handled = true;
  355. }
  356. if (e.Key == Key.Right)
  357. {
  358. PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeNextCharPlace, true);
  359. e.Handled = true;
  360. }
  361. if (e.Key == Key.Up)
  362. {
  363. PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeUpCharPlace, true);
  364. e.Handled = true;
  365. }
  366. if (e.Key == Key.Down)
  367. {
  368. PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeDownCharPlace, true);
  369. e.Handled = true;
  370. }
  371. }
  372. if (Keyboard.Modifiers == ModifierKeys.Alt)
  373. {
  374. if (e.SystemKey == Key.Up)
  375. {
  376. PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLocationLineBegin, false);
  377. e.Handled = true;
  378. }
  379. if (e.SystemKey == Key.Down)
  380. {
  381. PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeLineEnd, false);
  382. e.Handled = true;
  383. }
  384. }
  385. if (Keyboard.Modifiers == ModifierKeys.None)
  386. {
  387. if (e.Key == Key.Left)
  388. {
  389. PdfViewControl.PDFView.MoveEditArea(new Point(-5, 0));
  390. e.Handled = true;
  391. }
  392. if (e.Key == Key.Right)
  393. {
  394. PdfViewControl.PDFView.MoveEditArea(new Point(5, 0));
  395. e.Handled = true;
  396. }
  397. if (e.Key == Key.Up)
  398. {
  399. PdfViewControl.PDFView.MoveEditArea(new Point(0, -5));
  400. e.Handled = true;
  401. }
  402. if (e.Key == Key.Down)
  403. {
  404. PdfViewControl.PDFView.MoveEditArea(new Point(0, 5));
  405. e.Handled = true;
  406. }
  407. }
  408. if (Keyboard.Modifiers == (ModifierKeys.Control | ModifierKeys.Shift))
  409. {
  410. if (e.Key == Key.Left)
  411. {
  412. PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypePreWord, true);
  413. e.Handled = true;
  414. }
  415. if (e.Key == Key.Right)
  416. {
  417. PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeNextWord, true);
  418. e.Handled = true;
  419. }
  420. if (e.Key == Key.Up)
  421. {
  422. PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeSectionBegin, true);
  423. e.Handled = true;
  424. }
  425. if (e.Key == Key.Down)
  426. {
  427. PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeSectionEnd, true);
  428. e.Handled = true;
  429. }
  430. }
  431. }
  432. /// <summary>
  433. /// Text and Image Selected Event
  434. /// </summary>
  435. private void PDFView_PDFEditActiveHandler(object sender, PDFEditEvent e)
  436. {
  437. lastPDFEditEvent = e;
  438. lastPDFEditMultiEvents = null;
  439. if (e == null)
  440. {
  441. PropertyContainer.Child = pdfContentEditControl;
  442. if (pdfTextCreateParam != null && PdfViewControl != null && PdfViewControl.PDFView != null)
  443. {
  444. if (PdfViewControl.PDFView.GetPDFEditCreateType() == CPDFEditType.EditText)
  445. {
  446. pdfContentEditControl.SetPDFTextEditData(pdfTextCreateParam);
  447. }
  448. else if (PdfViewControl.PDFView.GetPDFEditCreateType() == CPDFEditType.None)
  449. {
  450. pdfContentEditControl.ClearContentControl();
  451. }
  452. }
  453. else
  454. {
  455. pdfContentEditControl.ClearContentControl();
  456. }
  457. return;
  458. }
  459. if (e.EditType == CPDFEditType.EditText)
  460. {
  461. pdfContentEditControl.SetPDFTextEditData(e, true);
  462. return;
  463. }
  464. if (e.EditType == CPDFEditType.EditImage && PdfViewControl != null)
  465. {
  466. UIElement pageView = sender as UIElement;
  467. if (pageView != null)
  468. {
  469. pageView.MouseLeftButtonUp += PageView_MouseLeftButtonUp;
  470. }
  471. pdfContentEditControl.SetPDFImageEditData(e);
  472. return;
  473. }
  474. }
  475. private void PageView_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  476. {
  477. UIElement pageView = sender as UIElement;
  478. if (pageView != null)
  479. {
  480. pageView.MouseLeftButtonUp -= PageView_MouseLeftButtonUp;
  481. }
  482. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  483. {
  484. pdfContentEditControl.SetPDFImageEditData(lastPDFEditEvent);
  485. }
  486. }
  487. public void ClearViewerControl()
  488. {
  489. if (BotaContainer.Child is CPDFBOTABarControl botaControl)
  490. {
  491. botaControl.ReplaceFunctionEnabled = false;
  492. }
  493. PDFGrid.Child = null;
  494. BotaContainer.Child = null;
  495. PropertyContainer.Child = null;
  496. displaySettingsControl = null;
  497. }
  498. #region Property changed
  499. protected void OnPropertyChanged([CallerMemberName] string name = null)
  500. {
  501. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  502. }
  503. public void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  504. {
  505. OnPropertyChanged(e.PropertyName);
  506. if (e.PropertyName == "CanSave")
  507. {
  508. OnCanSaveChanged?.Invoke(this, CanSave);
  509. }
  510. }
  511. #endregion
  512. #region Context menu
  513. private void PDFView_PDFEditCommandHandler(object sender, PDFEditCommand e)
  514. {
  515. if (e == null)
  516. {
  517. return;
  518. }
  519. if (e.EditType == CPDFEditType.EditText)
  520. {
  521. e.Handle = true;
  522. PDFEditTextContextMenu(sender, e);
  523. }
  524. if (e.EditType == CPDFEditType.EditImage)
  525. {
  526. e.Handle = true;
  527. PDFEditImageContextMenu(sender, e);
  528. }
  529. if(e.EditType== (CPDFEditType.EditText | CPDFEditType.EditImage))
  530. {
  531. e.Handle |= true;
  532. PDFEditMultiContextMenu(sender, e);
  533. }
  534. }
  535. private void PDFEditTextContextMenu(object sender, PDFEditCommand editCommand)
  536. {
  537. editCommand.PopupMenu = new ContextMenu();
  538. if (lastPDFEditEvent != null || lastPDFEditMultiEvents!=null)
  539. {
  540. if(PdfViewControl.PDFView.Document.GetPermissionsInfo().AllowsCopying)
  541. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Copy"), Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  542. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Cut"), Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  543. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Delete"), Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  544. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Paste"), Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  545. }
  546. else
  547. {
  548. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Paste"), Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  549. if (editCommand.TextAreaCopied)
  550. {
  551. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_MatchPaste"), Command = CustomCommands.PasteWithoutStyle, CommandTarget = (UIElement)sender });
  552. }
  553. }
  554. }
  555. private void PDFEditImageContextMenu(object sender, PDFEditCommand editCommand)
  556. {
  557. editCommand.PopupMenu = new ContextMenu();
  558. if (lastPDFEditEvent != null)
  559. {
  560. MenuItem rotateLeftMenu = new MenuItem();
  561. rotateLeftMenu.Header = LanguageHelper.CommonManager.GetString("Menu_RotateLeft");
  562. rotateLeftMenu.Click += (o, p) =>
  563. {
  564. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  565. {
  566. lastPDFEditEvent.Rotate = -90;
  567. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  568. pdfContentEditControl.SetRotationText(lastPDFEditEvent.CurrentRotated);
  569. pdfContentEditControl.RefreshThumb();
  570. }
  571. };
  572. editCommand.PopupMenu.Items.Add(rotateLeftMenu);
  573. MenuItem rotateRightMenu = new MenuItem();
  574. rotateRightMenu.Header = LanguageHelper.CommonManager.GetString("Menu_RotateRight");
  575. rotateRightMenu.Click += (o, p) =>
  576. {
  577. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  578. {
  579. lastPDFEditEvent.Rotate = 90;
  580. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  581. pdfContentEditControl.SetRotationText(lastPDFEditEvent.CurrentRotated);
  582. pdfContentEditControl.RefreshThumb();
  583. }
  584. };
  585. editCommand.PopupMenu.Items.Add(rotateRightMenu);
  586. MenuItem replaceMenu = new MenuItem();
  587. replaceMenu.Header = LanguageHelper.CommonManager.GetString("Menu_Replace");
  588. replaceMenu.Click += (o, p) =>
  589. {
  590. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  591. {
  592. OpenFileDialog openFileDialog = new OpenFileDialog();
  593. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  594. if (openFileDialog.ShowDialog() == true)
  595. {
  596. lastPDFEditEvent.ReplaceImagePath = openFileDialog.FileName;
  597. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  598. PdfViewControl.PDFView?.ClearSelectPDFEdit();
  599. pdfContentEditControl.RefreshThumb();
  600. }
  601. }
  602. };
  603. editCommand.PopupMenu.Items.Add(replaceMenu);
  604. MenuItem exportMenu = new MenuItem();
  605. exportMenu.Header = LanguageHelper.CommonManager.GetString("Menu_Export");
  606. exportMenu.Click += (o, p) =>
  607. {
  608. if (PdfViewControl != null && PdfViewControl.PDFView != null)
  609. {
  610. Dictionary<int, List<System.Drawing.Bitmap>> imageDict = PdfViewControl.PDFView.GetSelectedImages();
  611. if (imageDict != null && imageDict.Count > 0)
  612. {
  613. System.Windows.Forms.FolderBrowserDialog folderBrowser = new System.Windows.Forms.FolderBrowserDialog();
  614. if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  615. {
  616. string choosePath = folderBrowser.SelectedPath;
  617. string openPath = choosePath;
  618. foreach (int pageIndex in imageDict.Keys)
  619. {
  620. List<System.Drawing.Bitmap> imageList = imageDict[pageIndex];
  621. foreach (System.Drawing.Bitmap image in imageList)
  622. {
  623. string savePath = System.IO.Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
  624. image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  625. openPath = savePath;
  626. }
  627. }
  628. Process.Start("explorer", "/select,\"" + openPath + "\"");
  629. }
  630. }
  631. }
  632. };
  633. editCommand.PopupMenu.Items.Add(exportMenu);
  634. MenuItem opacityMenu = new MenuItem();
  635. opacityMenu.Header = LanguageHelper.CommonManager.GetString("Menu_Opacity");
  636. editCommand.PopupMenu.Items.Add(opacityMenu);
  637. AppendOpacityMenu(opacityMenu);
  638. MenuItem horizonMirror = new MenuItem();
  639. horizonMirror.Header = LanguageHelper.CommonManager.GetString("Menu_HFlip");
  640. horizonMirror.Click += (o, p) =>
  641. {
  642. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  643. {
  644. lastPDFEditEvent.HorizontalMirror = true;
  645. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  646. }
  647. };
  648. editCommand.PopupMenu.Items.Add(horizonMirror);
  649. MenuItem verticalMirror = new MenuItem();
  650. verticalMirror.Header = LanguageHelper.CommonManager.GetString("Menu_VFlip");
  651. verticalMirror.Click += (o, p) =>
  652. {
  653. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  654. {
  655. lastPDFEditEvent.VerticalMirror = true;
  656. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  657. }
  658. };
  659. editCommand.PopupMenu.Items.Add(verticalMirror);
  660. MenuItem cropMenu = new MenuItem();
  661. cropMenu.Header = LanguageHelper.CommonManager.GetString("Menu_Crop");
  662. cropMenu.Click += (o, p) =>
  663. {
  664. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  665. {
  666. lastPDFEditEvent.ClipImage = true;
  667. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  668. }
  669. };
  670. editCommand.PopupMenu.Items.Add(cropMenu);
  671. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Copy"), Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  672. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Cut"), Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  673. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Delete"), Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  674. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Paste"), Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  675. if (editCommand.TextAreaCopied)
  676. {
  677. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_MatchPaste"), Command = CustomCommands.PasteWithoutStyle, CommandTarget = (UIElement)sender });
  678. }
  679. }
  680. else
  681. {
  682. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Paste"), Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  683. }
  684. }
  685. private void PDFEditMultiContextMenu(object sender, PDFEditCommand editCommand)
  686. {
  687. if(editCommand!=null)
  688. {
  689. editCommand.PopupMenu = new ContextMenu();
  690. if (lastPDFEditMultiEvents!=null)
  691. {
  692. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Copy"), Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  693. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Cut"), Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  694. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Delete"), Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  695. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Paste"), Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  696. if(CustomCommands.PasteWithoutStyle.CanExecute(null, (UIElement)sender))
  697. {
  698. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_MatchPaste"), Command = CustomCommands.PasteWithoutStyle, CommandTarget = (UIElement)sender });
  699. }
  700. }
  701. else
  702. {
  703. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Paste"), Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  704. if (CustomCommands.PasteWithoutStyle.CanExecute(null, (UIElement)sender))
  705. {
  706. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_MatchPaste"), Command = CustomCommands.PasteWithoutStyle, CommandTarget = (UIElement)sender });
  707. }
  708. }
  709. }
  710. }
  711. private void AppendOpacityMenu(MenuItem parentMenu)
  712. {
  713. List<int> opacityList = new List<int>()
  714. {
  715. 25,50,75,100
  716. };
  717. foreach (int opacity in opacityList)
  718. {
  719. MenuItem opacityMenu = new MenuItem();
  720. opacityMenu.Header = string.Format("{0}%", opacity);
  721. opacityMenu.Click += (o, p) =>
  722. {
  723. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  724. {
  725. lastPDFEditEvent.Transparency = (int)Math.Ceiling(opacity * 255 / 100D);
  726. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  727. }
  728. };
  729. parentMenu.Items.Add(opacityMenu);
  730. }
  731. }
  732. #endregion
  733. private void CommandBinding_Executed_Undo(object sender, ExecutedRoutedEventArgs e)
  734. {
  735. if (PdfViewControl != null && PdfViewControl.PDFView != null && CanUndo)
  736. {
  737. PdfViewControl.PDFView.UndoManager?.Undo();
  738. }
  739. }
  740. private void CommandBinding_Executed_Redo(object sender, ExecutedRoutedEventArgs e)
  741. {
  742. if (PdfViewControl != null && PdfViewControl.PDFView != null && CanRedo)
  743. {
  744. PdfViewControl.PDFView.UndoManager?.Redo();
  745. }
  746. }
  747. }
  748. }