Insert a formatted footnote using Microsoft.Interop.Office.Word - c#

I am trying to inset formatted footnotes into an open word document using a WinForms application.
While I am able to use Interop.Word to set plain text footnotes and so long as I use plain text it works fine. However, I also want the user to be able to paste rich text formatted text from a rich text box into the footnote. This never works and always shows the rich text codes.
I know that footnotes can take formatting because if I put the rich text int a clipboard and paste it into a footnote the formatting is preserved.
I have even tried putting the rich text into the clipboard and then setting the string (s) to the clipboard contents using "s = Clipboard.GetText(TextDataFormat.Rtf);" It seems as if this should be exactly what I am pasting, but if I paste into the footnote it works. If the program sets it using the code below it does not work.
I appreciate any help.
application = (Microsoft.Office.Interop.Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
application.ActiveDocument.Footnotes.Add(application.Selection.Range, "", s);

Cindy's answer was helpful. I fixed the problem by doing the following:
Saving the current selected position in the document and current clipboard contents.
Inserting a blank footnote.
Selecting the footnote.
Putting the string into the clipboard as rich text format.
Sending/pasting the information from the clipboard to Word.
Restoring the clipboard and original selected position in the document.

Related

Tab Delimited Text Copy Paste To Excel

I am generating a block of text via C# Stringbuilder, with appropriately tab-delimited text (new lines, "\t", etc.) and displaying the text within a tag in my web application. If I copy/paste this to Excel, all the text pastes into the first column on the spreadsheet.
If I copy the outerHTML of the block (Chrome) or copy/paste into Notepad++ and THEN paste to Excel, all the data pastes neatly into individual cells - which is the desired outcome of this effort. Can anyone tell me what I need to do in order to be able to copy the text from my application and paste it into Excel, so that the text pastes into individual cells (ie, keeps the tab-delimited format)?
Unfortunately, it appears an extra step is required.
The default behaviour of paste in Excel (& many other applications) is to use the original format of the contents of the clipboard.
Copying from a web application/HTML page will result in the clipboard contents being flagged as HTML. The clipboard contents will have the tab character, but as HTML renders tabs to whitespace (compacting to a single whitespace if there are multiple tabs) - just using control-V will convert the tabs to spaces & the contents will appear in a single cell when pasting into Excel..
You will need to use the "Paste Special" option & select "Unicode text" to retain the tabs. There is no way to set this by default : https://answers.microsoft.com/en-us/msoffice/forum/msoffice_excel-mso_windows8-mso_365hp/how-to-change-default-paste-behavior-with-ctrl-v/f58da075-8fd5-4c80-a64b-5e71ec8ad38b?auth=1
Copying the outerHTML of Chrome sets the clipboard format to text in the first place, pasting to Notepad++ does paste as text rather than HTML then recopying sets the format to text - which is why these methods work.
I was able to paste to Excel and keep the format by changing the element my text wrote to from a div to textarea. Copy/paste of tsv from textarea to a tab-delimited Excel spreadsheet keeps the data formatted in tabular format when pasted. Thanks all for the help.
Try this ,
Before pasting data set excel's text to column delimiters to space and try to paste your values again.
Old topic but I had this. Need to copy something each day from a programme which outputs to google chrome browser. Whenever it was copied and pasted into excel it was going into 1 cell. Yet if I control all and paste it was ok.
I found you can copy only the lines you want and it will behave as wanted - if you don't drag the cursor all the way down off the page. ie if you highlight down to the last piece of information in the bottom right of screen more carefully, when you past to excel it pastes it as tab separated. Whereas if you drag it right down it pastes it all in 1 cell. Might help someone in the future who has this.
You can use 'text to columns' in Excel to format the data in the way you want it to.
After pasting the data into excel, select the column with your data.
Go to Data - click 'Text to columns'
Choose delimited and hit next
Choose Tab and hit finish.
If you don't want to use this extra step, the following works for Excel 2013:
Go to Home, Paste options and click 'Use text import wizard'
Choose delimited and hit next
Choose Tab and hit finish.

Appending data to saved file from RichTextBox

Am writing data in .rtf Format from a RichTextBox using (Text is color coded )
RichTextBox .SaveFile(path);
There is a 'Clear Text' button on the GUI on click of which clears the RichTextBox .
The problem arises when new data is printed on the RichTextBox instead of appending the data the RichTextBox .SaveFile(path) method clears out the previous data and contains only the newly added data.
How can i append the data? StreamWriter is wrtitng the data in plain text i need it in .RTF.
Can anybody help me on this?
You can save the previous data in a variable, set the richTextBox's data to previous+new and then call the SaveFile method.
Seems like saving from the rich text box overwrites the file. You can have a look at this or this for more info.
Some of the solutions suggested are saving to a different file, or reading, concatenating, and saving.
There is no option to append text to RTF file as you have already read from the comments.
What you can do, however, is use 2 RichEdit controls, one that reads the stream in using EM_STREAMIN message (this will preserve the char format) and concatenate the new data to it. For the preservation of the char format of the new data from the other RichEdit control (colors,fonts etc), you have to use EM_GETCHARFORMAT message on the selection of the new data you want to concatenate. And then, you need to use EM_SETCHARFORMAT message with SCF_SELECTION OR SCF_WORD to set the char format on the RichEdit control that will hold all data together. After that, use EM_REPLACESEL message to concatenate all the data together. Finally, use EM_STREAMOUT message to save all of the stream at one go.

Displaying Raw Data From Image File Using TextBox or RichTextBox?

My program reads a DDS image file and stores it as a byte array. I want to be able to show the users the raw data in a TextBox form so at first I convert the byte array to a string using the following code:
string data = System.Text.Encoding.ASCII.GetString(bytes);
I then set the TextBox text:
textBox.Text = data;
The problem I am having is the text box is not showing all the data. Here is a screenshot of how it looks:
As you can see only the first few characters are displayed. I am assuming this is because the string contains a null terminator which the TextBox interprets as the end of the string. Here is a copy paste of the first 50 or so characters in the string which I copied directly from the debugger watch window:
DDS |\0\0\0\a\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\
As you can see the first null character comes right after "DDS |" which explains why that's all that shows up in the TextBox.
What I want to be displayed is similar to what you see if you edit the raw DDS file with a text editor such as Notepadd++.
Opening the DDS file in Notepad++ produces the following:
My question is, how do I get my TextBox (or RichTextBox) to show the data in the same way that Notepad++ shows it?
The simplest solution is to use this:
textbox.Text = data.Replace("\0", #"\0");
This will force the textbox to actually show a backslash followed by a zero where the nulls would be. Alternatively, you could replace the nulls with some other character or string.

C# RichTextBox colored text

I've a RichTextBox and I want color text in it. Is there any tag option? I'd like something like this [color:red]nick[/color] some message. Because I need to save it as text and I want on reload have also colored text.
Can I do something like this without writing own method?
You can set color for text in RichTextBox with SelectionColor
And if you want to save your rtf as plain text, then you will have to look at rtf format. Example:
{\rtf1\ansi\deff0 {\colortbl;\red0\green0\blue0;\red255\green0\blue0;}
This line is the default color\line \cf2 This line is red\line \cf1
This line is the default color }
EDIT:
From this example - first of all you have to declare color table \colortbl in fololowing format:
{\colortbl; color1; color2; ... ; colorN;}
And then in the text you will have to enclose text with {\cfN YOUR_TEXT} where N is a number of color from table; you can not specify the boundaries of the block {}, then everything after \ cfN will be one color.
As the name says RichTextBox contains RichText
to change the Rtf text with 'rtf specific-tags' you can set/use the
RichTextBox.RtfProperty
also take a look at RichTextBox.SelectionColor to color text patterns in code
but when you don't want to use rtf, you said
need to save as text.
you could write your own 'markup' there is no built in expect rtf/html?
but rtf is text - at all
Example to use RichTextBix.SelectionColor to Color the text
richTextBox1.Text = "Hello";
richTextBox1.Select(0,2);
richTextBox1.SelectionColor = Color.Red;
colors the start of "Hello" red
and now you can access the 'taggeg' text in the RTFProperty of the RichTextBox
If you need examples of how things are encoded in RTF, you can create the document manually in Word or Wordpad, and save it as RTF. This will give you a hint about how to encode your formatting. Furthermore, if you're for instance creating help-documents, you can include them as an embedded resource and load them directly into the RichTextBox, with all the formatting included.
rtfMain.SaveFile(dlgSave.FileName);
From Reference Save text from rich text box with C#

Crystal Reports messing up Rich Text

I have a string that gets saved as rich text to a SQL field, so it has a lot of symbols in front of it like so...
{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fprq2\fcharset0 Calibri;}{\f1\fnil\fcharset0 Verdana;}}
{\colortbl ;\red0\green0\blue255;}
\viewkind4\uc1\pard\sa200\sl276\slmult1\f0\fs22 Blah Blah Blah \par
I'm passing in this to Crystal Reports 11.5 as a parameter field, so the data type is string. I'm then just displaying it straight up by dragging the parameter to the report. The interpretation is set to RTF also in the paragraph tab of the formatting settings for this.
My problem is, it is removing the formatting tags but it is screwing up the text. It is randomly garbling words and inserting characters like the letter 'i' randomly in words, it's also putting some random letters in the background behind other words, making it look like it's been typed over manually if that makes sense, just makes the word look unreadable and bolded like someone typed over it with a typewriter.
I tried setting a new text object and then putting the rich text parameter in that to display, but then it doesn't remove/do any of the rich text formatting tags.
What gives? It can obviously do rich text as it removes the tags, but what is the deal with the random i's inserted into words and a couple of instances of the jumbled text over other words?
Unfortunately, I haven't used 11.5 yet.. but give this a try..
Format Field > Paragraph tab > Text Interpretation > RTF text
If the text is getting overlapped, then you can replace the following control set from your RTF text. It should work properly and not overlap.
data = data.Replace(#"{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fprq2\fcharset0 Calibri;}{\f1\fnil\fcharset0 Verdana;}} {\colortbl ;\red0\green0\blue255;} \viewkind4\uc1\pard\sa200\sl276\slmult1\f0\fs22 Blah Blah Blah \par", string.Empty);

Categories