using ComPDFKit.PDFDocument; using PDF_Office.Model.BOTA; using PDF_Office.Views.BOTA; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Media; using System.Windows.Media.TextFormatting; namespace PDF_Office.Helper { public class OutlineSerach { public string Title { get; set; } } public class RichTextBoxHelper : DependencyObject { public static FlowDocument GetDocumentBind(DependencyObject obj) { return (FlowDocument)obj.GetValue(DocumentBindProperty); } public static void SetDocumentBind(DependencyObject obj, FlowDocument value) { obj.SetValue(DocumentBindProperty, value); } public static FlowDocument GetOutlineSearchDocumentBind(DependencyObject obj) { return (FlowDocument)obj.GetValue(OutlineSearchDocumentBindProperty); } public static string searchWord = ""; public static void SetOutlineSearchDocumentBind(DependencyObject obj, FlowDocument value) { obj.SetValue(OutlineSearchDocumentBindProperty, value); } public static readonly DependencyProperty DocumentBindProperty = DependencyProperty.RegisterAttached("DocumentBind", typeof(TextBindProperty), typeof(RichTextBoxHelper), new FrameworkPropertyMetadata { BindsTwoWayByDefault = true, PropertyChangedCallback = (obj, e) => { RichTextBox richTextBox = obj as RichTextBox; TextBindProperty bindItem = e.NewValue as TextBindProperty; if (richTextBox != null && bindItem != null) { richTextBox.Document = GetFlowDocument(bindItem.TextContent.Trim(), bindItem.SearchWord, bindItem.HighLightColor); } } }); public static readonly DependencyProperty OutlineSearchDocumentBindProperty = DependencyProperty.RegisterAttached("OutlineSearchDocumentBind", typeof(OutlineNode), typeof(RichTextBoxHelper), new FrameworkPropertyMetadata { BindsTwoWayByDefault = false, PropertyChangedCallback = (obj, e) => { RichTextBox richTextBox = obj as RichTextBox; OutlineNode bindItem = e.NewValue as OutlineNode; if (richTextBox != null ) { // richTextBox.Document = GetOutlineFlowDocument(bindItem.outline.Title, searchWord, Color.FromArgb(0x99, 0xFF, 0xF7, 0x00)); /* if (richTextBox.Document.Name!="nodata") { richTextBox.Visibility = Visibility.Collapsed; }*/ } } }); public static FlowDocument GetOutlineFlowDocument(string content, string keyword, Color textColor) { FlowDocument Document = new FlowDocument(); Paragraph textPara = new Paragraph(); Document.Blocks.Add(textPara); List indexList = new List(); content = Regex.Replace(content, "[\r\n]", " "); if (keyword.Length > 0) { for (int i = 0; i < content.Length && i >= 0;) { i = content.IndexOf(keyword, i, StringComparison.OrdinalIgnoreCase); if (i == -1) { break; } if (indexList.Contains(i) == false) { indexList.Add(i); } i += keyword.Length; } } List splitList = new List(); int lastIndex = -1; foreach (int index in indexList) { string prevStr = string.Empty; if (lastIndex == -1) { prevStr = content.Substring(0, index); //Document.Name = "nodata"; } else { prevStr = content.Substring(lastIndex + keyword.Length, index - lastIndex - 1); } if (prevStr != string.Empty) { splitList.Add(prevStr); } splitList.Add(content.Substring(index, keyword.Length)); lastIndex = index; } if (indexList.Count > 0) { lastIndex = indexList[indexList.Count - 1]; if (content.Length > lastIndex + keyword.Length) { splitList.Add(content.Substring(lastIndex + keyword.Length)); } } TextBlock addBlock = new TextBlock(); if (splitList.Count == 0) { Run textRun = new Run(content); addBlock.Inlines.Add(textRun); } else { foreach (string textappend in splitList) { Run textRun = new Run(textappend); if (textappend.Equals(keyword, StringComparison.OrdinalIgnoreCase)) { textRun.Background = new SolidColorBrush(textColor); } addBlock.Inlines.Add(textRun); } } addBlock.TextTrimming = TextTrimming.CharacterEllipsis; textPara.Inlines.Add(addBlock); return Document; } public static FlowDocument GetFlowDocument(string content, string keyword, Color textColor) { FlowDocument Document = new FlowDocument(); Paragraph textPara = new Paragraph(); Document.Blocks.Add(textPara); List indexList = new List(); content = Regex.Replace(content, "[\r\n]", " "); if (keyword.Length > 0) { for (int i = 0; i < content.Length && i >= 0;) { i = content.IndexOf(keyword, i, StringComparison.OrdinalIgnoreCase); if (i == -1) { break; } if (indexList.Contains(i) == false) { indexList.Add(i); } i += keyword.Length; } } List splitList = new List(); int lastIndex = -1; foreach (int index in indexList) { string prevStr = string.Empty; if (lastIndex == -1) { prevStr = content.Substring(0, index); } else { prevStr = content.Substring(lastIndex + keyword.Length, index - lastIndex - 1); } if (prevStr != string.Empty) { splitList.Add(prevStr); } splitList.Add(content.Substring(index, keyword.Length)); lastIndex = index; } if (indexList.Count > 0) { lastIndex = indexList[indexList.Count - 1]; if (content.Length > lastIndex + keyword.Length) { splitList.Add(content.Substring(lastIndex + keyword.Length)); } } TextBlock addBlock = new TextBlock(); foreach (string textappend in splitList) { Run textRun = new Run(textappend); if (textappend.Equals(keyword, StringComparison.OrdinalIgnoreCase)) { textRun.Background = new SolidColorBrush(textColor); textRun.FontWeight = FontWeight.FromOpenTypeWeight(600); } addBlock.Inlines.Add(textRun); } addBlock.TextTrimming = TextTrimming.CharacterEllipsis; textPara.Inlines.Add(addBlock); return Document; } } }