ContentEditControl.xaml.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. using ComPDFKit.PDFPage;
  2. using ComPDFKit.PDFPage.Edit;
  3. using ComPDFKit.Tool;
  4. using ComPDFKit.Tool.SettingParam;
  5. using Compdfkit_Tools.Edit;
  6. using Compdfkit_Tools.Helper;
  7. using ComPDFKitViewer;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.ComponentModel;
  11. using System.Diagnostics;
  12. using System.Drawing;
  13. using System.IO;
  14. using System.Runtime.CompilerServices;
  15. using System.Windows;
  16. using System.Windows.Controls.Primitives;
  17. using System.Windows.Forms;
  18. using System.Windows.Input;
  19. using System.Windows.Media;
  20. using static ComPDFKit.Tool.CPDFToolManager;
  21. using System.Windows.Media.Imaging;
  22. using ComPDFKit.Import;
  23. using ComPDFKit.Tool.Help;
  24. using ComPDFKit.Viewer.Helper;
  25. using ContextMenu = System.Windows.Controls.ContextMenu;
  26. using KeyEventArgs = System.Windows.Input.KeyEventArgs;
  27. using KeyEventHandler = System.Windows.Input.KeyEventHandler;
  28. using MenuItem = System.Windows.Controls.MenuItem;
  29. using OpenFileDialog = Microsoft.Win32.OpenFileDialog;
  30. using UserControl = System.Windows.Controls.UserControl;
  31. namespace Compdfkit_Tools.PDFControl
  32. {
  33. public partial class ContentEditControl : UserControl, INotifyPropertyChanged
  34. {
  35. #region Property
  36. private bool isUpdateStartPoint = true;
  37. public ICommand CloseTabCommand;
  38. public ICommand ExpandPropertyPanelCommand;
  39. private CPoint startPoint;
  40. private CPoint endPoint;
  41. public PDFViewControl PdfViewControl;
  42. public PDFContentEditControl pdfContentEditControl = new PDFContentEditControl();
  43. private CPDFDisplaySettingsControl displaySettingsControl = null;
  44. private TextEditParam pdfTextCreateParam;
  45. private PDFEditParam imageAreaParam = null;
  46. private List<TextEditParam> lastPDFEditMultiEvents = null;
  47. private PanelState panelState = PanelState.GetInstance();
  48. private KeyEventHandler KeyDownHandler;
  49. public event PropertyChangedEventHandler PropertyChanged;
  50. public bool CanUndo
  51. {
  52. get
  53. {
  54. try
  55. {
  56. if (PdfViewControl != null && PdfViewControl.PDFViewTool != null)
  57. {
  58. CPDFViewerTool viewerTool = PdfViewControl.PDFViewTool;
  59. CPDFViewer pdfViewer = viewerTool.GetCPDFViewer();
  60. return pdfViewer.UndoManager.CanUndo;
  61. }
  62. }
  63. catch (Exception ex)
  64. {
  65. }
  66. return false;
  67. }
  68. }
  69. public bool CanRedo
  70. {
  71. get
  72. {
  73. try
  74. {
  75. if (PdfViewControl != null && PdfViewControl.PDFViewTool != null)
  76. {
  77. CPDFViewerTool viewerTool = PdfViewControl.PDFViewTool;
  78. CPDFViewer pdfViewer = viewerTool.GetCPDFViewer();
  79. return pdfViewer.UndoManager.CanRedo;
  80. }
  81. }
  82. catch (Exception ex)
  83. {
  84. }
  85. return false;
  86. }
  87. }
  88. private bool CanSave
  89. {
  90. get
  91. {
  92. try
  93. {
  94. if (PdfViewControl != null && PdfViewControl.PDFViewTool != null)
  95. {
  96. CPDFViewerTool viewerTool = PdfViewControl.PDFViewTool;
  97. CPDFViewer pdfViewer = viewerTool.GetCPDFViewer();
  98. return (pdfViewer.UndoManager.CanUndo | pdfViewer.UndoManager.CanRedo);
  99. }
  100. }
  101. catch (Exception ex)
  102. {
  103. }
  104. return false;
  105. }
  106. }
  107. public event EventHandler<bool> OnCanSaveChanged;
  108. public event EventHandler OnAnnotEditHandler;
  109. #endregion
  110. public ContentEditControl()
  111. {
  112. InitializeComponent();
  113. panelState.PropertyChanged -= PanelState_PropertyChanged;
  114. panelState.PropertyChanged += PanelState_PropertyChanged;
  115. }
  116. #region public method
  117. public void ClearPDFEditState(ToggleButton ignoreBtn = null)
  118. {
  119. List<ToggleButton> clearBtnList = new List<ToggleButton>()
  120. {
  121. PDFTextEditButton,
  122. PDFImageEditButton
  123. };
  124. foreach (ToggleButton item in clearBtnList)
  125. {
  126. if (ignoreBtn == item)
  127. {
  128. continue;
  129. }
  130. item.IsChecked = false;
  131. }
  132. PdfViewControl.PDFToolManager.SetCreateContentEditType(CPDFEditType.None);
  133. PdfViewControl.PDFViewTool.GetCPDFViewer().SetIsVisibleCustomMouse(false);
  134. PdfViewControl.PDFViewTool.GetCPDFViewer().SetIsShowStampMouse(false);
  135. }
  136. public void SetViewSettings(Visibility visibility, CPDFDisplaySettingsControl displaySettingsControl = null)
  137. {
  138. this.PropertyContainer.Child = displaySettingsControl;
  139. this.PropertyContainer.Visibility = visibility;
  140. }
  141. public void SetBOTAContainer(CPDFBOTABarControl botaControl)
  142. {
  143. this.BotaContainer.Child = botaControl;
  144. botaControl.ReplaceFunctionEnabled = true;
  145. }
  146. public void SetDisplaySettingsControl(CPDFDisplaySettingsControl displaySettingsControl)
  147. {
  148. this.displaySettingsControl = displaySettingsControl;
  149. }
  150. public void InitWithPDFViewer(PDFViewControl view)
  151. {
  152. PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  153. PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  154. PdfViewControl.MouseLeftButtonDownHandler -= PDFToolManager_MouseLeftButtonDownHandler;
  155. PdfViewControl.MouseLeftButtonDownHandler += PDFToolManager_MouseLeftButtonDownHandler;
  156. PdfViewControl.MouseLeftButtonUpHandler -= PDFToolManager_MouseLeftButtonUpHandler;
  157. PdfViewControl.MouseLeftButtonUpHandler += PDFToolManager_MouseLeftButtonUpHandler;
  158. PdfViewControl = view;
  159. PDFGrid.Child = PdfViewControl;
  160. FloatPageTool.InitWithPDFViewer(view);
  161. pdfContentEditControl.InitWithPDFViewer(view);
  162. DataContext = this;
  163. if (PdfViewControl != null)
  164. {
  165. PdfViewControl.PDFViewTool.IsManipulationEnabled = true;
  166. if (KeyDownHandler != null)
  167. {
  168. PdfViewControl.PDFViewTool.RemoveHandler(KeyDownEvent, KeyDownHandler);
  169. }
  170. KeyDownHandler = new KeyEventHandler(PDFView_KeyDown);
  171. PdfViewControl.PDFViewTool.AddHandler(KeyDownEvent, KeyDownHandler, false);
  172. }
  173. }
  174. /// <summary>
  175. /// Short cut key for PDFView
  176. /// </summary>
  177. public void PDFView_KeyDown(object sender, KeyEventArgs e)
  178. {
  179. if (PdfViewControl.PDFViewTool.GetViewerModel() != MouseModes.ContentEdit)
  180. {
  181. return;
  182. }
  183. int pageIndex = -1;
  184. CPDFEditTextArea textArea = PdfViewControl.PDFToolManager.GetSelectedEditAreaObject(ref pageIndex) as CPDFEditTextArea;
  185. if (textArea == null)
  186. {
  187. return;
  188. }
  189. if (Keyboard.Modifiers == ModifierKeys.Control)
  190. {
  191. if (e.Key == Key.Left)
  192. {
  193. textArea.GetPreWordCharPlace();
  194. UpdateTextArea(textArea);
  195. e.Handled = true;
  196. }
  197. if (e.Key == Key.Right)
  198. {
  199. textArea.GetNextWordCharPlace();
  200. UpdateTextArea(textArea);
  201. e.Handled = true;
  202. }
  203. if (e.Key == Key.Up)
  204. {
  205. textArea.GetSectionBeginCharPlace();
  206. UpdateTextArea(textArea);
  207. e.Handled = true;
  208. }
  209. if (e.Key == Key.Down)
  210. {
  211. textArea.GetSectionEndCharPlace();
  212. UpdateTextArea(textArea);
  213. e.Handled = true;
  214. }
  215. }
  216. if (Keyboard.Modifiers == ModifierKeys.Shift)
  217. {
  218. if (isUpdateStartPoint)
  219. {
  220. startPoint = GetPoint(textArea);
  221. isUpdateStartPoint = false;
  222. }
  223. if (e.Key == Key.Left)
  224. {
  225. textArea.GetPrevCharPlace();
  226. endPoint = GetPoint(textArea);
  227. textArea.GetSelectChars(startPoint, endPoint);
  228. UpdateTextArea(textArea);
  229. e.Handled = true;
  230. }
  231. if (e.Key == Key.Right)
  232. {
  233. textArea.GetNextCharPlace();
  234. endPoint = GetPoint(textArea);
  235. textArea.GetSelectChars(startPoint, endPoint);
  236. UpdateTextArea(textArea);
  237. e.Handled = true;
  238. }
  239. if (e.Key == Key.Up)
  240. {
  241. textArea.GetUpCharPlace();
  242. endPoint = GetPoint(textArea);
  243. textArea.GetSelectChars(startPoint, endPoint);
  244. UpdateTextArea(textArea);
  245. e.Handled = true;
  246. }
  247. if (e.Key == Key.Down)
  248. {
  249. textArea.GetDownCharPlace();
  250. endPoint = GetPoint(textArea);
  251. textArea.GetSelectChars(startPoint, endPoint);
  252. UpdateTextArea(textArea);
  253. e.Handled = true;
  254. }
  255. }
  256. if (Keyboard.Modifiers == ModifierKeys.Alt)
  257. {
  258. if (e.SystemKey == Key.Up)
  259. {
  260. textArea.GetLineBeginCharPlace();
  261. UpdateTextArea(textArea);
  262. e.Handled = true;
  263. }
  264. if (e.SystemKey == Key.Down)
  265. {
  266. textArea.GetLineEndCharPlace();
  267. UpdateTextArea(textArea);
  268. e.Handled = true;
  269. }
  270. }
  271. if (Keyboard.Modifiers == ModifierKeys.None)
  272. {
  273. var oldFrame = textArea.GetFrame();
  274. if (e.Key == Key.Left)
  275. {
  276. oldFrame.left -= 5;
  277. e.Handled = true;
  278. }
  279. if (e.Key == Key.Right)
  280. {
  281. oldFrame.left += 5;
  282. e.Handled = true;
  283. }
  284. if (e.Key == Key.Up)
  285. {
  286. oldFrame.top -= 5;
  287. e.Handled = true;
  288. }
  289. if (e.Key == Key.Down)
  290. {
  291. oldFrame.top += 5;
  292. e.Handled = true;
  293. }
  294. textArea.SetFrame(oldFrame);
  295. }
  296. if (Keyboard.Modifiers == (ModifierKeys.Control | ModifierKeys.Shift))
  297. {
  298. if (isUpdateStartPoint)
  299. {
  300. startPoint = GetPoint(textArea);
  301. isUpdateStartPoint = false;
  302. }
  303. if (e.Key == Key.Left)
  304. {
  305. textArea.GetPreWordCharPlace();
  306. endPoint = GetPoint(textArea);
  307. textArea.GetSelectChars(startPoint, endPoint);
  308. UpdateTextArea(textArea);
  309. e.Handled = true;
  310. }
  311. if (e.Key == Key.Right)
  312. {
  313. textArea.GetNextWordCharPlace();
  314. endPoint = GetPoint(textArea);
  315. textArea.GetSelectChars(startPoint, endPoint);
  316. UpdateTextArea(textArea);
  317. e.Handled = true;
  318. }
  319. if (e.Key == Key.Up)
  320. {
  321. textArea.GetSectionBeginCharPlace();
  322. endPoint = GetPoint(textArea);
  323. textArea.GetSelectChars(startPoint, endPoint);
  324. UpdateTextArea(textArea);
  325. e.Handled = true;
  326. }
  327. if (e.Key == Key.Down)
  328. {
  329. textArea.GetSectionEndCharPlace();
  330. endPoint = GetPoint(textArea);
  331. textArea.GetSelectChars(startPoint, endPoint);
  332. UpdateTextArea(textArea);
  333. e.Handled = true;
  334. }
  335. }
  336. }
  337. #endregion
  338. #region Panel
  339. public void ExpandLeftPanel(bool isExpand)
  340. {
  341. BotaContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  342. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  343. if (isExpand)
  344. {
  345. BodyGrid.ColumnDefinitions[0].Width = new GridLength(320);
  346. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  347. }
  348. else
  349. {
  350. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  351. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  352. }
  353. }
  354. public void ExpandRightPropertyPanel(UIElement propertytPanel, Visibility visible)
  355. {
  356. PropertyContainer.Width = 260;
  357. PropertyContainer.Child = propertytPanel;
  358. PropertyContainer.Visibility = visible;
  359. }
  360. private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
  361. {
  362. if (e.PropertyName == nameof(PanelState.IsLeftPanelExpand))
  363. {
  364. ExpandLeftPanel(panelState.IsLeftPanelExpand);
  365. }
  366. else if (e.PropertyName == nameof(PanelState.RightPanel))
  367. {
  368. if (panelState.RightPanel == PanelState.RightPanelState.PropertyPanel)
  369. {
  370. ExpandRightPropertyPanel(pdfContentEditControl, Visibility.Visible);
  371. }
  372. else if (panelState.RightPanel == PanelState.RightPanelState.ViewSettings)
  373. {
  374. ExpandRightPropertyPanel(displaySettingsControl, Visibility.Visible);
  375. }
  376. else
  377. {
  378. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  379. }
  380. }
  381. }
  382. #endregion
  383. #region UI
  384. private void PDFTextEditButton_Click(object sender, RoutedEventArgs e)
  385. {
  386. ToggleButton senderBtn = sender as ToggleButton;
  387. if (senderBtn != null && PdfViewControl != null)
  388. {
  389. ClearPDFEditState(senderBtn);
  390. if (senderBtn.IsChecked == true)
  391. {
  392. TextEditParam textEditParam = new TextEditParam();
  393. textEditParam.EditType = CPDFEditType.EditText;
  394. textEditParam.IsBold = false;
  395. textEditParam.IsItalic = false;
  396. textEditParam.FontSize = 14;
  397. textEditParam.FontName = "Arial";
  398. textEditParam.FontColor = new byte[] { 0, 0, 0 };
  399. textEditParam.EditIndex = -1;
  400. textEditParam.TextAlign = TextAlignType.AlignLeft;
  401. textEditParam.Transparency = 255;
  402. pdfContentEditControl.SetPDFTextEditData(textEditParam);
  403. DefaultSettingParam defaultSettingParam = PdfViewControl.PDFViewTool.GetDefaultSettingParam();
  404. defaultSettingParam.SetPDFEditParamm(textEditParam);
  405. panelState.RightPanel = PanelState.RightPanelState.PropertyPanel;
  406. PdfViewControl.PDFToolManager.SetCreateContentEditType(CPDFEditType.EditText);
  407. }
  408. else
  409. {
  410. }
  411. }
  412. }
  413. private void PDFImageEditButton_Click(object sender, RoutedEventArgs e)
  414. {
  415. ToggleButton senderBtn = sender as ToggleButton;
  416. if (senderBtn != null && PdfViewControl != null)
  417. {
  418. panelState.RightPanel = PanelState.RightPanelState.None;
  419. senderBtn.IsChecked = false;
  420. OpenFileDialog openFileDialog = new OpenFileDialog();
  421. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  422. if (openFileDialog.ShowDialog() == true)
  423. {
  424. ClearPDFEditState(senderBtn);
  425. try
  426. {
  427. bool SetImage = PdfViewControl.PDFViewTool.GetCPDFViewer().SetStampMouseImage(openFileDialog.FileName);
  428. PdfViewControl.PDFToolManager.SetCreateImagePath(openFileDialog.FileName);
  429. PdfViewControl.PDFViewTool.GetCPDFViewer().SetIsVisibleCustomMouse(SetImage);
  430. PdfViewControl.PDFViewTool.GetCPDFViewer().SetIsShowStampMouse(SetImage);
  431. PdfViewControl.PDFToolManager.SetCreateContentEditType(CPDFEditType.EditImage);
  432. }
  433. catch
  434. {
  435. }
  436. }
  437. }
  438. }
  439. private void UndoBtn_Click(object sender, RoutedEventArgs e)
  440. {
  441. if (PdfViewControl != null && PdfViewControl.PDFViewTool != null)
  442. {
  443. PdfViewControl.PDFViewTool.GetCPDFViewer()?.UndoManager?.Undo();
  444. PdfViewControl.PDFViewTool.GetCPDFViewer().UpdateRenderFrame();
  445. }
  446. }
  447. private void RedoBtn_Click(object sender, RoutedEventArgs e)
  448. {
  449. if (PdfViewControl != null && PdfViewControl.PDFViewTool != null)
  450. {
  451. PdfViewControl.PDFViewTool.GetCPDFViewer()?.UndoManager?.Redo();
  452. PdfViewControl.PDFViewTool.GetCPDFViewer().UpdateRenderFrame();
  453. }
  454. }
  455. #endregion
  456. #region
  457. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  458. {
  459. PdfViewControl.MouseRightButtonDownHandler -= PdfViewControl_MouseRightButtonDownHandler;
  460. PdfViewControl.MouseRightButtonDownHandler += PdfViewControl_MouseRightButtonDownHandler;
  461. }
  462. private void UserControl_UnLoaded(object sender, RoutedEventArgs e)
  463. {
  464. PdfViewControl.MouseRightButtonDownHandler -= PdfViewControl_MouseRightButtonDownHandler;
  465. }
  466. private void PDFEditEmptyPanel()
  467. {
  468. PropertyContainer.Child = pdfContentEditControl;
  469. if (pdfTextCreateParam != null && PdfViewControl != null && PdfViewControl.PDFView != null)
  470. {
  471. if (PdfViewControl.PDFToolManager.GetCreateContentEditType() == CPDFEditType.EditText)
  472. {
  473. pdfContentEditControl.SetPDFTextEditData(pdfTextCreateParam);
  474. }
  475. else if (PdfViewControl.PDFToolManager.GetCreateContentEditType() == CPDFEditType.None)
  476. {
  477. pdfContentEditControl.ClearContentControl();
  478. }
  479. }
  480. else
  481. {
  482. pdfContentEditControl.ClearContentControl();
  483. }
  484. }
  485. private CPoint GetPoint(CPDFEditTextArea textArea)
  486. {
  487. CPoint caretPoint = new CPoint();
  488. CPoint caretPointHigh = new CPoint();
  489. textArea.GetTextCursorPoints(ref caretPoint, ref caretPointHigh);
  490. var lineHeight = caretPoint.y - caretPointHigh.y;
  491. CRect caretRect = new CRect(caretPoint.x, caretPoint.y, caretPointHigh.x, caretPointHigh.y);
  492. caretPoint = new CPoint(caretRect.left, caretRect.top);
  493. return new CPoint(caretRect.left, (caretRect.top + caretRect.bottom) / 2);
  494. }
  495. /// <summary>
  496. /// Update the text appearance after the text area is changed
  497. /// </summary>
  498. private void UpdateTextArea(CPDFEditTextArea textArea)
  499. {
  500. Rect oldRect = DataConversionForWPF.CRectConversionForRect(textArea.GetFrame());
  501. PdfViewControl.PDFViewTool.UpdateRender(oldRect, textArea);
  502. }
  503. /// <summary>
  504. /// Update the image appearance after the image area is changed
  505. /// </summary>
  506. private void UpdateImageArea(CPDFEditImageArea imageArea)
  507. {
  508. Rect oldRect = DataConversionForWPF.CRectConversionForRect(imageArea.GetFrame());
  509. PdfViewControl.PDFViewTool.UpdateRender(oldRect, imageArea);
  510. }
  511. private void PdfViewControl_MouseRightButtonDownHandler(object sender, MouseEventObject e)
  512. {
  513. ContextMenu ContextMenu = PdfViewControl.GetRightMenu();
  514. if (ContextMenu == null)
  515. {
  516. ContextMenu = new ContextMenu();
  517. }
  518. switch (e.hitTestType)
  519. {
  520. case MouseHitTestType.TextEdit:
  521. CreateTextEditMenu(sender, ref ContextMenu);
  522. break;
  523. case MouseHitTestType.ImageEdit:
  524. CreateImageEditMenu(sender, ref ContextMenu);
  525. break;
  526. default:
  527. ContextMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Paste"), Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  528. ContextMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_MatchPaste"), Command = CustomCommands.PasteWithoutStyle, CommandTarget = (UIElement)sender });
  529. break;
  530. }
  531. PdfViewControl.SetRightMenu(ContextMenu);
  532. }
  533. #endregion
  534. #region Property changed
  535. protected void OnPropertyChanged([CallerMemberName] string name = null)
  536. {
  537. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  538. }
  539. public void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  540. {
  541. OnPropertyChanged(e.PropertyName);
  542. if (e.PropertyName == "CanSave")
  543. {
  544. OnCanSaveChanged?.Invoke(this, CanSave);
  545. }
  546. }
  547. #endregion
  548. #region Context menu
  549. private void AppendOpacityMenu(MenuItem parentMenu, CPDFEditArea editArea, CPDFEditType editType)
  550. {
  551. List<int> opacityList = new List<int>()
  552. {
  553. 25,50,75,100
  554. };
  555. foreach (int opacity in opacityList)
  556. {
  557. MenuItem opacityMenu = new MenuItem();
  558. opacityMenu.Header = string.Format("{0}%", opacity);
  559. opacityMenu.Click += (o, p) =>
  560. {
  561. if (editArea != null && editType == CPDFEditType.EditImage)
  562. {
  563. CPDFEditImageArea editImageArea = editArea as CPDFEditImageArea;
  564. editImageArea?.SetImageTransparency((byte)Math.Ceiling(opacity * 255 / 100D));
  565. pdfContentEditControl.RefreshThumb();
  566. UpdateImageArea(editImageArea);
  567. }
  568. else if (editArea != null && editType == CPDFEditType.EditText)
  569. {
  570. CPDFEditTextArea editTextArea = editArea as CPDFEditTextArea;
  571. editTextArea?.SetCharsFontTransparency((byte)Math.Ceiling(opacity * 255 / 100D));
  572. pdfContentEditControl.RefreshThumb();
  573. UpdateTextArea(editTextArea);
  574. }
  575. };
  576. parentMenu.Items.Add(opacityMenu);
  577. }
  578. }
  579. private void PDFToolManager_MouseLeftButtonUpHandler(object sender, MouseEventObject e)
  580. {
  581. if (e.IsCreate)
  582. {
  583. PdfViewControl.SetIsShowStampMouse(false);
  584. }
  585. else
  586. {
  587. int PageIndex = -1;
  588. CPDFEditArea editAreaArea = PdfViewControl.PDFToolManager.GetSelectedEditAreaObject(ref PageIndex);
  589. if (editAreaArea == null)
  590. {
  591. return;
  592. }
  593. else
  594. {
  595. if (editAreaArea.Type == CPDFEditType.EditText)
  596. {
  597. PDFEditParam pDFEditParam = ParamConverter.CPDFDataConverterToPDFEitParam(PdfViewControl.PDFToolManager.GetDocument(), editAreaArea, PageIndex);
  598. pdfContentEditControl.SetPDFTextEditData((TextEditParam)pDFEditParam, true);
  599. PropertyContainer.Child = pdfContentEditControl;
  600. }
  601. //panelState.RightPanel = PanelState.RightPanelState.PropertyPanel;
  602. }
  603. }
  604. }
  605. private void PDFToolManager_MouseLeftButtonDownHandler(object sender, MouseEventObject e)
  606. {
  607. isUpdateStartPoint = true;
  608. if (PdfViewControl.PDFToolManager.GetToolType() != ToolType.ContentEdit)
  609. {
  610. PropertyContainer.Child = pdfContentEditControl;
  611. return;
  612. }
  613. int PageIndex = -1;
  614. CPDFEditArea editAreaArea = PdfViewControl.PDFToolManager.GetSelectedEditAreaObject(ref PageIndex);
  615. if (editAreaArea == null)
  616. {
  617. if (PdfViewControl.PDFToolManager.GetCreateContentEditType() != CPDFEditType.EditText)
  618. {
  619. pdfContentEditControl.ClearContentControl();
  620. }
  621. return;
  622. }
  623. else
  624. {
  625. if (editAreaArea.Type == CPDFEditType.EditText)
  626. {
  627. PDFEditParam pDFEditParam = ParamConverter.CPDFDataConverterToPDFEitParam(PdfViewControl.PDFToolManager.GetDocument(), editAreaArea, PageIndex);
  628. pdfContentEditControl.SetPDFTextEditData((TextEditParam)pDFEditParam, true);
  629. PropertyContainer.Child = pdfContentEditControl;
  630. }
  631. else if (editAreaArea.Type == CPDFEditType.EditImage && PdfViewControl != null)
  632. {
  633. UIElement pageView = sender as UIElement;
  634. if (pageView != null)
  635. {
  636. pageView.MouseLeftButtonUp -= PageView_MouseLeftButtonUp;
  637. pageView.MouseLeftButtonUp += PageView_MouseLeftButtonUp;
  638. }
  639. PDFEditParam pDFEditParam = ParamConverter.CPDFDataConverterToPDFEitParam(PdfViewControl.PDFToolManager.GetDocument(), editAreaArea, PageIndex);
  640. pdfContentEditControl.SetPDFImageEditData((ImageEditParam)pDFEditParam);
  641. PropertyContainer.Child = pdfContentEditControl;
  642. }
  643. }
  644. }
  645. private void PdfViewControl_SplitPDFViewToolCreated(object sender, EventArgs e)
  646. {
  647. PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  648. PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  649. }
  650. private void PageView_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  651. {
  652. UIElement pageView = sender as UIElement;
  653. if (pageView != null)
  654. {
  655. pageView.MouseLeftButtonUp -= PageView_MouseLeftButtonUp;
  656. }
  657. if (imageAreaParam != null)
  658. {
  659. pdfContentEditControl.SetPDFImageEditData((ImageEditParam)imageAreaParam);
  660. }
  661. }
  662. public void ClearViewerControl()
  663. {
  664. PDFGrid.Child = null;
  665. BotaContainer.Child = null;
  666. PropertyContainer.Child = null;
  667. displaySettingsControl = null;
  668. }
  669. private void CommandBinding_Executed_Undo(object sender, ExecutedRoutedEventArgs e)
  670. {
  671. if (PdfViewControl != null && PdfViewControl.PDFViewTool != null && CanUndo)
  672. {
  673. PdfViewControl.PDFViewTool.GetCPDFViewer()?.UndoManager?.Undo();
  674. }
  675. }
  676. private void CommandBinding_Executed_Redo(object sender, ExecutedRoutedEventArgs e)
  677. {
  678. if (PdfViewControl != null && PdfViewControl.PDFViewTool != null && CanRedo)
  679. {
  680. PdfViewControl.PDFViewTool.GetCPDFViewer()?.UndoManager?.Redo();
  681. }
  682. }
  683. private void CreateTextEditMenu(object sender, ref ContextMenu menu)
  684. {
  685. int index = -1;
  686. CPDFEditTextArea textArea =
  687. PdfViewControl.PDFToolManager.GetSelectedEditAreaObject(ref index) as CPDFEditTextArea;
  688. if (textArea != null)
  689. {
  690. if (PdfViewControl.PDFToolManager.GetDocument().GetPermissionsInfo().AllowsCopying)
  691. {
  692. menu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Copy"), Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  693. }
  694. menu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Cut"), Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  695. menu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Delete"), Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  696. menu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Paste"), Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  697. }
  698. else
  699. {
  700. menu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Paste"), Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  701. menu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_MatchPaste"), Command = CustomCommands.PasteWithoutStyle, CommandTarget = (UIElement)sender });
  702. }
  703. }
  704. private void CreateImageEditMenu(object sender, ref ContextMenu menu)
  705. {
  706. int index = -1;
  707. CPDFEditImageArea imageArea = PdfViewControl.PDFToolManager.GetSelectedEditAreaObject(ref index) as CPDFEditImageArea;
  708. MenuItem rotateLeftMenu = new MenuItem();
  709. rotateLeftMenu.Header = LanguageHelper.CommonManager.GetString("Menu_RotateLeft");
  710. rotateLeftMenu.Click += (o, p) =>
  711. {
  712. if (imageArea != null)
  713. {
  714. imageArea.Rotate(-90);
  715. pdfContentEditControl.RefreshThumb();
  716. UpdateImageArea(imageArea);
  717. }
  718. };
  719. menu.Items.Add(rotateLeftMenu);
  720. MenuItem rotateRightMenu = new MenuItem();
  721. rotateRightMenu.Header = LanguageHelper.CommonManager.GetString("Menu_RotateRight");
  722. rotateRightMenu.Click += (o, p) =>
  723. {
  724. if (imageArea != null)
  725. {
  726. imageArea.Rotate(90);
  727. pdfContentEditControl.RefreshThumb();
  728. UpdateImageArea(imageArea);
  729. }
  730. };
  731. menu.Items.Add(rotateRightMenu);
  732. MenuItem replaceMenu = new MenuItem();
  733. replaceMenu.Header = LanguageHelper.CommonManager.GetString("Menu_Replace");
  734. replaceMenu.Click += (o, p) =>
  735. {
  736. if (imageArea != null)
  737. {
  738. OpenFileDialog openFileDialog = new OpenFileDialog();
  739. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  740. if (openFileDialog.ShowDialog() == true)
  741. {
  742. int imageWidth = 0;
  743. int imageHeight = 0;
  744. byte[] imageData = null;
  745. BitmapFrame frame = null;
  746. BitmapDecoder decoder = BitmapDecoder.Create(new Uri(openFileDialog.FileName), BitmapCreateOptions.None, BitmapCacheOption.Default);
  747. if (decoder.Frames.Count > 0)
  748. {
  749. frame = decoder.Frames[0];
  750. }
  751. if (frame != null)
  752. {
  753. imageData = new byte[frame.PixelWidth * frame.PixelHeight * 4];
  754. if (frame.Format != PixelFormats.Bgra32)
  755. {
  756. FormatConvertedBitmap covert = new FormatConvertedBitmap(frame, PixelFormats.Bgra32, frame.Palette, 0);
  757. covert.CopyPixels(imageData, frame.PixelWidth * 4, 0);
  758. }
  759. else
  760. {
  761. frame.CopyPixels(imageData, frame.PixelWidth * 4, 0);
  762. }
  763. imageWidth = frame.PixelWidth;
  764. imageHeight = frame.PixelHeight;
  765. }
  766. imageArea.ReplaceImageArea(imageArea.GetFrame(), imageData, imageWidth, imageHeight);
  767. pdfContentEditControl.RefreshThumb();
  768. UpdateImageArea(imageArea);
  769. }
  770. }
  771. };
  772. menu.Items.Add(replaceMenu);
  773. MenuItem exportMenu = new MenuItem();
  774. exportMenu.Header = LanguageHelper.CommonManager.GetString("Menu_Export");
  775. exportMenu.Click += (o, p) =>
  776. {
  777. if (PdfViewControl != null && PdfViewControl.PDFView != null)
  778. {
  779. try
  780. {
  781. FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
  782. if (folderBrowserDialog.ShowDialog() == DialogResult.OK && imageArea != null)
  783. {
  784. string path = Path.GetTempPath();
  785. string uuid = Guid.NewGuid().ToString("N");
  786. string imagePath = Path.Combine(path, uuid + ".tmp");
  787. imageArea.ExtractImage(imagePath);
  788. Bitmap bitmapImage = new Bitmap(imagePath);
  789. string fileName = Path.Combine(folderBrowserDialog.SelectedPath, uuid + ".jpg");
  790. bitmapImage.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
  791. Process.Start("explorer", "/select,\"" + fileName + "\"");
  792. }
  793. }
  794. catch (Exception ex)
  795. {
  796. }
  797. }
  798. };
  799. menu.Items.Add(exportMenu);
  800. MenuItem opacityMenu = new MenuItem();
  801. opacityMenu.Header = LanguageHelper.CommonManager.GetString("Menu_Opacity");
  802. menu.Items.Add(opacityMenu);
  803. AppendOpacityMenu(opacityMenu, imageArea, CPDFEditType.EditImage);
  804. MenuItem horizonMirror = new MenuItem();
  805. horizonMirror.Header = LanguageHelper.CommonManager.GetString("Menu_HFlip");
  806. horizonMirror.Click += (o, p) =>
  807. {
  808. imageArea?.HorizontalMirror();
  809. pdfContentEditControl.RefreshThumb();
  810. UpdateImageArea(imageArea);
  811. };
  812. menu.Items.Add(horizonMirror);
  813. MenuItem verticalMirror = new MenuItem();
  814. verticalMirror.Header = LanguageHelper.CommonManager.GetString("Menu_VFlip");
  815. verticalMirror.Click += (o, p) =>
  816. {
  817. if (imageArea != null)
  818. {
  819. imageArea?.VerticalMirror();
  820. pdfContentEditControl.RefreshThumb();
  821. UpdateImageArea(imageArea);
  822. }
  823. };
  824. menu.Items.Add(verticalMirror);
  825. MenuItem cropMenu = new MenuItem();
  826. cropMenu.Header = LanguageHelper.CommonManager.GetString("Menu_Crop");
  827. cropMenu.Click += (o, p) =>
  828. {
  829. if (imageArea != null)
  830. {
  831. PdfViewControl.PDFViewTool.SetCropMode(!PdfViewControl.PDFViewTool.GetIsCropMode());
  832. }
  833. };
  834. menu.Items.Add(cropMenu);
  835. menu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Copy"), Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  836. menu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Cut"), Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  837. menu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Delete"), Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  838. menu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Paste"), Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  839. }
  840. }
  841. }
  842. #endregion