PDFTextEditControl.xaml.cs 15 KB

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