PDFTextEditControl.xaml.cs 34 KB

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