Creating Rich Text Templates - c#

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
}

Related

How do I display a link as plain text in a README file

I am writing a README file and I want to display the text "ASP.NET" in the file, but it displays as a link. Is there a way to force a URL (or text that looks like a URL) to display as plain text?
You have various ways to Disable Github-flavored Markdown autolinking
For example:
https://<!--This is a comment-->gist.github.com/<!--This is, too-->alexpeattie/4729247
# give:
https://gist.github.com/alexpeattie/4729247
Try also a code block: `https://your/url`, if you don't mind the code presentation.

Adding text to email.Body messes with the text formatting

I'm trying to replace some text in the body of the email, but whenever I do it seems to screw up the formatting of the signature text. For example, if I try email.Body.Replace("Info", "Information") then this text:
First Last Title Info
Website: www.stackoverflow.com
turns into:
First Last Title Information Website: HYPERLINK
"http://www.stackoverflow.com/"www.stackoverflow.com
This happens even with just a simple addition to the text like email.Body += "text".
Along with this, it also switches the font of the body from Arial 10 to Calabri 12 (this probably has to do with Calabri 12 being the default email text when Arial 10 is the signiture font).
How would I save the format of the current email body so that when I add text to it, I could set it back or what would be the best way to add text to the body to keep the formatting?
Your help would be welcomed.
MailMessage.Body can be formatted in RTF, which I think it does by default, or in HTML. What the top message you posted actually looks like is something like this:
{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Calibri;}}
{\colortbl ;\red0\green0\blue255;}
{\*\generator Msftedit 5.41.21.2510;}\viewkind4\uc1\pard\sl240\slmult1\lang9\f0\fs22 First Last \par
Title \par
\par
Info \par
Website: {\field{\*\fldinst{HYPERLINK "www.stackoverflow.com"}}{\fldrslt{\ul\cf1 www.stackoverflow.com}}}\f0\fs22\par
}
I'm assuming that appending "text" to the end of that is causing the RTF message to be parsed improperly, resulting in a loss of formatting.
http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.body.aspx
So, what I ended up doing was using the email.HTMLBody. I created a paragraph block similar to what the HTML already had. This is the block: (my HTML isn't very good so... a lot of if is copy/paste with some editing)
<p class=MsoNormal>
<b>
<span style='font-size:10.0pt;font-family:"Arial","sans-serif"'>Replace Text
</span>
</b>
</p>
I set this to a string, say htmlPar, then do a replace:
string htmlReplace = htmlPar.Replace("Replace Text", myNewText");
This will create a paragraph block that I can add to the HTML with whatever my text is. To actually add it to the end of the HTMLBody I do a string.Replace like so:
string temp = email.HTMLBody.Replace("</body></html>", htmlReplace + "</body></html>");
email.HTMLBody = temp;
With this I was able to keep the formatting in the rest of the email. Like I said, my HTML isn't great so I'm not sure if this could work to add text in general or how good of a method doing it this way actually is.
Either way, I hope this can help someone else down the road.

How to insert text from a text file into a Label Control in C#

Could someone demonstrate how to insert text from a text file e.g. test.txt into a Label control on a visual C# form please
You leave much to the imagination as to where you currently are with this and from which point you need help, but in the simplest form, try this:
theLabel.Text = File.ReadAllText(pathToFile);
label.Text = File.ReadAllText("test.txt");
File.ReadAllText Method
The answers for the question are useful so far.
Also you are considering this for WinForms as far as I understood from the tags.
If you would like to do any kind of action like this on Web Forms, you should consider that the text from the file goes without any encoding to label control. So, any kind of JavaScript code can be injected.
Always HtmlEncode your text;
var pathToFile = Server.MapPath("~/poo.txt");
lblPoo.Text = HttpUtility.HtmlEncode(File.ReadAllText(pathToFile));

WPF writing html to a rich text box

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.

How to copy all data from a HTML doc and save it to a string using C#

I need to create a data index of HTML pages provided to a service by essentially grabbing all text on them and putting them in a string to go into a storage system.
If this were GUI based, I would simply Ctrl+A on the HTML page, copy it, then go to Notepad and Ctrl+V. Simples. If I can do it via good old point n' click, then surely there must be a way to do it programmatically, but I'm struggling to find anything useful.
The HTML docs in question are being loaded for rendering currently using the System.Windows.Controls.WebBrowser class, so I wonder if its somehow possible to grab the data from there?
I'm going to keep hunting, but any pointers would be very appreciated.
Note: We don't want the HTML source code, and would also really rather not have to parse all the source code to get the text unless we absolutely have to.
If I understand your problem correctly, you will have to do a bit of work to get the data.
WebBrowser browser=new WebBrowser(); // This is what you have
HtmlDocument doc = browser.Document; // This gives you the browser contents
String content =
(((mshtml.HTMLDocumentClass)(doc.DomDocument)).documentElement).innerText;
That last line is the browser's view of the rendered content.
This looks like it might be quite helpful.

Categories

Resources