PDFTextEditControl.xaml.cs 35 KB

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