/// <summary> /// Converts HTML document to RTF format; Places this RTF in RichTextBox; /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Start_Click(object sender, RoutedEventArgs e) { string htmlFile = @"..\..\Sample.html"; string rtfString = String.Empty; // Create an instance of the converter. SautinSoft.HtmlToRtf h = new HtmlToRtf(); h.TextStyle.DefaultFontFamily = "Calibri"; // Convert HTML to RTF. if (h.OpenHtml(htmlFile)) { using (MemoryStream msRtf = new MemoryStream()) { // Convert HTML to RTF. if (h.ToRtf(msRtf)) { // Place the RTF into RichTextBox. System.Windows.Documents.TextRange tr = new System.Windows.Documents.TextRange( RtfControl.Document.ContentStart, RtfControl.Document.ContentEnd); tr.Load(msRtf, DataFormats.Rtf); } } } }
public override void Init(object data) { if (data != null && data is string) { var text = data as string; if (!string.IsNullOrEmpty(text)) { var h2r = new HtmlToRtf(); using (var stream = new MemoryStream()) { h2r.OpenHtml(text); h2r.ToRtf(stream); stream.Seek(0, SeekOrigin.Begin); using (var reader = new StreamReader(stream)) { var txt = reader.ReadToEnd(); byte[] byteArray = Encoding.ASCII.GetBytes(txt); using (MemoryStream ms = new MemoryStream(byteArray)) { TextRange tr = new TextRange(MainRTB.Document.ContentStart, MainRTB.Document.ContentEnd); tr.Load(ms, DataFormats.Rtf); } } } } } }
private static void RichTextProperty_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e) { var view = d as CustomRichTextBox; string text = "\t"; if (e.NewValue != null && !string.IsNullOrEmpty(e.NewValue.ToString())) { text = e.NewValue as string; } try { if (!string.IsNullOrEmpty(text)) { var h2r = new HtmlToRtf(); using (var stream = new MemoryStream()) { h2r.OpenHtml(text); h2r.ToRtf(stream); stream.Seek(0, SeekOrigin.Begin); using (var reader = new StreamReader(stream)) { var txt = reader.ReadToEnd(); byte[] byteArray = Encoding.ASCII.GetBytes(txt); using (MemoryStream ms = new MemoryStream(byteArray)) { TextRange tr = new TextRange(view.Document.ContentStart, view.Document.ContentEnd); tr.Load(ms, DataFormats.Rtf); } } } } } catch (Exception ex) { App.Logger.Error(ex.Message); } }
protected override Task WriteAsyncTask(LogEventInfo logEvent, CancellationToken cancellationToken) { if (FlowDoc == null) { return(Task.Run(() => { var me = this; var dispatcherOperation = Application.Current.Dispatcher.BeginInvoke( DispatcherPriority.Background, (Func <object>)(() => Application.Current.FindResource(me.ResourceKey)), cancellationToken); dispatcherOperation.Task.ContinueWith((task) => { lock (me.FlowDocLock) { me.FlowDoc = (FlowDocument)dispatcherOperation.Result; } }, cancellationToken); })); } return(Task.Run(() => { string logMessage = this.RenderLogEvent(this.Layout, logEvent); SautinSoft.HtmlToRtf h = new HtmlToRtf(); string rtf = String.Empty; if (h.OpenHtml(logMessage)) { rtf = h.ToRtf(); SetRTFText(rtf); foreach (var block in rtb.Document.Blocks) { FlowDoc.Blocks.Add(block); } } })); }