PDFTextEditControl.xaml.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFPage;
  3. using ComPDFKit.PDFPage.Edit;
  4. using ComPDFKit.Tool;
  5. using ComPDFKit.Tool.UndoManger;
  6. using ComPDFKitViewer;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Controls.Primitives;
  12. using System.Windows.Media;
  13. namespace Compdfkit_Tools.Edit
  14. {
  15. public partial class PDFTextEditControl : UserControl
  16. {
  17. public CPDFViewerTool ToolView { get; private set; }
  18. public TextEditParam EditEvent { get; set; }
  19. public PDFTextEditControl()
  20. {
  21. InitializeComponent();
  22. Loaded += PDFTextEditControl_Loaded;
  23. }
  24. public void InitWithPDFViewer(CPDFViewerTool newPDFView)
  25. {
  26. ToolView = newPDFView;
  27. }
  28. public void SetPDFTextEditData(TextEditParam newEvent)
  29. {
  30. EditEvent = null;
  31. if (newEvent != null && newEvent.EditType == CPDFEditType.EditText)
  32. {
  33. GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  34. List<string> sysfontList = new List<string>();
  35. if (textArea != null)
  36. {
  37. sysfontList= textArea.GetFontList();
  38. }
  39. if (sysfontList.Count == 0)
  40. {
  41. sysfontList.Add("Helvetica");
  42. sysfontList.Add("Courier New");
  43. sysfontList.Add("Times New Roman");
  44. }
  45. if (sysfontList.Contains(newEvent.FontName) == false && string.IsNullOrEmpty(newEvent.FontName) == false)
  46. {
  47. sysfontList.Add(newEvent.FontName);
  48. }
  49. TextStyleUI.SetFontNames(sysfontList);
  50. TextStyleUI.SelectFontName(newEvent.FontName);
  51. TextStyleUI.SetFontStyle(newEvent.IsBold, newEvent.IsItalic);
  52. TextStyleUI.SetFontSize(newEvent.FontSize);
  53. OpacityTextBox.Text = string.Format("{0}%", (int)(Math.Ceiling(newEvent.Transparency * 100 / 255D)));
  54. FontOpacitySlider.Value = ((int)(Math.Ceiling(newEvent.Transparency * 100 / 255D))) / 100D;
  55. TextAlignUI.SetFontAlign(newEvent.TextAlign);
  56. if(newEvent.FontColor != null && newEvent.FontColor.Length==3)
  57. {
  58. FontColorUI.SetCheckedForColor(Color.FromRgb(
  59. newEvent.FontColor[0],
  60. newEvent.FontColor[1],
  61. newEvent.FontColor[2]));
  62. }
  63. }
  64. EditEvent = newEvent;
  65. }
  66. private void Slider_DragCompleted(object sender, DragCompletedEventArgs e)
  67. {
  68. Slider slider = sender as Slider;
  69. if (slider != null)
  70. {
  71. slider.Tag = "true";
  72. }
  73. GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  74. if (textArea != null)
  75. {
  76. Rect oldRect=textArea.GetFrame();
  77. if (textArea.SetCharsFontSize((float)slider.Value,false))
  78. {
  79. PDFEditHistory editHistory = new PDFEditHistory();
  80. editHistory.EditPage = editPage;
  81. if (pdfPage != null)
  82. {
  83. editHistory.PageIndex = pdfPage.PageIndex;
  84. }
  85. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  86. ToolView.UpDataRender(oldRect, textArea);
  87. }
  88. }
  89. }
  90. private void SliderOpacity_DragCompleted(object sender, DragCompletedEventArgs e)
  91. {
  92. Slider slider = sender as Slider;
  93. if (slider != null)
  94. {
  95. slider.Tag = "true";
  96. }
  97. GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  98. if (textArea != null)
  99. {
  100. Rect oldRect=textArea.GetFrame();
  101. if(textArea.SetCharsFontTransparency((byte)(FontOpacitySlider.Value * 255)))
  102. {
  103. PDFEditHistory editHistory = new PDFEditHistory();
  104. editHistory.EditPage = editPage;
  105. if (pdfPage != null)
  106. {
  107. editHistory.PageIndex = pdfPage.PageIndex;
  108. }
  109. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  110. ToolView.UpDataRender(oldRect, textArea);
  111. }
  112. }
  113. }
  114. private void SliderOpacity_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  115. {
  116. Slider slider = sender as Slider;
  117. if (OpacityTextBox != null && FontOpacitySlider != null)
  118. {
  119. OpacityTextBox.Text = string.Format("{0}%", (int)(FontOpacitySlider.Value * 100));
  120. }
  121. if (slider != null && slider.Tag != null && slider.Tag.ToString() == "false")
  122. {
  123. return;
  124. }
  125. GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  126. if (textArea != null)
  127. {
  128. Rect oldRect=textArea.GetFrame();
  129. if (textArea.SetCharsFontTransparency((byte)(FontOpacitySlider.Value * 255)))
  130. {
  131. PDFEditHistory editHistory = new PDFEditHistory();
  132. editHistory.EditPage = editPage;
  133. if (pdfPage != null)
  134. {
  135. editHistory.PageIndex = pdfPage.PageIndex;
  136. }
  137. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  138. ToolView.UpDataRender(oldRect, textArea);
  139. }
  140. }
  141. }
  142. private void Slider_DragStarted(object sender, DragStartedEventArgs e)
  143. {
  144. Slider slider = sender as Slider;
  145. if (slider != null)
  146. {
  147. slider.Tag = "false";
  148. }
  149. }
  150. private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  151. {
  152. Slider slider = sender as Slider;
  153. if (slider != null && slider.Tag != null && slider.Tag.ToString() == "false")
  154. {
  155. return;
  156. }
  157. GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  158. if (textArea != null)
  159. {
  160. Rect oldRect=textArea.GetFrame();
  161. if (textArea.SetCharsFontSize((float)slider.Value,false))
  162. {
  163. PDFEditHistory editHistory = new PDFEditHistory();
  164. editHistory.EditPage = editPage;
  165. if (pdfPage != null)
  166. {
  167. editHistory.PageIndex = pdfPage.PageIndex;
  168. }
  169. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  170. ToolView.UpDataRender(oldRect, textArea);
  171. }
  172. }
  173. }
  174. private void PDFTextEditControl_Loaded(object sender, RoutedEventArgs e)
  175. {
  176. TextStyleUI.TextFontChanged -= TextStyleUI_TextFontChanged;
  177. TextStyleUI.TextBoldChanged -= TextStyleUI_TextBoldChanged;
  178. TextStyleUI.TextItalicChanged -= TextStyleUI_TextItalicChanged;
  179. TextStyleUI.TextSizeChanged -= TextStyleUI_TextSizeChanged;
  180. TextAlignUI.TextAlignChanged -= TextAlignUI_TextAlignChanged;
  181. FontColorUI.ColorChanged -= FontColorUI_ColorChanged;
  182. TextStyleUI.TextFontChanged += TextStyleUI_TextFontChanged;
  183. TextStyleUI.TextBoldChanged += TextStyleUI_TextBoldChanged;
  184. TextStyleUI.TextItalicChanged += TextStyleUI_TextItalicChanged;
  185. TextStyleUI.TextSizeChanged += TextStyleUI_TextSizeChanged;
  186. TextAlignUI.TextAlignChanged += TextAlignUI_TextAlignChanged;
  187. FontColorUI.ColorChanged += FontColorUI_ColorChanged;
  188. }
  189. private void TextStyleUI_TextSizeChanged(object sender, double e)
  190. {
  191. GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  192. if (textArea != null)
  193. {
  194. Rect oldRect=textArea.GetFrame();
  195. if (textArea.SetCharsFontSize((float)e, false))
  196. {
  197. PDFEditHistory editHistory = new PDFEditHistory();
  198. editHistory.EditPage = editPage;
  199. if (pdfPage != null)
  200. {
  201. editHistory.PageIndex = pdfPage.PageIndex;
  202. }
  203. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  204. ToolView.UpDataRender(oldRect, textArea);
  205. }
  206. }
  207. }
  208. private void FontColorUI_ColorChanged(object sender, EventArgs e)
  209. {
  210. SolidColorBrush newBrush = FontColorUI.Brush as SolidColorBrush;
  211. GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  212. if (textArea != null && newBrush!=null)
  213. {
  214. Rect oldRect= textArea.GetFrame();
  215. if (textArea.SetCharsFontColor(newBrush.Color.R, newBrush.Color.G, newBrush.Color.B))
  216. {
  217. PDFEditHistory editHistory = new PDFEditHistory();
  218. editHistory.EditPage = editPage;
  219. if (pdfPage != null)
  220. {
  221. editHistory.PageIndex = pdfPage.PageIndex;
  222. }
  223. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  224. ToolView.UpDataRender(oldRect, textArea);
  225. }
  226. }
  227. }
  228. private void TextAlignUI_TextAlignChanged(object sender, TextAlignType e)
  229. {
  230. GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  231. if (textArea != null)
  232. {
  233. bool result = false;
  234. Rect oldRect= textArea.GetFrame();
  235. if(textArea.SelectLineRects!=null && textArea.SelectLineRects.Count>0)
  236. {
  237. result = textArea.SetTextRangeAlign(e);
  238. }
  239. else
  240. {
  241. result= textArea.SetTextAreaAlign(e);
  242. }
  243. if (result)
  244. {
  245. PDFEditHistory editHistory = new PDFEditHistory();
  246. editHistory.EditPage = editPage;
  247. if (pdfPage != null)
  248. {
  249. editHistory.PageIndex = pdfPage.PageIndex;
  250. }
  251. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  252. ToolView.UpDataRender(oldRect, textArea);
  253. }
  254. }
  255. }
  256. private void TextStyleUI_TextItalicChanged(object sender, bool e)
  257. {
  258. GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  259. if (textArea != null)
  260. {
  261. Rect oldRect= textArea.GetFrame();
  262. if(textArea.SetCharsFontItalic(e))
  263. {
  264. PDFEditHistory editHistory = new PDFEditHistory();
  265. editHistory.EditPage = editPage;
  266. if (pdfPage != null)
  267. {
  268. editHistory.PageIndex = pdfPage.PageIndex;
  269. }
  270. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  271. ToolView.UpDataRender(oldRect, textArea);
  272. }
  273. }
  274. }
  275. private void TextStyleUI_TextBoldChanged(object sender, bool e)
  276. {
  277. GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  278. if (textArea != null)
  279. {
  280. Rect oldRect= textArea.GetFrame();
  281. if (textArea.SetCharsFontBold(e))
  282. {
  283. PDFEditHistory editHistory = new PDFEditHistory();
  284. editHistory.EditPage = editPage;
  285. if (pdfPage != null)
  286. {
  287. editHistory.PageIndex = pdfPage.PageIndex;
  288. }
  289. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  290. ToolView.UpDataRender(oldRect, textArea);
  291. }
  292. }
  293. }
  294. private void TextStyleUI_TextFontChanged(object sender, string e)
  295. {
  296. GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  297. if (textArea != null)
  298. {
  299. Rect oldRect= textArea.GetFrame();
  300. if (textArea.SetCharsFontName(e))
  301. {
  302. PDFEditHistory editHistory = new PDFEditHistory();
  303. editHistory.EditPage = editPage;
  304. if (pdfPage != null)
  305. {
  306. editHistory.PageIndex = pdfPage.PageIndex;
  307. }
  308. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  309. ToolView.UpDataRender(oldRect, textArea);
  310. }
  311. }
  312. }
  313. private void OpacityComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  314. {
  315. ComboBoxItem selectItem = OpacityComboBox.SelectedItem as ComboBoxItem;
  316. if (selectItem != null && selectItem.Content != null)
  317. {
  318. if (double.TryParse(selectItem.Content.ToString().TrimEnd('%'), out double newOpacity))
  319. {
  320. OpacityTextBox.Text = selectItem.Content.ToString();
  321. FontOpacitySlider.Value = newOpacity / 100.0;
  322. }
  323. }
  324. }
  325. private void GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage)
  326. {
  327. textArea = null;
  328. editPage = null;
  329. pdfPage = null;
  330. if (ToolView == null || EditEvent == null)
  331. {
  332. return;
  333. }
  334. try
  335. {
  336. CPDFViewer pdfViewer = ToolView.GetCPDFViewer();
  337. CPDFDocument pdfDoc = pdfViewer.GetDocument();
  338. pdfPage = pdfDoc.PageAtIndex(EditEvent.PageIndex);
  339. editPage = pdfPage.GetEditPage();
  340. List<CPDFEditArea> editAreas = editPage.GetEditAreaList();
  341. if (editAreas != null && editAreas.Count > EditEvent.EditIndex)
  342. {
  343. textArea = editAreas[EditEvent.EditIndex] as CPDFEditTextArea;
  344. }
  345. }
  346. catch (Exception ex)
  347. {
  348. }
  349. }
  350. }
  351. }