PDFTextEditControl.xaml.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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.Controls.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. using System.ComponentModel;
  17. using System.Runtime.CompilerServices;
  18. namespace ComPDFKit.Controls.Edit
  19. {
  20. public partial class PDFTextEditControl : UserControl, INotifyPropertyChanged
  21. {
  22. #region Property
  23. public CPDFViewerTool ToolView { get; private set; }
  24. public TextEditParam EditEvent { get; set; }
  25. public event PropertyChangedEventHandler PropertyChanged;
  26. private bool _isMultiSelected;
  27. public bool IsMultiSelected
  28. {
  29. get => _isMultiSelected;
  30. set
  31. {
  32. if (UpdateProper(ref _isMultiSelected, value))
  33. {
  34. ToolView.GetCPDFViewer().UpdateRenderFrame();
  35. }
  36. }
  37. }
  38. private bool _showBorder;
  39. public bool ShowBorder
  40. {
  41. get => _showBorder;
  42. set
  43. {
  44. UpdateProper(ref _showBorder, value);
  45. }
  46. }
  47. #endregion
  48. public PDFTextEditControl()
  49. {
  50. DataContext = this;
  51. InitializeComponent();
  52. Loaded += PDFTextEditControl_Loaded;
  53. }
  54. #region Init PDFView
  55. public void InitWithPDFViewer(CPDFViewerTool newPDFView)
  56. {
  57. ToolView = newPDFView;
  58. }
  59. #endregion
  60. #region UI
  61. public void SetPDFTextEditData(TextEditParam newEvent)
  62. {
  63. if (newEvent.EditIndex < 0)
  64. {
  65. EditEvent = null;
  66. }
  67. else
  68. {
  69. EditEvent = newEvent;
  70. }
  71. if (newEvent != null && newEvent.EditType == CPDFEditType.EditText)
  72. {
  73. GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  74. List<string> sysfontList = new List<string>();
  75. if (textArea != null)
  76. {
  77. sysfontList = textArea.GetFontList();
  78. }
  79. if (sysfontList.Count == 0)
  80. {
  81. sysfontList.Add("Helvetica");
  82. sysfontList.Add("Courier New");
  83. sysfontList.Add("Times New Roman");
  84. }
  85. if (sysfontList.Contains(newEvent.FontName) == false && string.IsNullOrEmpty(newEvent.FontName) == false)
  86. {
  87. sysfontList.Add(newEvent.FontName);
  88. }
  89. TextStyleUI.SetFontNames(sysfontList);
  90. TextStyleUI.SelectFontName(newEvent.FontName);
  91. TextStyleUI.SetFontStyle(newEvent.IsBold, newEvent.IsItalic);
  92. TextStyleUI.SetFontSize(newEvent.FontSize);
  93. OpacityTextBox.Text = string.Format("{0}%", (int)(Math.Ceiling(newEvent.Transparency * 100 / 255D)));
  94. FontOpacitySlider.Value = ((int)(Math.Ceiling(newEvent.Transparency * 100 / 255D))) / 100D;
  95. TextAlignUI.SetFontAlign(newEvent.TextAlign);
  96. if (newEvent.FontColor != null && newEvent.FontColor.Length == 3)
  97. {
  98. FontColorUI.SetCheckedForColor(Color.FromRgb(
  99. newEvent.FontColor[0],
  100. newEvent.FontColor[1],
  101. newEvent.FontColor[2]));
  102. }
  103. }
  104. EditEvent = newEvent;
  105. }
  106. //public void SetPDFTextMultiEditData(List<PDFEditEvent> editEvents)
  107. //{
  108. // EditEvent = null;
  109. // EditMultiEvents = null;
  110. // if(editEvents!=null && editEvents.Count>0)
  111. // {
  112. // PDFEditEvent editEvent= editEvents[0];
  113. // if (editEvent != null && editEvent.EditType == CPDFEditType.EditText)
  114. // {
  115. // if (editEvent.SystemFontNameList != null && editEvent.SystemFontNameList.Count == 0)
  116. // {
  117. // editEvent.SystemFontNameList.Add("Helvetica");
  118. // editEvent.SystemFontNameList.Add("Courier New");
  119. // editEvent.SystemFontNameList.Add("Times New Roman");
  120. // }
  121. // if (editEvent.SystemFontNameList.Contains(editEvent.FontName) == false && string.IsNullOrEmpty(editEvent.FontName) == false)
  122. // {
  123. // editEvent.SystemFontNameList.Add(editEvent.FontName);
  124. // }
  125. // TextStyleUI.SetFontNames(editEvent.SystemFontNameList);
  126. // TextStyleUI.SelectFontName(editEvent.FontName);
  127. // TextStyleUI.SetFontStyle(editEvent.IsBold, editEvent.IsItalic);
  128. // TextStyleUI.SetFontSize(editEvent.FontSize);
  129. // OpacityTextBox.Text = string.Format("{0}%", (int)(Math.Ceiling(editEvent.Transparency * 100 / 255D)));
  130. // FontOpacitySlider.Value = ((int)(Math.Ceiling(editEvent.Transparency * 100 / 255D))) / 100D;
  131. // TextAlignUI.SetFontAlign(editEvent.TextAlign);
  132. // FontColorUI.SetCheckedForColor(editEvent.FontColor);
  133. // }
  134. // }
  135. // EditMultiEvents=editEvents;
  136. //}
  137. private void SliderOpacity_DragCompleted(object sender, DragCompletedEventArgs e)
  138. {
  139. Slider slider = sender as Slider;
  140. if (slider != null)
  141. {
  142. slider.Tag = "true";
  143. }
  144. GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  145. if (textArea != null)
  146. {
  147. Rect oldRect = DataConversionForWPF.CRectConversionForRect(textArea.GetFrame());
  148. bool result;
  149. if (string.IsNullOrEmpty(textArea.SelectText))
  150. {
  151. string fontName = "Helvetica";
  152. float fontSize = 14;
  153. byte[] fontColor = { 0, 0, 0 };
  154. byte transparency = 255;
  155. bool isBold = false;
  156. bool isItalic = false;
  157. textArea.GetTextStyle(ref fontName, ref fontSize, ref fontColor, ref transparency, ref isBold, ref isItalic);
  158. result = textArea.SetCurTextStyle(fontName, fontSize, fontColor[0], fontColor[1], fontColor[2], (byte)(FontOpacitySlider.Value * 255), isBold, isItalic);
  159. }
  160. else
  161. {
  162. result = textArea.SetCharsFontTransparency((byte)(FontOpacitySlider.Value * 255));
  163. }
  164. if (result)
  165. {
  166. PDFEditHistory editHistory = new PDFEditHistory();
  167. editHistory.EditPage = editPage;
  168. if (pdfPage != null)
  169. {
  170. editHistory.PageIndex = pdfPage.PageIndex;
  171. }
  172. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  173. ToolView.UpdateRender(oldRect, textArea);
  174. editPage.EndEdit();
  175. }
  176. }
  177. if (EditEvent != null && textArea == null)
  178. {
  179. EditEvent.Transparency = (byte)(FontOpacitySlider.Value * 255);
  180. DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
  181. defaultSettingParam.SetPDFEditParamm(EditEvent);
  182. }
  183. }
  184. private void SliderOpacity_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  185. {
  186. Slider slider = sender as Slider;
  187. if (OpacityTextBox != null && FontOpacitySlider != null)
  188. {
  189. OpacityTextBox.Text = string.Format("{0}%", (int)(FontOpacitySlider.Value * 100));
  190. }
  191. if (slider != null && slider.Tag != null && slider.Tag.ToString() == "false")
  192. {
  193. return;
  194. }
  195. GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  196. if (textArea != null)
  197. {
  198. Rect oldRect = DataConversionForWPF.CRectConversionForRect(textArea.GetFrame());
  199. bool result;
  200. if (string.IsNullOrEmpty(textArea.SelectText))
  201. {
  202. string fontName = "Helvetica";
  203. float fontSize = 14;
  204. byte[] fontColor = { 0, 0, 0 };
  205. byte transparency = 255;
  206. bool isBold = false;
  207. bool isItalic = false;
  208. textArea.GetTextStyle(ref fontName, ref fontSize, ref fontColor, ref transparency, ref isBold, ref isItalic);
  209. result = textArea.SetCurTextStyle(fontName, fontSize, fontColor[0], fontColor[1], fontColor[2], (byte)(FontOpacitySlider.Value * 255), isBold, isItalic);
  210. }
  211. else
  212. {
  213. result = textArea.SetCharsFontTransparency((byte)(FontOpacitySlider.Value * 255));
  214. }
  215. if (result)
  216. {
  217. PDFEditHistory editHistory = new PDFEditHistory();
  218. editHistory.EditPage = editPage;
  219. if (pdfPage != null)
  220. {
  221. editHistory.PageIndex = pdfPage.PageIndex;
  222. }
  223. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  224. ToolView.UpdateRender(oldRect, textArea);
  225. editPage.EndEdit();
  226. }
  227. }
  228. if (EditEvent != null && textArea == null)
  229. {
  230. EditEvent.Transparency = (byte)(FontOpacitySlider.Value * 255);
  231. DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
  232. defaultSettingParam.SetPDFEditParamm(EditEvent);
  233. }
  234. }
  235. private void Slider_DragStarted(object sender, DragStartedEventArgs e)
  236. {
  237. Slider slider = sender as Slider;
  238. if (slider != null)
  239. {
  240. slider.Tag = "false";
  241. }
  242. }
  243. #endregion
  244. #region Loaded
  245. private void PDFTextEditControl_Loaded(object sender, RoutedEventArgs e)
  246. {
  247. TextStyleUI.TextFontChanged -= TextStyleUI_TextFontChanged;
  248. TextStyleUI.TextBoldChanged -= TextStyleUI_TextBoldChanged;
  249. TextStyleUI.TextItalicChanged -= TextStyleUI_TextItalicChanged;
  250. TextStyleUI.TextSizeChanged -= TextStyleUI_TextSizeChanged;
  251. TextAlignUI.TextAlignChanged -= TextAlignUI_TextAlignChanged;
  252. FontColorUI.ColorChanged -= FontColorUI_ColorChanged;
  253. TextStyleUI.TextFontChanged += TextStyleUI_TextFontChanged;
  254. TextStyleUI.TextBoldChanged += TextStyleUI_TextBoldChanged;
  255. TextStyleUI.TextItalicChanged += TextStyleUI_TextItalicChanged;
  256. TextStyleUI.TextSizeChanged += TextStyleUI_TextSizeChanged;
  257. TextAlignUI.TextAlignChanged += TextAlignUI_TextAlignChanged;
  258. FontColorUI.ColorChanged += FontColorUI_ColorChanged;
  259. IsMultiSelected = ToolView.GetIsMultiSelected();
  260. ShowBorder = ToolView.GetEditPen() == null || ToolView.GetEditPen().Thickness != 0;
  261. }
  262. #endregion
  263. #region Property changed
  264. private void TextStyleUI_TextSizeChanged(object sender, double e)
  265. {
  266. GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  267. if (textArea != null)
  268. {
  269. Rect oldRect = DataConversionForWPF.CRectConversionForRect(textArea.GetFrame());
  270. bool result;
  271. if (string.IsNullOrEmpty(textArea.SelectText))
  272. {
  273. string fontName = "Helvetica";
  274. float fontSize = 14;
  275. byte[] fontColor = { 0, 0, 0 };
  276. byte transparency = 255;
  277. bool isBold = false;
  278. bool isItalic = false;
  279. textArea.GetTextStyle(ref fontName, ref fontSize, ref fontColor, ref transparency, ref isBold, ref isItalic);
  280. result = textArea.SetCurTextStyle(fontName, (float)e, fontColor[0], fontColor[1], fontColor[2], transparency, isBold, isItalic);
  281. }
  282. else
  283. {
  284. result = textArea.SetCharsFontSize((float)e, true);
  285. }
  286. if (result)
  287. {
  288. PDFEditHistory editHistory = new PDFEditHistory();
  289. editHistory.EditPage = editPage;
  290. if (pdfPage != null)
  291. {
  292. editHistory.PageIndex = pdfPage.PageIndex;
  293. }
  294. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  295. ToolView.UpdateRender(oldRect, textArea);
  296. editPage.EndEdit();
  297. }
  298. }
  299. if (EditEvent != null && textArea == null)
  300. {
  301. EditEvent.FontSize = e;
  302. DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
  303. defaultSettingParam.SetPDFEditParamm(EditEvent);
  304. }
  305. //if (EditMultiEvents != null)
  306. //{
  307. // foreach (PDFEditEvent editEvent in EditMultiEvents)
  308. // {
  309. // editEvent.FontSize = e;
  310. // }
  311. // PDFEditEvent.UpdatePDFEditList(EditMultiEvents);
  312. //}
  313. }
  314. private void FontColorUI_ColorChanged(object sender, EventArgs e)
  315. {
  316. SolidColorBrush newBrush = FontColorUI.Brush as SolidColorBrush;
  317. GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  318. if (textArea != null && newBrush != null)
  319. {
  320. Rect oldRect = DataConversionForWPF.CRectConversionForRect(textArea.GetFrame());
  321. bool result;
  322. if (string.IsNullOrEmpty(textArea.SelectText))
  323. {
  324. string fontName = "Helvetica";
  325. float fontSize = 14;
  326. byte[] fontColor = { 0, 0, 0 };
  327. byte transparency = 255;
  328. bool isBold = false;
  329. bool isItalic = false;
  330. textArea.GetTextStyle(ref fontName, ref fontSize, ref fontColor, ref transparency, ref isBold, ref isItalic);
  331. result = textArea.SetCurTextStyle(fontName, fontSize, newBrush.Color.R, newBrush.Color.G, newBrush.Color.B, transparency, isBold, isItalic);
  332. }
  333. else
  334. {
  335. result = textArea.SetCharsFontColor(newBrush.Color.R, newBrush.Color.G, newBrush.Color.B);
  336. }
  337. if (result)
  338. {
  339. PDFEditHistory editHistory = new PDFEditHistory();
  340. editHistory.EditPage = editPage;
  341. if (pdfPage != null)
  342. {
  343. editHistory.PageIndex = pdfPage.PageIndex;
  344. }
  345. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  346. ToolView.UpdateRender(oldRect, textArea);
  347. editPage.EndEdit();
  348. }
  349. }
  350. if (EditEvent != null && textArea == null && newBrush != null)
  351. {
  352. byte[] Color = new byte[3];
  353. Color[0] = newBrush.Color.R;
  354. Color[1] = newBrush.Color.G;
  355. Color[2] = newBrush.Color.B;
  356. EditEvent.FontColor = Color;
  357. DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
  358. defaultSettingParam.SetPDFEditParamm(EditEvent);
  359. }
  360. }
  361. private void TextAlignUI_TextAlignChanged(object sender, TextAlignType e)
  362. {
  363. GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  364. if (textArea != null)
  365. {
  366. bool result = false;
  367. Rect oldRect = DataConversionForWPF.CRectConversionForRect(textArea.GetFrame());
  368. if (textArea.SelectLineRects != null && textArea.SelectLineRects.Count > 0)
  369. {
  370. result = textArea.SetTextRangeAlign(e);
  371. }
  372. else
  373. {
  374. result = textArea.SetTextAreaAlign(e);
  375. }
  376. if (result)
  377. {
  378. PDFEditHistory editHistory = new PDFEditHistory();
  379. editHistory.EditPage = editPage;
  380. if (pdfPage != null)
  381. {
  382. editHistory.PageIndex = pdfPage.PageIndex;
  383. }
  384. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  385. ToolView.UpdateRender(oldRect, textArea);
  386. editPage.EndEdit();
  387. }
  388. }
  389. if (EditEvent != null && textArea == null)
  390. {
  391. EditEvent.TextAlign = e;
  392. DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
  393. defaultSettingParam.SetPDFEditParamm(EditEvent);
  394. }
  395. //if (EditMultiEvents != null)
  396. //{
  397. // foreach (PDFEditEvent editEvent in EditMultiEvents)
  398. // {
  399. // editEvent.TextAlign = e;
  400. // }
  401. // PDFEditEvent.UpdatePDFEditList(EditMultiEvents);
  402. //}
  403. }
  404. private void TextStyleUI_TextItalicChanged(object sender, bool e)
  405. {
  406. GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  407. if (textArea != null)
  408. {
  409. Rect oldRect = DataConversionForWPF.CRectConversionForRect(textArea.GetFrame());
  410. bool result;
  411. if (string.IsNullOrEmpty(textArea.SelectText))
  412. {
  413. string fontName = "Helvetica";
  414. float fontSize = 14;
  415. byte[] fontColor = { 0, 0, 0 };
  416. byte transparency = 255;
  417. bool isBold = false;
  418. bool isItalic = false;
  419. textArea.GetTextStyle(ref fontName, ref fontSize, ref fontColor, ref transparency, ref isBold, ref isItalic);
  420. result = textArea.SetCurTextStyle(fontName, fontSize, fontColor[0], fontColor[1], fontColor[2], transparency, isBold, e);
  421. }
  422. else
  423. {
  424. result = textArea.SetCharsFontItalic(e);
  425. }
  426. if (result)
  427. {
  428. PDFEditHistory editHistory = new PDFEditHistory();
  429. editHistory.EditPage = editPage;
  430. if (pdfPage != null)
  431. {
  432. editHistory.PageIndex = pdfPage.PageIndex;
  433. }
  434. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  435. ToolView.UpdateRender(oldRect, textArea);
  436. editPage.EndEdit();
  437. }
  438. }
  439. if (EditEvent != null && textArea == null)
  440. {
  441. EditEvent.IsItalic = e;
  442. DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
  443. defaultSettingParam.SetPDFEditParamm(EditEvent);
  444. }
  445. //if (EditMultiEvents != null)
  446. //{
  447. // foreach (PDFEditEvent editEvent in EditMultiEvents)
  448. // {
  449. // editEvent.IsItalic = e;
  450. // }
  451. // PDFEditEvent.UpdatePDFEditList(EditMultiEvents);
  452. //}
  453. }
  454. private void TextStyleUI_TextBoldChanged(object sender, bool e)
  455. {
  456. GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  457. if (textArea != null)
  458. {
  459. Rect oldRect = DataConversionForWPF.CRectConversionForRect(textArea.GetFrame());
  460. bool result;
  461. if (string.IsNullOrEmpty(textArea.SelectText))
  462. {
  463. string fontName = "Helvetica";
  464. float fontSize = 14;
  465. byte[] fontColor = { 0, 0, 0 };
  466. byte transparency = 255;
  467. bool isBold = false;
  468. bool isItalic = false;
  469. textArea.GetTextStyle(ref fontName, ref fontSize, ref fontColor, ref transparency, ref isBold, ref isItalic);
  470. result = textArea.SetCurTextStyle(fontName, fontSize, fontColor[0], fontColor[1], fontColor[2], transparency, e, isItalic);
  471. }
  472. else
  473. {
  474. result = textArea.SetCharsFontBold(e);
  475. }
  476. if (result)
  477. {
  478. PDFEditHistory editHistory = new PDFEditHistory();
  479. editHistory.EditPage = editPage;
  480. if (pdfPage != null)
  481. {
  482. editHistory.PageIndex = pdfPage.PageIndex;
  483. }
  484. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  485. ToolView.UpdateRender(oldRect, textArea);
  486. editPage.EndEdit();
  487. }
  488. }
  489. if (EditEvent != null && textArea == null)
  490. {
  491. EditEvent.IsBold = e;
  492. DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
  493. defaultSettingParam.SetPDFEditParamm(EditEvent);
  494. }
  495. //if (EditMultiEvents != null)
  496. //{
  497. // foreach (PDFEditEvent editEvent in EditMultiEvents)
  498. // {
  499. // editEvent.IsBold = e;
  500. // }
  501. // PDFEditEvent.UpdatePDFEditList(EditMultiEvents);
  502. //}
  503. }
  504. private void TextStyleUI_TextFontChanged(object sender, string e)
  505. {
  506. GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  507. if (textArea != null)
  508. {
  509. Rect oldRect = DataConversionForWPF.CRectConversionForRect(textArea.GetFrame());
  510. bool result;
  511. if (string.IsNullOrEmpty(textArea.SelectText))
  512. {
  513. string fontName = "Helvetica";
  514. float fontSize = 14;
  515. byte[] fontColor = { 0, 0, 0 };
  516. byte transparency = 255;
  517. bool isBold = false;
  518. bool isItalic = false;
  519. textArea.GetTextStyle(ref fontName, ref fontSize, ref fontColor, ref transparency, ref isBold, ref isItalic);
  520. result = textArea.SetCurTextStyle(e, fontSize, fontColor[0], fontColor[1], fontColor[2], transparency, isBold, isItalic);
  521. }
  522. else
  523. {
  524. result = textArea.SetCharsFontName(e);
  525. }
  526. if (result)
  527. {
  528. PDFEditHistory editHistory = new PDFEditHistory();
  529. editHistory.EditPage = editPage;
  530. if (pdfPage != null)
  531. {
  532. editHistory.PageIndex = pdfPage.PageIndex;
  533. }
  534. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  535. ToolView.UpdateRender(oldRect, textArea);
  536. editPage.EndEdit();
  537. }
  538. }
  539. if (EditEvent != null && textArea == null)
  540. {
  541. EditEvent.FontName = e;
  542. DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
  543. defaultSettingParam.SetPDFEditParamm(EditEvent);
  544. }
  545. //if (EditMultiEvents != null)
  546. //{
  547. // foreach (PDFEditEvent editEvent in EditMultiEvents)
  548. // {
  549. // editEvent.FontName = e;
  550. // }
  551. // PDFEditEvent.UpdatePDFEditList(EditMultiEvents);
  552. //}
  553. }
  554. private void OpacityComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  555. {
  556. ComboBoxItem selectItem = OpacityComboBox.SelectedItem as ComboBoxItem;
  557. if (selectItem != null && selectItem.Content != null)
  558. {
  559. if (double.TryParse(selectItem.Content.ToString().TrimEnd('%'), out double newOpacity))
  560. {
  561. OpacityTextBox.Text = selectItem.Content.ToString();
  562. FontOpacitySlider.Value = newOpacity / 100.0;
  563. }
  564. }
  565. }
  566. #endregion
  567. #region Text Edit
  568. private void GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage)
  569. {
  570. textArea = null;
  571. editPage = null;
  572. pdfPage = null;
  573. if (ToolView == null)
  574. {
  575. return;
  576. }
  577. if (EditEvent != null)
  578. {
  579. try
  580. {
  581. CPDFViewer pdfViewer = ToolView.GetCPDFViewer();
  582. CPDFDocument pdfDoc = pdfViewer.GetDocument();
  583. pdfPage = pdfDoc.PageAtIndex(EditEvent.PageIndex);
  584. editPage = pdfPage.GetEditPage();
  585. List<CPDFEditArea> editAreas = editPage.GetEditAreaList();
  586. if (editAreas != null && editAreas.Count > EditEvent.EditIndex)
  587. {
  588. textArea = editAreas[EditEvent.EditIndex] as CPDFEditTextArea;
  589. }
  590. }
  591. catch (Exception ex)
  592. {
  593. }
  594. }
  595. else
  596. {
  597. CPDFViewer pdfViewer = ToolView.GetCPDFViewer();
  598. CPDFDocument pdfDoc = pdfViewer.GetDocument();
  599. pdfPage = pdfDoc.PageAtIndex(0);
  600. editPage = pdfPage.GetEditPage();
  601. editPage.BeginEdit(CPDFEditType.EditText);
  602. editPage.EndEdit();
  603. }
  604. }
  605. #endregion
  606. private void chkMulti_Click(object sender, RoutedEventArgs e)
  607. {
  608. ToolView.SetIsMultiSelected((e.Source as CheckBox).IsChecked.GetValueOrDefault());
  609. }
  610. private void chkEditPen_Click(object sender, RoutedEventArgs e)
  611. {
  612. if ((e.Source as CheckBox).IsChecked.GetValueOrDefault())
  613. {
  614. ToolView.SetEditPen(null);
  615. }
  616. else
  617. {
  618. ToolView.SetEditPen(new Pen()
  619. {
  620. Brush = new SolidColorBrush(Colors.Black),
  621. Thickness = 0
  622. });
  623. }
  624. ShowBorder = ToolView.GetEditPen() == null || ToolView.GetEditPen().Thickness != 0;
  625. ToolView.GetCPDFViewer().UpdateRenderFrame();
  626. }
  627. protected bool UpdateProper<T>(ref T properValue,
  628. T newValue,
  629. [CallerMemberName] string properName = "")
  630. {
  631. if (object.Equals(properValue, newValue))
  632. return false;
  633. properValue = newValue;
  634. OnPropertyChanged(properName);
  635. return true;
  636. }
  637. protected void OnPropertyChanged([CallerMemberName] string propertyName = "") =>
  638. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  639. }
  640. }