So, the problem is there I already have the formatted text in RTF format but when I save the file it gets "wrong", the header disappears.
I'm using the RichTextBox Class to create the file, where finalText is the formatted RTF text, making the code look something like this:
System.Windows.Forms.RichTextBox RichTextBox = new System.Windows.Forms.RichTextBox();
string finalText = "{\rtf1\deff0{\fonttbl{\f0 Calibri;}{\f1 Arial;}{\f2 ..." //Formatted RTF here
RichTextBox.Rtf = finalText;
RichTextBox.SaveFile(_RtfPath_);
When I do this the final version of the generated file is almost complete, but the header disappears.
Editing the file in notepad++ I noticed that it removes the header and the information contained in it. After this I decided to manually create an RTF file. I opened the almost complete RTF file in notepad++, erased everything and pasted the finalText.
When I reopened this file the final version was the way I wanted it.
Is there any way I can save the text as RTF without having to use the RichTextBox Class to format the output file?
Related
I have a button in wpf that when clicked does the following:
Clipboard.SetText("a\u0000b")
When I try and paste the contents of the clipboard into notepad all i get is:
a
How can I get the entire string?
If I render this string in a wpf control, i see a[square thing]b. In other words, the view control does not terminate at a null unicode character.
I tried to find if you can copy text with NUL in Windows, and it presumably is not possible. Maybe you could temporarily replace NUL with some other character, which should never appear in any text you process (although it probably will one day, as Murphy's law states), then open the text file and convert all occurrences of this character back to NUL?
var text = "a\u0000b";
var textToCopy = text.Replace("\u0000", "\u3f45");
Clipboard.SetText(textToCopy);
// Next paste the contents to the file and reverse the replacement there
It is a workaround, but if you're the one using those text files, it might be worth a try.
I'm trying to put in the clipboard piece of HTML and plain text at the same time, so that HTML-capable editors could paste HTML, and other editors could use plain text.
Clipboard.SetData(DataFormats.Html, htmlWithHeader);
Clipboard.SetData(DataFormats.UnicodeText, plainText);
But only the last format is actually put to the clipboard. In the sample above, clipboard would contain only plaintext (as shown by Clipboard.GetDataObject().GetFormats()). And if I swap the lines, the clipboard would have only the HTML format.
How can I put both formats into the clipboard at the same time?
You can NOT use Clipboard.SetData for setting both HTML and plain text. The second call of SetData will clear the content of clipboard that has been set by first call and store the new data.
You should use DataObject and Clipboard.SetDataObject().
Example:
DataObject dataObj = new DataObject();
dataObj.SetData(DataFormats.Html, htmlWithHeader);
dataObj.SetData(DataFormats.Text, plainText);
Clipboard.SetDataObject(dataObj);
How to paste HTML ( tables ) code into Excel or PowerPoint?
I've overcome some issues concerning pasting HTML into Excel and PowerPoint and noticed that a lot of people are asking that.
I'd like to share my research, solution I made out for it.
Let's say we have a html file named html and we would like to access it in Excel, let's do following:
Clipboard.SetText(html);
We copy our html into the Clipboard. The clipboard generates from the html a real table or image/chart from the input file.
System.Threading.Thread.Sleep(2000);
Let's wait a second to have a preview
sheet.Range(cellmapp).PasteSpecial();
Now, we paste the content into a range that we could like to paste it, by defining cellmap.
System.Threading.Thread.Sleep(1000);
Let's wait a second to see the output
sheet.UsedRange.Copy(Missing.Value);
Now, in order to copy the table image into PowerPoint, we must work the with UsedRange.Copy, because it will copy the currently selected Excel area.
In order to check that we paste it into the correct Powerpoint slide
foreach (PowerPoint.Slide slide in presentation.Slides)
{
foreach (PowerPoint.Shape pptshape in slide.Shapes)
{
if(<your condition satisfies>)
{
slide.Select(); // some position in any slide
pptshape.Delete();//delete old content that was in that slide
ppApp.ActiveWindow.View.PasteSpecial(); //paste the Excel content
}
}
}
Of course there are other solutions, like making an image out of the html code and pasting that, which was my initial idea.
Another post refering that manipulation:
Showing HTML in PowerPoint
All, I write a log file to a .rtf file which has formatting underlining, bold etc. I have saved this file and want to read it back into the RichTextBox at a later time persisting its formatting. I have tried the following
tmpRichTextBox.LoadFile(#"F:\Path\File.rtf", RichTextBoxStreamType.RichText);
It loads the file but there is none of my original formatting. If I load the .rtf into word, the formatting shows up. How do I read the .rtf back into the RichTextBox including its formatting?
Thanks for your time.
Did you Check NRTFTree.
Its an awesome library for RTF management!
Edited:
It's possible that you are losing the formating later in code. There are certain operations that may cause the loss of the formating. For example,
richTextBox.Font = newFont;
I had this problem, but luckily I found a way around it. Here's the code that will let you change the font without losing the formating:
richTextBox.SelectAll();
richTextBox.SelectionFont = newFont;
string rtf = richTextBox.SelectedRtf;
richTextBox.Font = newFont;
richTextBox.Rtf = rtf;
If you can save your log file to html format you can read this file with WebBrowser Control.
Like this:
private void button1_Click(object sender, EventArgs e)
{
string fileName = Path.Combine(Environment.CurrentDirectory, "log1.htm");
webBrowser1.DocumentText = File.ReadAllText(fileName);
}
This works perfectrly.
How can I load a file in a c# app so that I can see it formatted.
If I use:
richTextBox2.LoadFile("a.txt", RichTextBoxStreamType.PlainText);
I did it with:
textBox2.Text = File.ReadAllText("a.txt");
The text is not formatted at all, new lines and CR's are totally missing, also the tabs
also the file open ok in notepad, but it opens bad in wordpad
Can I load the file in a textbox? (use multi-line)
Have you tried loading it as RichText?
richTextBox2.LoadFile("a.rtf", RichTextBoxStreamType.RichText);