RichTextBoxHelper.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using ComPDFKit.PDFDocument;
  2. using PDF_Master.Model.BOTA;
  3. using PDF_Master.Views.BOTA;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Text.RegularExpressions;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Documents;
  15. using System.Windows.Media;
  16. using System.Windows.Media.TextFormatting;
  17. namespace PDF_Master.Helper
  18. {
  19. public class OutlineSerach {
  20. public string Title { get; set; }
  21. }
  22. public class RichTextBoxHelper : DependencyObject
  23. {
  24. public static FlowDocument GetDocumentBind(DependencyObject obj)
  25. {
  26. return (FlowDocument)obj.GetValue(DocumentBindProperty);
  27. }
  28. public static void SetDocumentBind(DependencyObject obj, FlowDocument value)
  29. {
  30. obj.SetValue(DocumentBindProperty, value);
  31. }
  32. public static FlowDocument GetOutlineSearchDocumentBind(DependencyObject obj)
  33. {
  34. return (FlowDocument)obj.GetValue(OutlineSearchDocumentBindProperty);
  35. }
  36. public static string searchWord = "";
  37. public static void SetOutlineSearchDocumentBind(DependencyObject obj, FlowDocument value)
  38. {
  39. obj.SetValue(OutlineSearchDocumentBindProperty, value);
  40. }
  41. public static readonly DependencyProperty DocumentBindProperty =
  42. DependencyProperty.RegisterAttached("DocumentBind",
  43. typeof(TextBindProperty),
  44. typeof(RichTextBoxHelper),
  45. new FrameworkPropertyMetadata
  46. {
  47. BindsTwoWayByDefault = true,
  48. PropertyChangedCallback = (obj, e) =>
  49. {
  50. RichTextBox richTextBox = obj as RichTextBox;
  51. TextBindProperty bindItem = e.NewValue as TextBindProperty;
  52. if (richTextBox != null && bindItem != null)
  53. {
  54. richTextBox.Document = GetFlowDocument(bindItem.TextContent.Trim(), bindItem.SearchWord, bindItem.HighLightColor);
  55. }
  56. }
  57. });
  58. public static readonly DependencyProperty OutlineSearchDocumentBindProperty =
  59. DependencyProperty.RegisterAttached("OutlineSearchDocumentBind",
  60. typeof(OutlineNode),
  61. typeof(RichTextBoxHelper),
  62. new FrameworkPropertyMetadata
  63. {
  64. BindsTwoWayByDefault = false,
  65. PropertyChangedCallback = (obj, e) =>
  66. {
  67. RichTextBox richTextBox = obj as RichTextBox;
  68. OutlineNode bindItem = e.NewValue as OutlineNode;
  69. if (richTextBox != null )
  70. {
  71. // richTextBox.Document = GetOutlineFlowDocument(bindItem.outline.Title, searchWord, Color.FromArgb(0x99, 0xFF, 0xF7, 0x00));
  72. /* if (richTextBox.Document.Name!="nodata") {
  73. richTextBox.Visibility = Visibility.Collapsed;
  74. }*/
  75. }
  76. }
  77. });
  78. public static FlowDocument GetOutlineFlowDocument(string content, string keyword, Color textColor)
  79. {
  80. FlowDocument Document = new FlowDocument();
  81. Paragraph textPara = new Paragraph();
  82. Document.Blocks.Add(textPara);
  83. List<int> indexList = new List<int>();
  84. content = Regex.Replace(content, "[\r\n]", " ");
  85. if (keyword.Length > 0)
  86. {
  87. for (int i = 0; i < content.Length && i >= 0;)
  88. {
  89. i = content.IndexOf(keyword, i, StringComparison.OrdinalIgnoreCase);
  90. if (i == -1)
  91. {
  92. break;
  93. }
  94. if (indexList.Contains(i) == false)
  95. {
  96. indexList.Add(i);
  97. }
  98. i += keyword.Length;
  99. }
  100. }
  101. List<string> splitList = new List<string>();
  102. int lastIndex = -1;
  103. foreach (int index in indexList)
  104. {
  105. string prevStr = string.Empty;
  106. if (lastIndex == -1)
  107. {
  108. prevStr = content.Substring(0, index);
  109. //Document.Name = "nodata";
  110. }
  111. else
  112. {
  113. prevStr = content.Substring(lastIndex + keyword.Length, index - lastIndex - 1);
  114. }
  115. if (prevStr != string.Empty)
  116. {
  117. splitList.Add(prevStr);
  118. }
  119. splitList.Add(content.Substring(index, keyword.Length));
  120. lastIndex = index;
  121. }
  122. if (indexList.Count > 0)
  123. {
  124. lastIndex = indexList[indexList.Count - 1];
  125. if (content.Length > lastIndex + keyword.Length)
  126. {
  127. splitList.Add(content.Substring(lastIndex + keyword.Length));
  128. }
  129. }
  130. TextBlock addBlock = new TextBlock();
  131. if (splitList.Count == 0)
  132. {
  133. Run textRun = new Run(content);
  134. addBlock.Inlines.Add(textRun);
  135. }
  136. else
  137. {
  138. foreach (string textappend in splitList)
  139. {
  140. Run textRun = new Run(textappend);
  141. if (textappend.Equals(keyword, StringComparison.OrdinalIgnoreCase))
  142. {
  143. textRun.Background = new SolidColorBrush(textColor);
  144. }
  145. addBlock.Inlines.Add(textRun);
  146. }
  147. }
  148. addBlock.TextTrimming = TextTrimming.CharacterEllipsis;
  149. textPara.Inlines.Add(addBlock);
  150. return Document;
  151. }
  152. public static FlowDocument GetFlowDocument(string content, string keyword, Color textColor)
  153. {
  154. FlowDocument Document = new FlowDocument();
  155. Paragraph textPara = new Paragraph();
  156. Document.Blocks.Add(textPara);
  157. List<int> indexList = new List<int>();
  158. content = Regex.Replace(content, "[\r\n]", " ");
  159. if (keyword.Length > 0)
  160. {
  161. for (int i = 0; i < content.Length && i >= 0;)
  162. {
  163. i = content.IndexOf(keyword, i, StringComparison.OrdinalIgnoreCase);
  164. if (i == -1)
  165. {
  166. break;
  167. }
  168. if (indexList.Contains(i) == false)
  169. {
  170. indexList.Add(i);
  171. }
  172. i += keyword.Length;
  173. }
  174. }
  175. List<string> splitList = new List<string>();
  176. int lastIndex = -1;
  177. foreach (int index in indexList)
  178. {
  179. string prevStr = string.Empty;
  180. if (lastIndex == -1)
  181. {
  182. prevStr = content.Substring(0, index);
  183. }
  184. else
  185. {
  186. prevStr = content.Substring(lastIndex + keyword.Length, index - lastIndex - 1);
  187. }
  188. if (prevStr != string.Empty)
  189. {
  190. splitList.Add(prevStr);
  191. }
  192. splitList.Add(content.Substring(index, keyword.Length));
  193. lastIndex = index;
  194. }
  195. if (indexList.Count > 0)
  196. {
  197. lastIndex = indexList[indexList.Count - 1];
  198. if (content.Length > lastIndex + keyword.Length)
  199. {
  200. splitList.Add(content.Substring(lastIndex + keyword.Length));
  201. }
  202. }
  203. TextBlock addBlock = new TextBlock();
  204. foreach (string textappend in splitList)
  205. {
  206. Run textRun = new Run(textappend);
  207. if (textappend.Equals(keyword, StringComparison.OrdinalIgnoreCase))
  208. {
  209. textRun.Background = new SolidColorBrush(textColor);
  210. textRun.FontWeight = FontWeight.FromOpenTypeWeight(600);
  211. }
  212. addBlock.Inlines.Add(textRun);
  213. }
  214. addBlock.TextTrimming = TextTrimming.CharacterEllipsis;
  215. textPara.Inlines.Add(addBlock);
  216. return Document;
  217. }
  218. }
  219. }