ContentEditControl.xaml.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.PDFDocument;
  3. using ComPDFKit.PDFPage;
  4. using ComPDFKit.PDFPage.Edit;
  5. using ComPDFKit.Tool;
  6. using ComPDFKit.Tool.SettingParam;
  7. using Compdfkit_Tools.Edit;
  8. using Compdfkit_Tools.Helper;
  9. using ComPDFKitViewer;
  10. using Microsoft.Win32;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.ComponentModel;
  14. using System.Diagnostics;
  15. using System.Drawing;
  16. using System.IO;
  17. using System.Runtime.CompilerServices;
  18. using System.Windows;
  19. using System.Windows.Controls;
  20. using System.Windows.Controls.Primitives;
  21. using System.Windows.Input;
  22. using System.Windows.Media;
  23. using static ComPDFKit.Tool.CPDFToolManager;
  24. using System.Windows.Media.Imaging;
  25. using ComPDFKit.Tool.Help;
  26. namespace Compdfkit_Tools.PDFControl
  27. {
  28. public partial class ContentEditControl : UserControl, INotifyPropertyChanged
  29. {
  30. #region Property
  31. public PDFViewControl PdfViewControl;
  32. public PDFContentEditControl pdfContentEditControl = new PDFContentEditControl();
  33. private CPDFDisplaySettingsControl displaySettingsControl = null;
  34. private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  35. private TextEditParam pdfTextCreateParam;
  36. private PDFEditParam lastPDFEditEvent = null;
  37. private PanelState panelState = PanelState.GetInstance();
  38. private KeyEventHandler KeyDownHandler;
  39. public event PropertyChangedEventHandler PropertyChanged;
  40. public ICommand CloseTabCommand;
  41. public ICommand ExpandPropertyPanelCommand;
  42. public bool CanUndo
  43. {
  44. get
  45. {
  46. try
  47. {
  48. if (PdfViewControl != null && PdfViewControl.PDFViewTool != null)
  49. {
  50. CPDFViewerTool viewerTool = PdfViewControl.PDFViewTool;
  51. CPDFViewer pdfViewer = viewerTool.GetCPDFViewer();
  52. return pdfViewer.UndoManager.CanUndo;
  53. }
  54. }
  55. catch(Exception ex)
  56. {
  57. }
  58. return false;
  59. }
  60. }
  61. public bool CanRedo
  62. {
  63. get
  64. {
  65. try
  66. {
  67. if (PdfViewControl != null && PdfViewControl.PDFViewTool != null)
  68. {
  69. CPDFViewerTool viewerTool = PdfViewControl.PDFViewTool;
  70. CPDFViewer pdfViewer = viewerTool.GetCPDFViewer();
  71. return pdfViewer.UndoManager.CanRedo;
  72. }
  73. }
  74. catch (Exception ex)
  75. {
  76. }
  77. return false;
  78. }
  79. }
  80. private bool CanSave
  81. {
  82. get
  83. {
  84. try
  85. {
  86. if (PdfViewControl != null && PdfViewControl.PDFViewTool != null)
  87. {
  88. CPDFViewerTool viewerTool = PdfViewControl.PDFViewTool;
  89. CPDFViewer pdfViewer = viewerTool.GetCPDFViewer();
  90. return (pdfViewer.UndoManager.CanUndo | pdfViewer.UndoManager.CanRedo);
  91. }
  92. }
  93. catch (Exception ex)
  94. {
  95. }
  96. return false;
  97. }
  98. }
  99. public event EventHandler<bool> OnCanSaveChanged;
  100. public event EventHandler OnAnnotEditHandler;
  101. #endregion
  102. public ContentEditControl()
  103. {
  104. InitializeComponent();
  105. panelState.PropertyChanged += PanelState_PropertyChanged;
  106. }
  107. private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
  108. {
  109. if (e.PropertyName == nameof(PanelState.IsLeftPanelExpand))
  110. {
  111. ExpandLeftPanel(panelState.IsLeftPanelExpand);
  112. }
  113. else if (e.PropertyName == nameof(PanelState.RightPanel))
  114. {
  115. if (panelState.RightPanel == PanelState.RightPanelState.PropertyPanel)
  116. {
  117. ExpandRightPropertyPanel(pdfContentEditControl, Visibility.Visible);
  118. }
  119. else if (panelState.RightPanel == PanelState.RightPanelState.ViewSettings)
  120. {
  121. ExpandRightPropertyPanel(displaySettingsControl, Visibility.Visible);
  122. }
  123. else
  124. {
  125. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  126. }
  127. }
  128. }
  129. public void ClearPDFEditState(ToggleButton ignoreBtn = null)
  130. {
  131. List<ToggleButton> clearBtnList = new List<ToggleButton>()
  132. {
  133. PDFTextEditButton,
  134. PDFImageEditButton
  135. };
  136. foreach (ToggleButton item in clearBtnList)
  137. {
  138. if (ignoreBtn == item)
  139. {
  140. continue;
  141. }
  142. item.IsChecked = false;
  143. }
  144. PdfViewControl.PDFToolManager.SetCreateContentEditType(CPDFEditType.None);
  145. PdfViewControl.PDFViewTool.GetCPDFViewer().SetIsVisibleCustomMouse(false);
  146. PdfViewControl.PDFViewTool.GetCPDFViewer().SetIsShowStampMouse(false);
  147. }
  148. private void PDFTextEditButton_Click(object sender, RoutedEventArgs e)
  149. {
  150. ToggleButton senderBtn = sender as ToggleButton;
  151. if (senderBtn != null && PdfViewControl != null)
  152. {
  153. ClearPDFEditState(senderBtn);
  154. if (senderBtn.IsChecked == true)
  155. {
  156. TextEditParam textEditParam = new TextEditParam();
  157. textEditParam.EditType = CPDFEditType.EditText;
  158. textEditParam.IsBold = false;
  159. textEditParam.IsItalic = false;
  160. textEditParam.FontSize = 14;
  161. textEditParam.FontName = "Helvetica";
  162. textEditParam.FontColor = new byte[] { 0, 0, 0 };
  163. textEditParam.EditIndex = -1;
  164. textEditParam.TextAlign = TextAlignType.AlignLeft;
  165. textEditParam.Transparency = 255;
  166. pdfContentEditControl.SetPDFTextEditData(textEditParam);
  167. DefaultSettingParam defaultSettingParam = PdfViewControl.PDFViewTool.GetDefaultSettingParam();
  168. defaultSettingParam.SetPDFEditParamm(textEditParam);
  169. panelState.RightPanel = PanelState.RightPanelState.PropertyPanel;
  170. PdfViewControl.PDFToolManager.SetCreateContentEditType(CPDFEditType.EditText);
  171. }
  172. else
  173. {
  174. }
  175. // PDFEditEvent createParam = new PDFEditEvent();
  176. // createParam.EditType = CPDFEditType.EditText;
  177. // createParam.IsBold = false;
  178. // createParam.IsItalic = false;
  179. // createParam.FontSize = 14;
  180. // createParam.FontName = "Helvetica";
  181. // createParam.FontColor = Colors.Black;
  182. // createParam.TextAlign = TextAlignType.AlignLeft;
  183. // createParam.Transparency = 255;
  184. // if (PdfViewControl.PDFView != null && PdfViewControl.PDFView.Document != null)
  185. // {
  186. // CPDFDocument pdfDoc = PdfViewControl.PDFView.Document;
  187. // PdfViewControl.PDFView.ToolManager.EnableClickCreate = true;
  188. // PdfViewControl.PDFView.ToolManager.ClickCreateWidth = 100;
  189. // if (pdfDoc.PageCount > 0)
  190. // {
  191. // CPDFPage pdfPage = pdfDoc.PageAtIndex(0);
  192. // CPDFEditPage editPage = pdfPage.GetEditPage();
  193. // editPage.BeginEdit(CPDFEditType.EditText);
  194. // createParam.SystemFontNameList.AddRange(editPage.GetFontList());
  195. // editPage.EndEdit();
  196. // }
  197. // }
  198. // PdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditText);
  199. // PdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.EditText);
  200. // PdfViewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit);
  201. // PdfViewControl.PDFView?.ReloadDocument();
  202. // PdfViewControl.PDFView?.SetPDFEditParam(createParam);
  203. // pdfContentEditControl.SetPDFTextEditData(createParam);
  204. // panelState.RightPanel = PanelState.RightPanelState.PropertyPanel;
  205. // pdfTextCreateParam = createParam;
  206. //}
  207. //else
  208. //{
  209. // PdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.None);
  210. // PdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditImage | CPDFEditType.EditText);
  211. // PdfViewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit);
  212. // PdfViewControl.PDFView?.ReloadDocument();
  213. // pdfContentEditControl.ClearContentControl();
  214. // panelState.RightPanel = PanelState.RightPanelState.None;
  215. //}
  216. }
  217. }
  218. private void PDFImageEditButton_Click(object sender, RoutedEventArgs e)
  219. {
  220. ToggleButton senderBtn = sender as ToggleButton;
  221. if (senderBtn != null && PdfViewControl != null)
  222. {
  223. panelState.RightPanel = PanelState.RightPanelState.None;
  224. senderBtn.IsChecked = false;
  225. OpenFileDialog openFileDialog = new OpenFileDialog();
  226. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  227. if (openFileDialog.ShowDialog() == true)
  228. {
  229. ClearPDFEditState(senderBtn);
  230. try
  231. {
  232. BitmapImage image = new BitmapImage(new Uri(openFileDialog.FileName));
  233. PngBitmapEncoder encoder = new PngBitmapEncoder();
  234. encoder.Frames.Add(BitmapFrame.Create(image));
  235. MemoryStream memoryStream = new MemoryStream();
  236. encoder.Save(memoryStream);
  237. bool SetImage = PdfViewControl.PDFViewTool.GetCPDFViewer().SetStampMouseImage(openFileDialog.FileName);
  238. PdfViewControl.PDFToolManager.SetCreateImagePath(openFileDialog.FileName);
  239. PdfViewControl.PDFViewTool.GetCPDFViewer().SetIsVisibleCustomMouse(SetImage);
  240. PdfViewControl.PDFViewTool.GetCPDFViewer().SetIsShowStampMouse(SetImage);
  241. PdfViewControl.PDFToolManager.SetCreateContentEditType(CPDFEditType.EditImage);
  242. }
  243. catch
  244. {
  245. }
  246. //if (openFileDialog.ShowDialog() == true)
  247. //{
  248. // ClearPDFEditState(senderBtn);
  249. // PdfViewControl.PDFView?.ClearSelectPDFEdit();
  250. // PdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditImage | CPDFEditType.EditText);
  251. // PdfViewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit);
  252. // PdfViewControl.PDFView?.ReloadDocument();
  253. // PdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.EditImage);
  254. // PdfViewControl.PDFView?.AddPDFEditImage(openFileDialog.FileName);
  255. //}
  256. }
  257. }
  258. }
  259. public void ExpandLeftPanel(bool isExpand)
  260. {
  261. BotaContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  262. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  263. if (isExpand)
  264. {
  265. BodyGrid.ColumnDefinitions[0].Width = new GridLength(320);
  266. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  267. }
  268. else
  269. {
  270. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  271. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  272. }
  273. }
  274. private void UndoBtn_Click(object sender, RoutedEventArgs e)
  275. {
  276. if (PdfViewControl != null && PdfViewControl.PDFViewTool != null)
  277. {
  278. PdfViewControl.PDFViewTool.GetCPDFViewer()?.UndoManager?.Undo();
  279. PdfViewControl.PDFViewTool.GetCPDFViewer().UpDateRenderFrame();
  280. }
  281. }
  282. private void RedoBtn_Click(object sender, RoutedEventArgs e)
  283. {
  284. if (PdfViewControl != null && PdfViewControl.PDFViewTool != null)
  285. {
  286. PdfViewControl.PDFViewTool.GetCPDFViewer()?.UndoManager?.Redo();
  287. PdfViewControl.PDFViewTool.GetCPDFViewer().UpDateRenderFrame();
  288. }
  289. }
  290. public void SetViewSettings(Visibility visibility, CPDFDisplaySettingsControl displaySettingsControl = null)
  291. {
  292. this.PropertyContainer.Child = displaySettingsControl;
  293. this.PropertyContainer.Visibility = visibility;
  294. }
  295. public void SetBOTAContainer(CPDFBOTABarControl botaControl)
  296. {
  297. this.BotaContainer.Child = botaControl;
  298. }
  299. public void SetDisplaySettingsControl(CPDFDisplaySettingsControl displaySettingsControl)
  300. {
  301. this.displaySettingsControl = displaySettingsControl;
  302. }
  303. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  304. {
  305. }
  306. private void UserControl_UnLoaded(object sender, RoutedEventArgs e)
  307. {
  308. }
  309. public void ExpandRightPropertyPanel(UIElement propertytPanel, Visibility visible)
  310. {
  311. PropertyContainer.Width = 260;
  312. PropertyContainer.Child = propertytPanel;
  313. PropertyContainer.Visibility = visible;
  314. }
  315. public void InitWithPDFViewer(PDFViewControl view)
  316. {
  317. PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  318. PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  319. PdfViewControl.PDFToolManager.MouseLeftButtonDownHandler -= PDFToolManager_MouseLeftButtonDownHandler;
  320. PdfViewControl.PDFToolManager.MouseLeftButtonDownHandler += PDFToolManager_MouseLeftButtonDownHandler;
  321. PdfViewControl.PDFToolManager.MouseLeftButtonUpHandler -= PDFToolManager_MouseLeftButtonUpHandler;
  322. PdfViewControl.PDFToolManager.MouseLeftButtonUpHandler += PDFToolManager_MouseLeftButtonUpHandler;
  323. PdfViewControl = view;
  324. PdfViewControl.SplitPDFViewToolCreated -= PdfViewControl_SplitPDFViewToolCreated;
  325. PdfViewControl.SplitPDFViewToolCreated += PdfViewControl_SplitPDFViewToolCreated;
  326. PDFGrid.Child = PdfViewControl;
  327. FloatPageTool.InitWithPDFViewer(view);
  328. pdfContentEditControl.InitWithPDFViewer(view);
  329. DataContext = this;
  330. }
  331. private void PDFToolManager_MouseLeftButtonUpHandler(object sender, MouseEventObject e)
  332. {
  333. if (e.IsCreate)
  334. {
  335. PdfViewControl.PDFViewTool.GetCPDFViewer().SetIsShowStampMouse(false);
  336. }
  337. }
  338. private void PDFToolManager_MouseLeftButtonDownHandler(object sender, MouseEventObject e)
  339. {
  340. if (PdfViewControl.PDFToolManager.GetToolType() != ToolType.ContentEdit)
  341. {
  342. PropertyContainer.Child = pdfContentEditControl;
  343. //if (pdfTextCreateParam != null && PdfViewControl != null && PdfViewControl.PDFView != null)
  344. //{
  345. // if (PdfViewControl.PDFView.GetPDFEditCreateType() == CPDFEditType.EditText)
  346. // {
  347. // pdfContentEditControl.SetPDFTextEditData(pdfTextCreateParam);
  348. // }
  349. // else if (PdfViewControl.PDFView.GetPDFEditCreateType() == CPDFEditType.None)
  350. // {
  351. // pdfContentEditControl.ClearContentControl();
  352. // }
  353. //}
  354. //else
  355. //{
  356. // pdfContentEditControl.ClearContentControl();
  357. //}
  358. return;
  359. }
  360. int PageIndex = -1;
  361. CPDFEditArea editAreaArea = PdfViewControl.PDFToolManager.GetSelectedEditAreaObject(ref PageIndex);
  362. if (editAreaArea==null)
  363. {
  364. return;
  365. }
  366. else
  367. {
  368. if (editAreaArea.Type == CPDFEditType.EditText)
  369. {
  370. PDFEditParam pDFEditParam = ParamConverter.CPDFDataConverterToPDFEitParam(PdfViewControl.PDFToolManager.GetDocument(), editAreaArea, PageIndex);
  371. pdfContentEditControl.SetPDFTextEditData((TextEditParam)pDFEditParam, true);
  372. PropertyContainer.Child = pdfContentEditControl;
  373. return;
  374. }
  375. else if (editAreaArea.Type == CPDFEditType.EditImage && PdfViewControl != null)
  376. {
  377. UIElement pageView = sender as UIElement;
  378. if (pageView != null)
  379. {
  380. pageView.MouseLeftButtonUp += PageView_MouseLeftButtonUp;
  381. }
  382. PDFEditParam pDFEditParam = ParamConverter.CPDFDataConverterToPDFEitParam(PdfViewControl.PDFToolManager.GetDocument(), editAreaArea, PageIndex);
  383. pdfContentEditControl.SetPDFImageEditData((ImageEditParam)pDFEditParam);
  384. PropertyContainer.Child = pdfContentEditControl;
  385. return;
  386. }
  387. }
  388. }
  389. private void PdfViewControl_SplitPDFViewToolCreated(object sender, EventArgs e)
  390. {
  391. PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  392. PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  393. }
  394. /// <summary>
  395. /// Text and Image Selected Event
  396. /// </summary>
  397. private void PDFView_PDFEditActiveHandler(object sender, PDFEditParam e)
  398. {
  399. lastPDFEditEvent = e;
  400. if (e == null)
  401. {
  402. PropertyContainer.Child = pdfContentEditControl;
  403. //if (pdfTextCreateParam != null && PdfViewControl != null && PdfViewControl.PDFView != null)
  404. //{
  405. // if (PdfViewControl.PDFView.GetPDFEditCreateType() == CPDFEditType.EditText)
  406. // {
  407. // pdfContentEditControl.SetPDFTextEditData(pdfTextCreateParam);
  408. // }
  409. // else if (PdfViewControl.PDFView.GetPDFEditCreateType() == CPDFEditType.None)
  410. // {
  411. // pdfContentEditControl.ClearContentControl();
  412. // }
  413. //}
  414. //else
  415. //{
  416. // pdfContentEditControl.ClearContentControl();
  417. //}
  418. return;
  419. }
  420. if (e.EditType == CPDFEditType.EditText)
  421. {
  422. pdfContentEditControl.SetPDFTextEditData((TextEditParam)e, true);
  423. return;
  424. }
  425. if (e.EditType == CPDFEditType.EditImage && PdfViewControl != null)
  426. {
  427. UIElement pageView = sender as UIElement;
  428. if (pageView != null)
  429. {
  430. pageView.MouseLeftButtonUp += PageView_MouseLeftButtonUp;
  431. }
  432. pdfContentEditControl.SetPDFImageEditData((ImageEditParam)e);
  433. return;
  434. }
  435. }
  436. private void PageView_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  437. {
  438. UIElement pageView = sender as UIElement;
  439. if (pageView != null)
  440. {
  441. pageView.MouseLeftButtonUp -= PageView_MouseLeftButtonUp;
  442. }
  443. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  444. {
  445. pdfContentEditControl.SetPDFImageEditData((ImageEditParam)lastPDFEditEvent);
  446. }
  447. }
  448. public void ClearViewerControl()
  449. {
  450. PDFGrid.Child = null;
  451. BotaContainer.Child = null;
  452. PropertyContainer.Child = null;
  453. displaySettingsControl = null;
  454. }
  455. #region Property changed
  456. protected void OnPropertyChanged([CallerMemberName] string name = null)
  457. {
  458. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  459. }
  460. public void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  461. {
  462. OnPropertyChanged(e.PropertyName);
  463. if (e.PropertyName == "CanSave")
  464. {
  465. OnCanSaveChanged?.Invoke(this, CanSave);
  466. }
  467. }
  468. #endregion
  469. private void CommandBinding_Executed_Undo(object sender, ExecutedRoutedEventArgs e)
  470. {
  471. if (PdfViewControl != null && PdfViewControl.PDFViewTool != null && CanUndo)
  472. {
  473. PdfViewControl.PDFViewTool.GetCPDFViewer()?.UndoManager?.Undo();
  474. }
  475. }
  476. private void CommandBinding_Executed_Redo(object sender, ExecutedRoutedEventArgs e)
  477. {
  478. if (PdfViewControl != null && PdfViewControl.PDFViewTool != null && CanRedo)
  479. {
  480. PdfViewControl.PDFViewTool.GetCPDFViewer()?.UndoManager?.Redo();
  481. }
  482. }
  483. }
  484. }