PDFTextEditControl.xaml.cs 19 KB

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