ContentEditControl.xaml.cs 43 KB

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