ContentEditControl.xaml.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFPage;
  3. using ComPDFKit.PDFPage.Edit;
  4. using ComPDFKit.Tool;
  5. using Compdfkit_Tools.Edit;
  6. using Compdfkit_Tools.Helper;
  7. using ComPDFKitViewer;
  8. using Microsoft.Win32;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.ComponentModel;
  12. using System.Diagnostics;
  13. using System.Drawing;
  14. using System.Runtime.CompilerServices;
  15. using System.Windows;
  16. using System.Windows.Controls;
  17. using System.Windows.Controls.Primitives;
  18. using System.Windows.Input;
  19. using System.Windows.Media;
  20. namespace Compdfkit_Tools.PDFControl
  21. {
  22. public partial class ContentEditControl : UserControl, INotifyPropertyChanged
  23. {
  24. #region Property
  25. public PDFViewControl PdfViewControl = new PDFViewControl();
  26. public PDFContentEditControl pdfContentEditControl = new PDFContentEditControl();
  27. private CPDFDisplaySettingsControl displaySettingsControl = null;
  28. private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  29. private TextEditParam pdfTextCreateParam;
  30. private PDFEditParam lastPDFEditEvent = null;
  31. private PanelState panelState = PanelState.GetInstance();
  32. private KeyEventHandler KeyDownHandler;
  33. public event PropertyChangedEventHandler PropertyChanged;
  34. public ICommand CloseTabCommand;
  35. public ICommand ExpandPropertyPanelCommand;
  36. public bool CanUndo
  37. {
  38. get
  39. {
  40. try
  41. {
  42. if (PdfViewControl != null && PdfViewControl.PDFViewTool != null)
  43. {
  44. CPDFViewerTool viewerTool = PdfViewControl.PDFViewTool;
  45. CPDFViewer pdfViewer = viewerTool.GetCPDFViewer();
  46. return pdfViewer.UndoManager.CanUndo;
  47. }
  48. }
  49. catch(Exception ex)
  50. {
  51. }
  52. return false;
  53. }
  54. }
  55. public bool CanRedo
  56. {
  57. get
  58. {
  59. try
  60. {
  61. if (PdfViewControl != null && PdfViewControl.PDFViewTool != null)
  62. {
  63. CPDFViewerTool viewerTool = PdfViewControl.PDFViewTool;
  64. CPDFViewer pdfViewer = viewerTool.GetCPDFViewer();
  65. return pdfViewer.UndoManager.CanRedo;
  66. }
  67. }
  68. catch (Exception ex)
  69. {
  70. }
  71. return false;
  72. }
  73. }
  74. private bool CanSave
  75. {
  76. get
  77. {
  78. try
  79. {
  80. if (PdfViewControl != null && PdfViewControl.PDFViewTool != null)
  81. {
  82. CPDFViewerTool viewerTool = PdfViewControl.PDFViewTool;
  83. CPDFViewer pdfViewer = viewerTool.GetCPDFViewer();
  84. return (pdfViewer.UndoManager.CanUndo | pdfViewer.UndoManager.CanRedo);
  85. }
  86. }
  87. catch (Exception ex)
  88. {
  89. }
  90. return false;
  91. }
  92. }
  93. public event EventHandler<bool> OnCanSaveChanged;
  94. public event EventHandler OnAnnotEditHandler;
  95. #endregion
  96. public ContentEditControl()
  97. {
  98. InitializeComponent();
  99. panelState.PropertyChanged += PanelState_PropertyChanged;
  100. }
  101. private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
  102. {
  103. if (e.PropertyName == nameof(PanelState.IsLeftPanelExpand))
  104. {
  105. ExpandLeftPanel(panelState.IsLeftPanelExpand);
  106. }
  107. else if (e.PropertyName == nameof(PanelState.RightPanel))
  108. {
  109. if (panelState.RightPanel == PanelState.RightPanelState.PropertyPanel)
  110. {
  111. ExpandRightPropertyPanel(pdfContentEditControl, Visibility.Visible);
  112. }
  113. else if (panelState.RightPanel == PanelState.RightPanelState.ViewSettings)
  114. {
  115. ExpandRightPropertyPanel(displaySettingsControl, Visibility.Visible);
  116. }
  117. else
  118. {
  119. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  120. }
  121. }
  122. }
  123. public void ClearPDFEditState(ToggleButton ignoreBtn = null)
  124. {
  125. List<ToggleButton> clearBtnList = new List<ToggleButton>()
  126. {
  127. PDFTextEditButton,
  128. PDFImageEditButton
  129. };
  130. foreach (ToggleButton item in clearBtnList)
  131. {
  132. if (ignoreBtn == item)
  133. {
  134. continue;
  135. }
  136. item.IsChecked = false;
  137. }
  138. }
  139. private void PDFTextEditButton_Click(object sender, RoutedEventArgs e)
  140. {
  141. ToggleButton senderBtn = sender as ToggleButton;
  142. if (senderBtn != null && PdfViewControl != null)
  143. {
  144. ClearPDFEditState(senderBtn);
  145. //if (senderBtn.IsChecked == true)
  146. //{
  147. // PDFEditEvent createParam = new PDFEditEvent();
  148. // createParam.EditType = CPDFEditType.EditText;
  149. // createParam.IsBold = false;
  150. // createParam.IsItalic = false;
  151. // createParam.FontSize = 14;
  152. // createParam.FontName = "Helvetica";
  153. // createParam.FontColor = Colors.Black;
  154. // createParam.TextAlign = TextAlignType.AlignLeft;
  155. // createParam.Transparency = 255;
  156. // if (PdfViewControl.PDFView != null && PdfViewControl.PDFView.Document != null)
  157. // {
  158. // CPDFDocument pdfDoc = PdfViewControl.PDFView.Document;
  159. // PdfViewControl.PDFView.ToolManager.EnableClickCreate = true;
  160. // PdfViewControl.PDFView.ToolManager.ClickCreateWidth = 100;
  161. // if (pdfDoc.PageCount > 0)
  162. // {
  163. // CPDFPage pdfPage = pdfDoc.PageAtIndex(0);
  164. // CPDFEditPage editPage = pdfPage.GetEditPage();
  165. // editPage.BeginEdit(CPDFEditType.EditText);
  166. // createParam.SystemFontNameList.AddRange(editPage.GetFontList());
  167. // editPage.EndEdit();
  168. // }
  169. // }
  170. // PdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditText);
  171. // PdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.EditText);
  172. // PdfViewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit);
  173. // PdfViewControl.PDFView?.ReloadDocument();
  174. // PdfViewControl.PDFView?.SetPDFEditParam(createParam);
  175. // pdfContentEditControl.SetPDFTextEditData(createParam);
  176. // panelState.RightPanel = PanelState.RightPanelState.PropertyPanel;
  177. // pdfTextCreateParam = createParam;
  178. //}
  179. //else
  180. //{
  181. // PdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.None);
  182. // PdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditImage | CPDFEditType.EditText);
  183. // PdfViewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit);
  184. // PdfViewControl.PDFView?.ReloadDocument();
  185. // pdfContentEditControl.ClearContentControl();
  186. // panelState.RightPanel = PanelState.RightPanelState.None;
  187. //}
  188. }
  189. }
  190. private void PDFImageEditButton_Click(object sender, RoutedEventArgs e)
  191. {
  192. ToggleButton senderBtn = sender as ToggleButton;
  193. if (senderBtn != null && PdfViewControl != null)
  194. {
  195. panelState.RightPanel = PanelState.RightPanelState.None;
  196. senderBtn.IsChecked = false;
  197. OpenFileDialog openFileDialog = new OpenFileDialog();
  198. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  199. //if (openFileDialog.ShowDialog() == true)
  200. //{
  201. // ClearPDFEditState(senderBtn);
  202. // PdfViewControl.PDFView?.ClearSelectPDFEdit();
  203. // PdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditImage | CPDFEditType.EditText);
  204. // PdfViewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit);
  205. // PdfViewControl.PDFView?.ReloadDocument();
  206. // PdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.EditImage);
  207. // PdfViewControl.PDFView?.AddPDFEditImage(openFileDialog.FileName);
  208. //}
  209. }
  210. }
  211. public void ExpandLeftPanel(bool isExpand)
  212. {
  213. BotaContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  214. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  215. if (isExpand)
  216. {
  217. BodyGrid.ColumnDefinitions[0].Width = new GridLength(320);
  218. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  219. }
  220. else
  221. {
  222. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  223. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  224. }
  225. }
  226. private void UndoBtn_Click(object sender, RoutedEventArgs e)
  227. {
  228. if (PdfViewControl != null && PdfViewControl.PDFViewTool != null)
  229. {
  230. PdfViewControl.PDFViewTool.GetCPDFViewer()?.UndoManager?.Undo();
  231. PdfViewControl.PDFViewTool.GetCPDFViewer().UpDateRenderFrame();
  232. }
  233. }
  234. private void RedoBtn_Click(object sender, RoutedEventArgs e)
  235. {
  236. if (PdfViewControl != null && PdfViewControl.PDFViewTool != null)
  237. {
  238. PdfViewControl.PDFViewTool.GetCPDFViewer()?.UndoManager?.Redo();
  239. PdfViewControl.PDFViewTool.GetCPDFViewer().UpDateRenderFrame();
  240. }
  241. }
  242. public void SetViewSettings(Visibility visibility, CPDFDisplaySettingsControl displaySettingsControl = null)
  243. {
  244. this.PropertyContainer.Child = displaySettingsControl;
  245. this.PropertyContainer.Visibility = visibility;
  246. }
  247. public void SetBOTAContainer(CPDFBOTABarControl botaControl)
  248. {
  249. this.BotaContainer.Child = botaControl;
  250. }
  251. public void SetDisplaySettingsControl(CPDFDisplaySettingsControl displaySettingsControl)
  252. {
  253. this.displaySettingsControl = displaySettingsControl;
  254. }
  255. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  256. {
  257. }
  258. private void UserControl_UnLoaded(object sender, RoutedEventArgs e)
  259. {
  260. }
  261. public void ExpandRightPropertyPanel(UIElement propertytPanel, Visibility visible)
  262. {
  263. PropertyContainer.Width = 260;
  264. PropertyContainer.Child = propertytPanel;
  265. PropertyContainer.Visibility = visible;
  266. }
  267. public void InitWithPDFViewer(PDFViewControl view)
  268. {
  269. PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  270. PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  271. PdfViewControl = view;
  272. PDFGrid.Child = PdfViewControl;
  273. FloatPageTool.InitWithPDFViewer(view);
  274. pdfContentEditControl.InitWithPDFViewer(view);
  275. DataContext = this;
  276. }
  277. /// <summary>
  278. /// Text and Image Selected Event
  279. /// </summary>
  280. private void PDFView_PDFEditActiveHandler(object sender, PDFEditParam e)
  281. {
  282. lastPDFEditEvent = e;
  283. if (e == null)
  284. {
  285. PropertyContainer.Child = pdfContentEditControl;
  286. //if (pdfTextCreateParam != null && PdfViewControl != null && PdfViewControl.PDFView != null)
  287. //{
  288. // if (PdfViewControl.PDFView.GetPDFEditCreateType() == CPDFEditType.EditText)
  289. // {
  290. // pdfContentEditControl.SetPDFTextEditData(pdfTextCreateParam);
  291. // }
  292. // else if (PdfViewControl.PDFView.GetPDFEditCreateType() == CPDFEditType.None)
  293. // {
  294. // pdfContentEditControl.ClearContentControl();
  295. // }
  296. //}
  297. //else
  298. //{
  299. // pdfContentEditControl.ClearContentControl();
  300. //}
  301. return;
  302. }
  303. if (e.EditType == CPDFEditType.EditText)
  304. {
  305. pdfContentEditControl.SetPDFTextEditData((TextEditParam)e, true);
  306. return;
  307. }
  308. if (e.EditType == CPDFEditType.EditImage && PdfViewControl != null)
  309. {
  310. UIElement pageView = sender as UIElement;
  311. if (pageView != null)
  312. {
  313. pageView.MouseLeftButtonUp += PageView_MouseLeftButtonUp;
  314. }
  315. pdfContentEditControl.SetPDFImageEditData((ImageEditParam)e);
  316. return;
  317. }
  318. }
  319. private void PageView_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  320. {
  321. UIElement pageView = sender as UIElement;
  322. if (pageView != null)
  323. {
  324. pageView.MouseLeftButtonUp -= PageView_MouseLeftButtonUp;
  325. }
  326. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  327. {
  328. pdfContentEditControl.SetPDFImageEditData((ImageEditParam)lastPDFEditEvent);
  329. }
  330. }
  331. public void ClearViewerControl()
  332. {
  333. PDFGrid.Child = null;
  334. BotaContainer.Child = null;
  335. PropertyContainer.Child = null;
  336. displaySettingsControl = null;
  337. }
  338. #region Property changed
  339. protected void OnPropertyChanged([CallerMemberName] string name = null)
  340. {
  341. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  342. }
  343. public void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  344. {
  345. OnPropertyChanged(e.PropertyName);
  346. if (e.PropertyName == "CanSave")
  347. {
  348. OnCanSaveChanged?.Invoke(this, CanSave);
  349. }
  350. }
  351. #endregion
  352. private void CommandBinding_Executed_Undo(object sender, ExecutedRoutedEventArgs e)
  353. {
  354. if (PdfViewControl != null && PdfViewControl.PDFViewTool != null && CanUndo)
  355. {
  356. PdfViewControl.PDFViewTool.GetCPDFViewer()?.UndoManager?.Undo();
  357. }
  358. }
  359. private void CommandBinding_Executed_Redo(object sender, ExecutedRoutedEventArgs e)
  360. {
  361. if (PdfViewControl != null && PdfViewControl.PDFViewTool != null && CanRedo)
  362. {
  363. PdfViewControl.PDFViewTool.GetCPDFViewer()?.UndoManager?.Redo();
  364. }
  365. }
  366. }
  367. }