123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- using ComPDFKit.PDFDocument;
- using PDF_Master.Model.BOTA;
- using PDF_Master.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_Master.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<int> indexList = new List<int>();
- 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<string> splitList = new List<string>();
- 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<int> indexList = new List<int>();
- 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<string> splitList = new List<string>();
- 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;
- }
- }
- }
|