PDFTextEditControl.xaml.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFPage;
  3. using ComPDFKit.PDFPage.Edit;
  4. using ComPDFKit.Tool;
  5. using ComPDFKit.Tool.SettingParam;
  6. using ComPDFKit.Tool.UndoManger;
  7. using ComPDFKit.Viewer.Helper;
  8. using Compdfkit_Tools.PDFControl;
  9. using ComPDFKitViewer;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Controls.Primitives;
  15. using System.Windows.Media;
  16. namespace Compdfkit_Tools.Edit
  17. {
  18. public partial class PDFTextEditControl : UserControl
  19. {
  20. public CPDFViewerTool ToolView { get; private set; }
  21. public TextEditParam EditEvent { get; set; }
  22. public PDFTextEditControl()
  23. {
  24. InitializeComponent();
  25. Loaded += PDFTextEditControl_Loaded;
  26. }
  27. public void InitWithPDFViewer(CPDFViewerTool newPDFView)
  28. {
  29. ToolView = newPDFView;
  30. }
  31. public void SetPDFTextEditData(TextEditParam newEvent)
  32. {
  33. EditEvent = null;
  34. if (newEvent != null && newEvent.EditType == CPDFEditType.EditText)
  35. {
  36. GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  37. List<string> sysfontList = new List<string>();
  38. if (textArea != null)
  39. {
  40. sysfontList = textArea.GetFontList();
  41. }
  42. if (sysfontList.Count == 0)
  43. {
  44. sysfontList.Add("Helvetica");
  45. sysfontList.Add("Courier New");
  46. sysfontList.Add("Times New Roman");
  47. }
  48. if (sysfontList.Contains(newEvent.FontName) == false && string.IsNullOrEmpty(newEvent.FontName) == false)
  49. {
  50. sysfontList.Add(newEvent.FontName);
  51. }
  52. TextStyleUI.SetFontNames(sysfontList);
  53. TextStyleUI.SelectFontName(newEvent.FontName);
  54. TextStyleUI.SetFontStyle(newEvent.IsBold, newEvent.IsItalic);
  55. TextStyleUI.SetFontSize(newEvent.FontSize);
  56. OpacityTextBox.Text = string.Format("{0}%", (int)(Math.Ceiling(newEvent.Transparency * 100 / 255D)));
  57. FontOpacitySlider.Value = ((int)(Math.Ceiling(newEvent.Transparency * 100 / 255D))) / 100D;
  58. TextAlignUI.SetFontAlign(newEvent.TextAlign);
  59. if (newEvent.FontColor != null && newEvent.FontColor.Length == 3)
  60. {
  61. FontColorUI.SetCheckedForColor(Color.FromRgb(
  62. newEvent.FontColor[0],
  63. newEvent.FontColor[1],
  64. newEvent.FontColor[2]));
  65. }
  66. }
  67. EditEvent = newEvent;
  68. }
  69. private void Slider_DragCompleted(object sender, DragCompletedEventArgs e)
  70. {
  71. Slider slider = sender as Slider;
  72. if (slider != null)
  73. {
  74. slider.Tag = "true";
  75. }
  76. GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  77. if (textArea != null)
  78. {
  79. Rect oldRect = DataConversionForWPF.CRectConversionForRect(textArea.GetFrame());
  80. if (textArea.SetCharsFontSize((float)slider.Value, false))
  81. {
  82. PDFEditHistory editHistory = new PDFEditHistory();
  83. editHistory.EditPage = editPage;
  84. if (pdfPage != null)
  85. {
  86. editHistory.PageIndex = pdfPage.PageIndex;
  87. }
  88. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  89. ToolView.UpDateRender(oldRect, textArea);
  90. editPage.EndEdit();
  91. }
  92. }
  93. if (EditEvent != null && textArea == null)
  94. {
  95. EditEvent.FontSize = slider.Value;
  96. DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
  97. defaultSettingParam.SetPDFEditParamm(EditEvent);
  98. }
  99. }
  100. private void SliderOpacity_DragCompleted(object sender, DragCompletedEventArgs e)
  101. {
  102. Slider slider = sender as Slider;
  103. if (slider != null)
  104. {
  105. slider.Tag = "true";
  106. }
  107. GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  108. if (textArea != null)
  109. {
  110. Rect oldRect = DataConversionForWPF.CRectConversionForRect(textArea.GetFrame());
  111. if (textArea.SetCharsFontTransparency((byte)(FontOpacitySlider.Value * 255)))
  112. {
  113. PDFEditHistory editHistory = new PDFEditHistory();
  114. editHistory.EditPage = editPage;
  115. if (pdfPage != null)
  116. {
  117. editHistory.PageIndex = pdfPage.PageIndex;
  118. }
  119. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  120. ToolView.UpDateRender(oldRect, textArea);
  121. editPage.EndEdit();
  122. }
  123. }
  124. if (EditEvent != null && textArea == null)
  125. {
  126. EditEvent.Transparency = (byte)(FontOpacitySlider.Value * 255);
  127. DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
  128. defaultSettingParam.SetPDFEditParamm(EditEvent);
  129. }
  130. }
  131. private void SliderOpacity_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  132. {
  133. Slider slider = sender as Slider;
  134. if (OpacityTextBox != null && FontOpacitySlider != null)
  135. {
  136. OpacityTextBox.Text = string.Format("{0}%", (int)(FontOpacitySlider.Value * 100));
  137. }
  138. if (slider != null && slider.Tag != null && slider.Tag.ToString() == "false")
  139. {
  140. return;
  141. }
  142. GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  143. if (textArea != null)
  144. {
  145. Rect oldRect = DataConversionForWPF.CRectConversionForRect(textArea.GetFrame());
  146. if (textArea.SetCharsFontTransparency((byte)(FontOpacitySlider.Value * 255)))
  147. {
  148. PDFEditHistory editHistory = new PDFEditHistory();
  149. editHistory.EditPage = editPage;
  150. if (pdfPage != null)
  151. {
  152. editHistory.PageIndex = pdfPage.PageIndex;
  153. }
  154. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  155. ToolView.UpDateRender(oldRect, textArea);
  156. editPage.EndEdit();
  157. }
  158. }
  159. if (EditEvent != null && textArea == null)
  160. {
  161. EditEvent.Transparency = (byte)(FontOpacitySlider.Value * 255);
  162. DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
  163. defaultSettingParam.SetPDFEditParamm(EditEvent);
  164. }
  165. }
  166. private void Slider_DragStarted(object sender, DragStartedEventArgs e)
  167. {
  168. Slider slider = sender as Slider;
  169. if (slider != null)
  170. {
  171. slider.Tag = "false";
  172. }
  173. }
  174. private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  175. {
  176. Slider slider = sender as Slider;
  177. if (slider != null && slider.Tag != null && slider.Tag.ToString() == "false")
  178. {
  179. return;
  180. }
  181. GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  182. if (textArea != null)
  183. {
  184. Rect oldRect = DataConversionForWPF.CRectConversionForRect(textArea.GetFrame());
  185. if (textArea.SetCharsFontSize((float)slider.Value, false))
  186. {
  187. PDFEditHistory editHistory = new PDFEditHistory();
  188. editHistory.EditPage = editPage;
  189. if (pdfPage != null)
  190. {
  191. editHistory.PageIndex = pdfPage.PageIndex;
  192. }
  193. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  194. ToolView.UpDateRender(oldRect, textArea);
  195. editPage.EndEdit();
  196. }
  197. }
  198. if (EditEvent != null && textArea == null)
  199. {
  200. EditEvent.FontSize = slider.Value;
  201. DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
  202. defaultSettingParam.SetPDFEditParamm(EditEvent);
  203. }
  204. }
  205. private void PDFTextEditControl_Loaded(object sender, RoutedEventArgs e)
  206. {
  207. TextStyleUI.TextFontChanged -= TextStyleUI_TextFontChanged;
  208. TextStyleUI.TextBoldChanged -= TextStyleUI_TextBoldChanged;
  209. TextStyleUI.TextItalicChanged -= TextStyleUI_TextItalicChanged;
  210. TextStyleUI.TextSizeChanged -= TextStyleUI_TextSizeChanged;
  211. TextAlignUI.TextAlignChanged -= TextAlignUI_TextAlignChanged;
  212. FontColorUI.ColorChanged -= FontColorUI_ColorChanged;
  213. TextStyleUI.TextFontChanged += TextStyleUI_TextFontChanged;
  214. TextStyleUI.TextBoldChanged += TextStyleUI_TextBoldChanged;
  215. TextStyleUI.TextItalicChanged += TextStyleUI_TextItalicChanged;
  216. TextStyleUI.TextSizeChanged += TextStyleUI_TextSizeChanged;
  217. TextAlignUI.TextAlignChanged += TextAlignUI_TextAlignChanged;
  218. FontColorUI.ColorChanged += FontColorUI_ColorChanged;
  219. }
  220. private void TextStyleUI_TextSizeChanged(object sender, double e)
  221. {
  222. GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  223. if (textArea != null)
  224. {
  225. Rect oldRect = DataConversionForWPF.CRectConversionForRect(textArea.GetFrame());
  226. if (textArea.SetCharsFontSize((float)e, false))
  227. {
  228. PDFEditHistory editHistory = new PDFEditHistory();
  229. editHistory.EditPage = editPage;
  230. if (pdfPage != null)
  231. {
  232. editHistory.PageIndex = pdfPage.PageIndex;
  233. }
  234. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  235. ToolView.UpDateRender(oldRect, textArea);
  236. editPage.EndEdit();
  237. }
  238. }
  239. if (EditEvent != null && textArea == null)
  240. {
  241. EditEvent.FontSize = e;
  242. DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
  243. defaultSettingParam.SetPDFEditParamm(EditEvent);
  244. }
  245. }
  246. private void FontColorUI_ColorChanged(object sender, EventArgs e)
  247. {
  248. SolidColorBrush newBrush = FontColorUI.Brush as SolidColorBrush;
  249. GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  250. if (textArea != null && newBrush != null)
  251. {
  252. Rect oldRect = DataConversionForWPF.CRectConversionForRect(textArea.GetFrame());
  253. if (textArea.SetCharsFontColor(newBrush.Color.R, newBrush.Color.G, newBrush.Color.B))
  254. {
  255. PDFEditHistory editHistory = new PDFEditHistory();
  256. editHistory.EditPage = editPage;
  257. if (pdfPage != null)
  258. {
  259. editHistory.PageIndex = pdfPage.PageIndex;
  260. }
  261. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  262. ToolView.UpDateRender(oldRect, textArea);
  263. editPage.EndEdit();
  264. }
  265. }
  266. if (EditEvent != null && textArea == null && newBrush != null)
  267. {
  268. byte[] Color = new byte[3];
  269. Color[0] = newBrush.Color.R;
  270. Color[1] = newBrush.Color.G;
  271. Color[2] = newBrush.Color.B;
  272. EditEvent.FontColor = Color;
  273. DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
  274. defaultSettingParam.SetPDFEditParamm(EditEvent);
  275. }
  276. }
  277. private void TextAlignUI_TextAlignChanged(object sender, TextAlignType e)
  278. {
  279. GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  280. if (textArea != null)
  281. {
  282. bool result = false;
  283. Rect oldRect = DataConversionForWPF.CRectConversionForRect(textArea.GetFrame());
  284. if (textArea.SelectLineRects != null && textArea.SelectLineRects.Count > 0)
  285. {
  286. result = textArea.SetTextRangeAlign(e);
  287. }
  288. else
  289. {
  290. result = textArea.SetTextAreaAlign(e);
  291. }
  292. if (result)
  293. {
  294. PDFEditHistory editHistory = new PDFEditHistory();
  295. editHistory.EditPage = editPage;
  296. if (pdfPage != null)
  297. {
  298. editHistory.PageIndex = pdfPage.PageIndex;
  299. }
  300. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  301. ToolView.UpDateRender(oldRect, textArea);
  302. editPage.EndEdit();
  303. }
  304. }
  305. if (EditEvent != null && textArea == null)
  306. {
  307. EditEvent.TextAlign = e;
  308. DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
  309. defaultSettingParam.SetPDFEditParamm(EditEvent);
  310. }
  311. }
  312. private void TextStyleUI_TextItalicChanged(object sender, bool e)
  313. {
  314. GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  315. if (textArea != null)
  316. {
  317. Rect oldRect = DataConversionForWPF.CRectConversionForRect(textArea.GetFrame());
  318. if (textArea.SetCharsFontItalic(e))
  319. {
  320. PDFEditHistory editHistory = new PDFEditHistory();
  321. editHistory.EditPage = editPage;
  322. if (pdfPage != null)
  323. {
  324. editHistory.PageIndex = pdfPage.PageIndex;
  325. }
  326. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  327. ToolView.UpDateRender(oldRect, textArea);
  328. editPage.EndEdit();
  329. }
  330. }
  331. if (EditEvent != null && textArea == null)
  332. {
  333. EditEvent.IsItalic = e;
  334. DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
  335. defaultSettingParam.SetPDFEditParamm(EditEvent);
  336. }
  337. }
  338. private void TextStyleUI_TextBoldChanged(object sender, bool e)
  339. {
  340. GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  341. if (textArea != null)
  342. {
  343. Rect oldRect = DataConversionForWPF.CRectConversionForRect(textArea.GetFrame());
  344. if (textArea.SetCharsFontBold(e))
  345. {
  346. PDFEditHistory editHistory = new PDFEditHistory();
  347. editHistory.EditPage = editPage;
  348. if (pdfPage != null)
  349. {
  350. editHistory.PageIndex = pdfPage.PageIndex;
  351. }
  352. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  353. ToolView.UpDateRender(oldRect, textArea);
  354. editPage.EndEdit();
  355. }
  356. }
  357. if (EditEvent != null && textArea == null)
  358. {
  359. EditEvent.IsBold = e;
  360. DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
  361. defaultSettingParam.SetPDFEditParamm(EditEvent);
  362. }
  363. }
  364. private void TextStyleUI_TextFontChanged(object sender, string e)
  365. {
  366. GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  367. if (textArea != null)
  368. {
  369. Rect oldRect = DataConversionForWPF.CRectConversionForRect(textArea.GetFrame());
  370. if (textArea.SetCharsFontName(e))
  371. {
  372. PDFEditHistory editHistory = new PDFEditHistory();
  373. editHistory.EditPage = editPage;
  374. if (pdfPage != null)
  375. {
  376. editHistory.PageIndex = pdfPage.PageIndex;
  377. }
  378. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  379. ToolView.UpDateRender(oldRect, textArea);
  380. editPage.EndEdit();
  381. }
  382. }
  383. if (EditEvent != null && textArea == null)
  384. {
  385. EditEvent.FontName = e;
  386. DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
  387. defaultSettingParam.SetPDFEditParamm(EditEvent);
  388. }
  389. }
  390. private void OpacityComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  391. {
  392. ComboBoxItem selectItem = OpacityComboBox.SelectedItem as ComboBoxItem;
  393. if (selectItem != null && selectItem.Content != null)
  394. {
  395. if (double.TryParse(selectItem.Content.ToString().TrimEnd('%'), out double newOpacity))
  396. {
  397. OpacityTextBox.Text = selectItem.Content.ToString();
  398. FontOpacitySlider.Value = newOpacity / 100.0;
  399. }
  400. }
  401. }
  402. private void GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage)
  403. {
  404. textArea = null;
  405. editPage = null;
  406. pdfPage = null;
  407. if (ToolView == null)
  408. {
  409. return;
  410. }
  411. if (EditEvent != null)
  412. {
  413. try
  414. {
  415. CPDFViewer pdfViewer = ToolView.GetCPDFViewer();
  416. CPDFDocument pdfDoc = pdfViewer.GetDocument();
  417. pdfPage = pdfDoc.PageAtIndex(EditEvent.PageIndex);
  418. editPage = pdfPage.GetEditPage();
  419. List<CPDFEditArea> editAreas = editPage.GetEditAreaList();
  420. if (editAreas != null && editAreas.Count > EditEvent.EditIndex)
  421. {
  422. textArea = editAreas[EditEvent.EditIndex] as CPDFEditTextArea;
  423. }
  424. }
  425. catch (Exception ex)
  426. {
  427. }
  428. }
  429. else
  430. {
  431. CPDFViewer pdfViewer = ToolView.GetCPDFViewer();
  432. CPDFDocument pdfDoc = pdfViewer.GetDocument();
  433. pdfPage = pdfDoc.PageAtIndex(0);
  434. editPage = pdfPage.GetEditPage();
  435. editPage.BeginEdit(CPDFEditType.EditText);
  436. editPage.EndEdit();
  437. }
  438. }
  439. }
  440. }