I am working on a WPF Windows application using C# code. This is an application I inherited. I only have limited experience using WPF.
I have a Rich Text Box control which is used to build an Email using html. There is code to read the Rich Text Box contents and store the results as html in a database. This code is working. I need to write the reverse that is writing the html to the rich text box so it appears as text.
Below is the code I have to read the rich text box.
TextRange xaml = new TextRange(EmailBodyRichTextBox.Document.ContentStart, EmailBodyRichTextBox.Document.ContentEnd);
MemoryStream ms = new MemoryStream();
xaml.Save(ms, DataFormats.Xaml);
string xamlString = ASCIIEncoding.Default.GetString(ms.ToArray());
string html = HTMLConverter.HtmlFromXamlConverter.ConvertXamlToHtml("<FlowDocument>" + xamlString + "</FlowDocument>");
html = HttpUtility.HtmlEncode(html);
From the code you post above , it seems , you are saving the text in the rich text box to xaml, and then translate the xaml to HTML.
if you want to reverse, then translate your HTML to xaml and then load it to the rich text box.
To save the RichTextBox's content to a regular string that you can save in a database:
string formattedEmail = XamlWriter.Save(EmailBodyRichTextBox.Document);
To reload the RichTextBox from the string:
EmailBodyRichTextBox.Document = XamlReader.Parse(formattedEmail);
If you only want to store the emails in the database and then reload them to RichTextBoxes, there is no point in converting to HTML, it can only introduce conversion format mismatches.
Related
I have a RichTextBox in which I write text and I give it color and format and what I want is that when I press a button that I have programmed, I open the Word application and pass the text that I have in the RichTextBox to the Word document, along with the color and format that I have given in my application.
I have the following code that opens Word and it passes the text that I had in the RichTextBox but the problem is that it does not show me the color and format that I had in the text in my application.
colorLetra = new ColorDialog();
objWord = new Word.Application();
objWord.Visible = true;
objDocumento = objWord.Documents.Add(Missing.Value);
objWord.Selection.Font.Color = objWord.Selection.Font.Color;
objWord.Selection.TypeText(richTextBox.Text);
Could you tell me why it does not show me the color and format in Word?
Your question is:
Could you tell me why it does not show me the color and format in Word?
The reason is because you only enter/type the text. You don't apply any formatting. You're simply transferring the string value of the Windows Forms control to the Word document, as a string.
Your implied question is: How do I pass formatted RichTextBox content to Word...
There is no way to directly pass formatted information from a Windows Form to a Word document. You must go over the clipboard, as was suggested in a comment. The code that comments points to, however, is incorrect for formatted text. The following works for me:
if (richTextBox.Text.Length > 0)
{
// Copy the formatted content to the clipboard
Clipboard.SetText(richTextBox.Rtf, TextDataFormat.Rtf);
objWord.Selection.Paste();
}
I have a PDF template with form fields where I used iText 7.1.0 to replace some placeholder text inside different form fields which is working fine:
PdfReader reader = new PdfReader(pdf_template);
PdfDocument pdf = new PdfDocument(reader, new PdfWriter(pdf_output));
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdf, true);
form.GetField("Vertragsnummer").SetValue("12345");
form.FlattenFields();
pdf.Close();
reader.Close();
Now I have a form field for which option is set to allow rich text format as there are some parts with bold font etc..
Inside the form field standard text there are again placeholders which I want to replace.
Currently I've got the following code:
PdfFormField tf_maintext = form.GetField("maintext");
tf_maintext.SetRichText(tf_maintext.GetRichText());
Now I need to replace the placeholders but I didn't find any working solution for C#.
For normal form fields I can use
PdfFormField kdnr = form.GetField("Kundennummer");
kdnr.SetValue(kdnr.GetValueAsString().Replace("7777777777", "1111111111"));
the normal Replace function to achieve this.
But I need a solution to replace text in the rich text format field to ensure that the formatting is still there afterwards.
How can I do this?
When I send an html email with an attached file, Plain Text on Thunderbird doesn't show anything, especially when an attached file is docx, pdf, or etc. txt file was okay when I tested though.
It works well if I don't attach any files, Thunderbird correctly shows Html Text and Plain Text if I switch its view setting.
using(var mail = new MailMessage()){
// ...omit
var alternateView = AlternateView.CreateAlternateViewFromString("<h1>TEST</h1>", Encoding, MediaTypeNames.Text.Html);
alternateView.TransferEncoding = TransferEncoding.SevenBit;
mail.AlternateViews.Add(alternateView);
mail.Attachments.Add(new Attachment("path to a file"));
// ...omit
}
I tried https://stackoverflow.com/a/2828438/366049 but no luck.
--EDITED--
As Caius Jard suggested, I tried the following and the results are below.
setting it as the main body + the HTML as an alternateview
It works, but this means I have to prepare plain text and html text. If I don't attach any files, Thunderbird shows plain text which is from html text removing tags. I want this behavior all the time since input text is made by our users. Otherwise I need to retrieve plain text from html.
setting both the text and the HTML as alternate views, no main body
Both Plain Text and Html Text settings in Thunderbird show the text alternate view.
setting the text as both the main body and a text alternateview as well as having an HTML alternateview
Thunderbird shows the text alternate view even if I switch its view setting.
According to these experiments, a plain alternate view is superior than an html alternate view and a main body.
Though your question doesn't seem to contain example code that demonstrates how you set the plaintext, try
setting it as the main body + the HTML as an alternateview
setting both the text and the HTML as alternate views, no main body
setting the text as both the main body and a text alternateview as well as having an HTML alternateview
I'd be interested to know which of these works out for thunderbird..
I need to create a Rich Text Formatted (RTF) Template File I can pass data through which generates the RAW rich text (that can then be saved).
I see a TON of HTML editors...I don't want that.
I need something to create the actual RTF...raw!
I will save that raw RTF as a template
Having a hard time finding something. Thanks
FOR EXAMPLE:
I need the raw RTF for a template...I DO NOT want HTML
{\rtf1\ansi\ansicpg1252\uc1\deff0{\fonttbl
{\f0\fnil\fcharset0\fprq2 Arial;}
{\f1\fswiss\fcharset0\fprq2 Tahoma;}
{\f2\froman\fcharset2\fprq2 Symbol;}}
{\colortbl;}
{\stylesheet{\s0\itap0\nowidctlpar\f0\fs24 [Normal];}{\*\cs10\additive Default Paragraph Font;}}
{\*\generator TX_RTF32 19.0.542.500;}
\paperw7315\paperh15840\margl0\margt0\margr0\margb0\deftab1134\widowctrl\lytexcttp\formshade\sectd
\headery720\footery720\pgwsxn7315\pghsxn15840\marglsxn0\margtsxn0\margrsxn0\margbsxn0\pgbrdropt32\pard\itap0\nowidctlpar\plain\f1\fs20 MED BILL INV# 28989293\par }
Sorry... Your question is not very clear...
What about:
richTextBox1.SaveFile(filename.rtf);
If you open that file with Notepad you'll see something like:
{\rtf1\ansi\ansicpg1252\deff0\deflang1040{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
\viewkind4\uc1\pard\b\i\f0\fs36\par
}
I have an outlook message file , converting into stream using below code
Stream messageStream = File.Open(MessageFileName, FileMode.Open, FileAccess.Read);
Taking this stream into OutlookStorage.Message object as shown below.
OutlookStorage.Message message = new OutlookStorage.Message(messageStream);
I am displaying this message object body into RichTextBox.
RichTextBox1.Text = message.BodyText;
In RichTextBox1 am able to see only Text data wherin images and any formatted data not displaying in richtextbox
My requirement is i want to display message body as it is into Richtextbox whether it might be image , Formatted text or anything.
How can i?
You need to use message.BodyRTF instead of message.BodyText. To get it displayed in your RichTextBox, use Rtf:
RichTextBox1.Rtf = message.BodyRTF;