PDFTextEditControl.xaml.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  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. TextMarkupUI.TextUnderlineChanged -= TextMarkupUI_TextUnderlineChanged;
  266. TextMarkupUI.TextStrikethroughChanged -= TextMarkupUI_TextStrikethroughChanged;
  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. TextMarkupUI.TextUnderlineChanged += TextMarkupUI_TextUnderlineChanged;
  274. TextMarkupUI.TextStrikethroughChanged += TextMarkupUI_TextStrikethroughChanged;
  275. IsMultiSelected = ToolView.GetIsMultiSelected();
  276. ShowBorder = ToolView.GetEditPen() == null || ToolView.GetEditPen().Thickness != 0;
  277. }
  278. #endregion
  279. #region Property changed
  280. private void TextStyleUI_TextSizeChanged(object sender, double e)
  281. {
  282. GetTextArea(out List<CPDFEditTextArea> textAreas, out CPDFPage pdfPage, out CPDFEditPage editPage);
  283. if (textAreas.Count == 0 || pdfPage == null || editPage == null)
  284. return;
  285. if (ToolView.CurrentEditAreaObject() != null)
  286. {
  287. bool result;
  288. Rect oldRect = DataConversionForWPF.CRectConversionForRect(textAreas[0].GetFrame());
  289. if (string.IsNullOrEmpty(textAreas[0].SelectText))
  290. {
  291. string fontName = "Helvetica";
  292. float fontSize = 14;
  293. byte[] fontColor = { 0, 0, 0 };
  294. byte transparency = 255;
  295. bool isBold = false;
  296. bool isItalic = false;
  297. textAreas[0].GetTextStyle(ref fontName, ref fontSize, ref fontColor, ref transparency, ref isBold, ref isItalic);
  298. result = textAreas[0].SetCurTextStyle(fontName, (float)e, fontColor[0], fontColor[1], fontColor[2], transparency, isBold, isItalic);
  299. }
  300. else
  301. {
  302. result = textAreas[0].SetCharsFontSize((float)e, true);
  303. }
  304. if (result)
  305. {
  306. PDFEditHistory editHistory = new PDFEditHistory();
  307. editHistory.EditPage = editPage;
  308. editHistory.PageIndex = pdfPage.PageIndex;
  309. ToolView.GetCPDFViewer().UndoManager.AddHistory(editHistory);
  310. ToolView.UpdateRender(oldRect, textAreas[0]);
  311. }
  312. }
  313. else
  314. {
  315. GroupHistory groupHistory = new GroupHistory();
  316. foreach (CPDFEditTextArea textArea in textAreas)
  317. {
  318. if (textArea.SetCharsFontSize((float)e, true))
  319. {
  320. PDFEditHistory editHistory = new PDFEditHistory();
  321. editHistory.EditPage = editPage;
  322. editHistory.PageIndex = pdfPage.PageIndex;
  323. groupHistory.Histories.Add(editHistory);
  324. }
  325. }
  326. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(groupHistory);
  327. ToolView.GetCPDFViewer()?.UpdateRenderFrame();
  328. }
  329. editPage.EndEdit();
  330. if (EditEvents.Count > 0 && textAreas.Count > 0)
  331. {
  332. EditEvents.FirstOrDefault().FontSize = e;
  333. DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
  334. defaultSettingParam.SetPDFEditParamm(EditEvents.FirstOrDefault());
  335. }
  336. //if (EditMultiEvents != null)
  337. //{
  338. // foreach (PDFEditEvent editEvent in EditMultiEvents)
  339. // {
  340. // editEvent.FontSize = e;
  341. // }
  342. // PDFEditEvent.UpdatePDFEditList(EditMultiEvents);
  343. //}
  344. }
  345. private void FontColorUI_ColorChanged(object sender, EventArgs e)
  346. {
  347. SolidColorBrush newBrush = FontColorUI.Brush as SolidColorBrush;
  348. GetTextArea(out List<CPDFEditTextArea> textAreas, out CPDFPage pdfPage, out CPDFEditPage editPage);
  349. if (textAreas.Count == 0 || pdfPage == null || editPage == null)
  350. return;
  351. if (ToolView.CurrentEditAreaObject() != null)
  352. {
  353. bool result;
  354. Rect oldRect = DataConversionForWPF.CRectConversionForRect(textAreas[0].GetFrame());
  355. if (string.IsNullOrEmpty(textAreas[0].SelectText))
  356. {
  357. string fontName = "Helvetica";
  358. float fontSize = 14;
  359. byte[] fontColor = { 0, 0, 0 };
  360. byte transparency = 255;
  361. bool isBold = false;
  362. bool isItalic = false;
  363. textAreas[0].GetTextStyle(ref fontName, ref fontSize, ref fontColor, ref transparency, ref isBold, ref isItalic);
  364. result = textAreas[0].SetCurTextStyle(fontName, fontSize, newBrush.Color.R, newBrush.Color.G, newBrush.Color.B, transparency, isBold, isItalic);
  365. }
  366. else
  367. {
  368. result = textAreas[0].SetCharsFontColor(newBrush.Color.R, newBrush.Color.G, newBrush.Color.B);
  369. }
  370. if (result)
  371. {
  372. PDFEditHistory editHistory = new PDFEditHistory();
  373. editHistory.EditPage = editPage;
  374. editHistory.PageIndex = pdfPage.PageIndex;
  375. ToolView.GetCPDFViewer().UndoManager.AddHistory(editHistory);
  376. ToolView.UpdateRender(oldRect, textAreas[0]);
  377. }
  378. }
  379. else
  380. {
  381. GroupHistory groupHistory = new GroupHistory();
  382. foreach (CPDFEditTextArea textArea in textAreas)
  383. {
  384. if (textArea.SetCharsFontColor(newBrush.Color.R, newBrush.Color.G, newBrush.Color.B))
  385. {
  386. PDFEditHistory editHistory = new PDFEditHistory();
  387. editHistory.EditPage = editPage;
  388. editHistory.PageIndex = pdfPage.PageIndex;
  389. groupHistory.Histories.Add(editHistory);
  390. }
  391. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(groupHistory);
  392. ToolView.GetCPDFViewer()?.UpdateRenderFrame();
  393. }
  394. }
  395. editPage.EndEdit();
  396. if (EditEvents.Count > 0 && newBrush != null)
  397. {
  398. byte[] Color = new byte[3];
  399. Color[0] = newBrush.Color.R;
  400. Color[1] = newBrush.Color.G;
  401. Color[2] = newBrush.Color.B;
  402. EditEvents.FirstOrDefault().FontColor = Color;
  403. DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
  404. defaultSettingParam.SetPDFEditParamm(EditEvents.FirstOrDefault());
  405. }
  406. }
  407. private void TextAlignUI_TextAlignChanged(object sender, TextAlignType e)
  408. {
  409. GetTextArea(out List<CPDFEditTextArea> textAreas, out CPDFPage pdfPage, out CPDFEditPage editPage);
  410. if (textAreas.Count == 0 || pdfPage == null || editPage == null)
  411. return;
  412. if (ToolView.CurrentEditAreaObject() != null)
  413. {
  414. bool result;
  415. Rect oldRect = DataConversionForWPF.CRectConversionForRect(textAreas[0].GetFrame());
  416. if (textAreas[0].SelectLineRects != null && textAreas[0].SelectLineRects.Count > 0)
  417. {
  418. result = textAreas[0].SetTextRangeAlign(e);
  419. }
  420. else
  421. {
  422. result = textAreas[0].SetTextAreaAlign(e);
  423. }
  424. if (result)
  425. {
  426. PDFEditHistory editHistory = new PDFEditHistory();
  427. editHistory.EditPage = editPage;
  428. editHistory.PageIndex = pdfPage.PageIndex;
  429. ToolView.GetCPDFViewer().UndoManager.AddHistory(editHistory);
  430. ToolView.UpdateRender(oldRect, textAreas[0]);
  431. }
  432. }
  433. else
  434. {
  435. GroupHistory groupHistory = new GroupHistory();
  436. foreach (CPDFEditTextArea textArea in textAreas)
  437. {
  438. bool result;
  439. if (textArea.SelectLineRects != null && textArea.SelectLineRects.Count > 0)
  440. {
  441. result = textArea.SetTextRangeAlign(e);
  442. }
  443. else
  444. {
  445. result = textArea.SetTextAreaAlign(e);
  446. }
  447. if (result)
  448. {
  449. PDFEditHistory editHistory = new PDFEditHistory();
  450. editHistory.EditPage = editPage;
  451. editHistory.PageIndex = pdfPage.PageIndex;
  452. groupHistory.Histories.Add(editHistory);
  453. }
  454. }
  455. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(groupHistory);
  456. ToolView.GetCPDFViewer()?.UpdateRenderFrame();
  457. }
  458. editPage.EndEdit();
  459. if (EditEvents.Count > 0 && textAreas.Count > 0)
  460. {
  461. EditEvents.FirstOrDefault().TextAlign = e;
  462. DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
  463. defaultSettingParam.SetPDFEditParamm(EditEvents.FirstOrDefault());
  464. }
  465. //if (EditMultiEvents != null)
  466. //{
  467. // foreach (PDFEditEvent editEvent in EditMultiEvents)
  468. // {
  469. // editEvent.TextAlign = e;
  470. // }
  471. // PDFEditEvent.UpdatePDFEditList(EditMultiEvents);
  472. //}
  473. }
  474. private void TextStyleUI_TextItalicChanged(object sender, bool e)
  475. {
  476. GetTextArea(out List<CPDFEditTextArea> textAreas, out CPDFPage pdfPage, out CPDFEditPage editPage);
  477. if (textAreas.Count == 0 || pdfPage == null || editPage == null)
  478. return;
  479. if (ToolView.CurrentEditAreaObject() != null)
  480. {
  481. bool result;
  482. Rect oldRect = DataConversionForWPF.CRectConversionForRect(textAreas[0].GetFrame());
  483. if (string.IsNullOrEmpty(textAreas[0].SelectText))
  484. {
  485. string fontName = "Helvetica";
  486. float fontSize = 14;
  487. byte[] fontColor = { 0, 0, 0 };
  488. byte transparency = 255;
  489. bool isBold = false;
  490. bool isItalic = false;
  491. textAreas[0].GetTextStyle(ref fontName, ref fontSize, ref fontColor, ref transparency, ref isBold, ref isItalic);
  492. result = textAreas[0].SetCurTextStyle(fontName, fontSize, fontColor[0], fontColor[1], fontColor[2], transparency, isBold, e);
  493. }
  494. else
  495. {
  496. result = textAreas[0].SetCharsFontItalic(e);
  497. }
  498. if (result)
  499. {
  500. PDFEditHistory editHistory = new PDFEditHistory();
  501. editHistory.EditPage = editPage;
  502. editHistory.PageIndex = pdfPage.PageIndex;
  503. ToolView.GetCPDFViewer().UndoManager.AddHistory(editHistory);
  504. ToolView.UpdateRender(oldRect, textAreas[0]);
  505. }
  506. }
  507. else
  508. {
  509. GroupHistory groupHistory = new GroupHistory();
  510. foreach (CPDFEditTextArea textArea in textAreas)
  511. {
  512. if (textArea.SetCharsFontItalic(e))
  513. {
  514. PDFEditHistory editHistory = new PDFEditHistory();
  515. editHistory.EditPage = editPage;
  516. editHistory.PageIndex = pdfPage.PageIndex;
  517. groupHistory.Histories.Add(editHistory);
  518. }
  519. }
  520. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(groupHistory);
  521. ToolView.GetCPDFViewer()?.UpdateRenderFrame();
  522. }
  523. editPage.EndEdit();
  524. if (EditEvents.Count > 0 && textAreas.Count > 0)
  525. {
  526. EditEvents.FirstOrDefault().IsItalic = e;
  527. DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
  528. defaultSettingParam.SetPDFEditParamm(EditEvents.FirstOrDefault());
  529. }
  530. //if (EditMultiEvents != null)
  531. //{
  532. // foreach (PDFEditEvent editEvent in EditMultiEvents)
  533. // {
  534. // editEvent.IsItalic = e;
  535. // }
  536. // PDFEditEvent.UpdatePDFEditList(EditMultiEvents);
  537. //}
  538. }
  539. private void TextStyleUI_TextBoldChanged(object sender, bool e)
  540. {
  541. GetTextArea(out List<CPDFEditTextArea> textAreas, out CPDFPage pdfPage, out CPDFEditPage editPage);
  542. if (textAreas.Count == 0 || pdfPage == null || editPage == null)
  543. return;
  544. if (ToolView.CurrentEditAreaObject() != null)
  545. {
  546. bool result;
  547. Rect oldRect = DataConversionForWPF.CRectConversionForRect(textAreas[0].GetFrame());
  548. if (string.IsNullOrEmpty(textAreas[0].SelectText))
  549. {
  550. string fontName = "Helvetica";
  551. float fontSize = 14;
  552. byte[] fontColor = { 0, 0, 0 };
  553. byte transparency = 255;
  554. bool isBold = false;
  555. bool isItalic = false;
  556. textAreas[0].GetTextStyle(ref fontName, ref fontSize, ref fontColor, ref transparency, ref isBold, ref isItalic);
  557. result = textAreas[0].SetCurTextStyle(fontName, fontSize, fontColor[0], fontColor[1], fontColor[2], transparency, e, isItalic);
  558. }
  559. else
  560. {
  561. result = textAreas[0].SetCharsFontBold(e);
  562. }
  563. if (result)
  564. {
  565. PDFEditHistory editHistory = new PDFEditHistory();
  566. editHistory.EditPage = editPage;
  567. editHistory.PageIndex = pdfPage.PageIndex;
  568. ToolView.GetCPDFViewer().UndoManager.AddHistory(editHistory);
  569. ToolView.UpdateRender(oldRect, textAreas[0]);
  570. }
  571. }
  572. else
  573. {
  574. GroupHistory groupHistory = new GroupHistory();
  575. foreach (CPDFEditTextArea textArea in textAreas)
  576. {
  577. if (textArea.SetCharsFontBold(e))
  578. {
  579. PDFEditHistory editHistory = new PDFEditHistory();
  580. editHistory.EditPage = editPage;
  581. editHistory.PageIndex = pdfPage.PageIndex;
  582. groupHistory.Histories.Add(editHistory);
  583. }
  584. }
  585. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(groupHistory);
  586. ToolView.GetCPDFViewer()?.UpdateRenderFrame();
  587. }
  588. editPage.EndEdit();
  589. if (EditEvents.Count > 0 && textAreas.Count > 0)
  590. {
  591. EditEvents.FirstOrDefault().IsBold = e;
  592. DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
  593. defaultSettingParam.SetPDFEditParamm(EditEvents.FirstOrDefault());
  594. }
  595. //if (EditMultiEvents != null)
  596. //{
  597. // foreach (PDFEditEvent editEvent in EditMultiEvents)
  598. // {
  599. // editEvent.IsBold = e;
  600. // }
  601. // PDFEditEvent.UpdatePDFEditList(EditMultiEvents);
  602. //}
  603. }
  604. private void TextStyleUI_TextFontChanged(object sender, string e)
  605. {
  606. GetTextArea(out List<CPDFEditTextArea> textAreas, out CPDFPage pdfPage, out CPDFEditPage editPage);
  607. if (textAreas.Count == 0 || pdfPage == null || editPage == null)
  608. return;
  609. if (ToolView.CurrentEditAreaObject() != null)
  610. {
  611. bool result;
  612. Rect oldRect = DataConversionForWPF.CRectConversionForRect(textAreas[0].GetFrame());
  613. if (string.IsNullOrEmpty(textAreas[0].SelectText))
  614. {
  615. string fontName = "Helvetica";
  616. float fontSize = 14;
  617. byte[] fontColor = { 0, 0, 0 };
  618. byte transparency = 255;
  619. bool isBold = false;
  620. bool isItalic = false;
  621. textAreas[0].GetTextStyle(ref fontName, ref fontSize, ref fontColor, ref transparency, ref isBold, ref isItalic);
  622. result = textAreas[0].SetCurTextStyle(e, fontSize, fontColor[0], fontColor[1], fontColor[2], transparency, isBold, isItalic);
  623. }
  624. else
  625. {
  626. result = textAreas[0].SetCharsFontName(e);
  627. }
  628. if (result)
  629. {
  630. PDFEditHistory editHistory = new PDFEditHistory();
  631. editHistory.EditPage = editPage;
  632. editHistory.PageIndex = pdfPage.PageIndex;
  633. ToolView.GetCPDFViewer().UndoManager.AddHistory(editHistory);
  634. ToolView.UpdateRender(oldRect, textAreas[0]);
  635. }
  636. }
  637. else
  638. {
  639. GroupHistory groupHistory = new GroupHistory();
  640. foreach (CPDFEditTextArea textArea in textAreas)
  641. {
  642. if (textArea.SetCharsFontName(e))
  643. {
  644. PDFEditHistory editHistory = new PDFEditHistory();
  645. editHistory.EditPage = editPage;
  646. editHistory.PageIndex = pdfPage.PageIndex;
  647. groupHistory.Histories.Add(editHistory);
  648. }
  649. }
  650. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(groupHistory);
  651. ToolView.GetCPDFViewer()?.UpdateRenderFrame();
  652. }
  653. editPage.EndEdit();
  654. if (EditEvents.Count > 0 && textAreas.Count > 0)
  655. {
  656. EditEvents.FirstOrDefault().FontName = e;
  657. DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
  658. defaultSettingParam.SetPDFEditParamm(EditEvents.FirstOrDefault());
  659. }
  660. //if (EditMultiEvents != null)
  661. //{
  662. // foreach (PDFEditEvent editEvent in EditMultiEvents)
  663. // {
  664. // editEvent.FontName = e;
  665. // }
  666. // PDFEditEvent.UpdatePDFEditList(EditMultiEvents);
  667. //}
  668. }
  669. private void OpacityComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  670. {
  671. ComboBoxItem selectItem = OpacityComboBox.SelectedItem as ComboBoxItem;
  672. if (selectItem != null && selectItem.Content != null)
  673. {
  674. if (double.TryParse(selectItem.Content.ToString().TrimEnd('%'), out double newOpacity))
  675. {
  676. FontOpacitySlider.Value = newOpacity / 100.0;
  677. }
  678. }
  679. }
  680. private void TextMarkupUI_TextUnderlineChanged(object sender, bool e)
  681. {
  682. GetTextArea(out List<CPDFEditTextArea> textAreas, out CPDFPage pdfPage, out CPDFEditPage editPage);
  683. if (textAreas.Count == 0 || pdfPage == null || editPage == null)
  684. return;
  685. if (ToolView.CurrentEditAreaObject() != null)
  686. {
  687. bool result;
  688. Rect oldRect = DataConversionForWPF.CRectConversionForRect(textAreas[0].GetFrame());
  689. if(e)
  690. {
  691. result = textAreas[0].AddUnderline();
  692. }
  693. else
  694. {
  695. result = textAreas[0].RemoveUnderline();
  696. }
  697. if (result)
  698. {
  699. PDFEditHistory editHistory = new PDFEditHistory();
  700. editHistory.EditPage = editPage;
  701. editHistory.PageIndex = pdfPage.PageIndex;
  702. ToolView.GetCPDFViewer().UndoManager.AddHistory(editHistory);
  703. ToolView.UpdateRender(oldRect, textAreas[0]);
  704. }
  705. }
  706. else
  707. {
  708. GroupHistory groupHistory = new GroupHistory();
  709. foreach (CPDFEditTextArea textArea in textAreas)
  710. {
  711. bool result;
  712. if (e)
  713. {
  714. result = textArea.AddUnderline();
  715. }
  716. else
  717. {
  718. result = textArea.RemoveUnderline();
  719. }
  720. if (result)
  721. {
  722. PDFEditHistory editHistory = new PDFEditHistory();
  723. editHistory.EditPage = editPage;
  724. editHistory.PageIndex = pdfPage.PageIndex;
  725. groupHistory.Histories.Add(editHistory);
  726. }
  727. }
  728. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(groupHistory);
  729. ToolView.GetCPDFViewer()?.UpdateRenderFrame();
  730. }
  731. editPage.EndEdit();
  732. }
  733. private void TextMarkupUI_TextStrikethroughChanged(object sender, bool e)
  734. {
  735. GetTextArea(out List<CPDFEditTextArea> textAreas, out CPDFPage pdfPage, out CPDFEditPage editPage);
  736. if (textAreas.Count == 0 || pdfPage == null || editPage == null)
  737. return;
  738. if (ToolView.CurrentEditAreaObject() != null)
  739. {
  740. bool result;
  741. Rect oldRect = DataConversionForWPF.CRectConversionForRect(textAreas[0].GetFrame());
  742. if (e)
  743. {
  744. result = textAreas[0].AddStrikethrough();
  745. }
  746. else
  747. {
  748. result = textAreas[0].RemoveStrikethrough();
  749. }
  750. if (result)
  751. {
  752. PDFEditHistory editHistory = new PDFEditHistory();
  753. editHistory.EditPage = editPage;
  754. editHistory.PageIndex = pdfPage.PageIndex;
  755. ToolView.GetCPDFViewer().UndoManager.AddHistory(editHistory);
  756. ToolView.UpdateRender(oldRect, textAreas[0]);
  757. }
  758. }
  759. else
  760. {
  761. GroupHistory groupHistory = new GroupHistory();
  762. foreach (CPDFEditTextArea textArea in textAreas)
  763. {
  764. bool result;
  765. if (e)
  766. {
  767. result = textArea.AddStrikethrough();
  768. }
  769. else
  770. {
  771. result = textArea.RemoveStrikethrough();
  772. }
  773. if (result)
  774. {
  775. PDFEditHistory editHistory = new PDFEditHistory();
  776. editHistory.EditPage = editPage;
  777. editHistory.PageIndex = pdfPage.PageIndex;
  778. groupHistory.Histories.Add(editHistory);
  779. }
  780. }
  781. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(groupHistory);
  782. ToolView.GetCPDFViewer()?.UpdateRenderFrame();
  783. }
  784. editPage.EndEdit();
  785. }
  786. #endregion
  787. #region Text Edit
  788. private void GetTextArea(out List<CPDFEditTextArea> textAreas, out CPDFPage pdfPage, out CPDFEditPage editPage)
  789. {
  790. textAreas = new List<CPDFEditTextArea>();
  791. editPage = null;
  792. pdfPage = null;
  793. if (ToolView == null)
  794. {
  795. return;
  796. }
  797. if (EditEvents != null && EditEvents.Count>0 )
  798. {
  799. try
  800. {
  801. CPDFViewer pdfViewer = ToolView.GetCPDFViewer();
  802. CPDFDocument pdfDoc = pdfViewer.GetDocument();
  803. pdfPage = pdfDoc.PageAtIndex(EditEvents.FirstOrDefault().PageIndex);
  804. editPage = pdfPage.GetEditPage();
  805. List<CPDFEditArea> editAreas = editPage.GetEditAreaList();
  806. foreach (TextEditParam editEvent in EditEvents)
  807. {
  808. if (editAreas != null && editAreas.Count > editEvent.EditIndex)
  809. {
  810. textAreas.Add(editAreas[editEvent.EditIndex] as CPDFEditTextArea);
  811. }
  812. }
  813. }
  814. catch (Exception ex)
  815. {
  816. }
  817. }
  818. }
  819. #endregion
  820. private void chkMulti_Click(object sender, RoutedEventArgs e)
  821. {
  822. ToolView.SetIsMultiSelected((e.Source as CheckBox).IsChecked.GetValueOrDefault());
  823. }
  824. private void chkEditPen_Click(object sender, RoutedEventArgs e)
  825. {
  826. if ((e.Source as CheckBox).IsChecked.GetValueOrDefault())
  827. {
  828. ToolView.SetEditPen(null);
  829. }
  830. else
  831. {
  832. ToolView.SetEditPen(new Pen()
  833. {
  834. Brush = new SolidColorBrush(Colors.Black),
  835. Thickness = 0
  836. });
  837. }
  838. ShowBorder = ToolView.GetEditPen() == null || ToolView.GetEditPen().Thickness != 0;
  839. ToolView.GetCPDFViewer().UpdateRenderFrame();
  840. }
  841. protected bool UpdateProper<T>(ref T properValue,
  842. T newValue,
  843. [CallerMemberName] string properName = "")
  844. {
  845. if (object.Equals(properValue, newValue))
  846. return false;
  847. properValue = newValue;
  848. OnPropertyChanged(properName);
  849. return true;
  850. }
  851. protected void OnPropertyChanged([CallerMemberName] string propertyName = "") =>
  852. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  853. }
  854. }