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);
Related
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.
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.
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#
I have strange problem with coping text into clipboard. I want to copy text from textbox with additional formatting. In order to do that I intercept KeyDown event on textbox and I check if keys CTRL and C are pressed. Then I do sth like that
string extraFormatedText = formatText(txtBox.Text);
Clipboard.SetText(extraFormatedText, TextDataFormat.Text);
Function formatText adds couple of empty additional lines in text. However if I paste this text into notepad there is no additional formatting.
Why is that ??
If I call function Clipboard.GetText() I can clearly see that in returned string there are extra characters ( \n \r).
\r represents a carriage return and \n a newline (linefeed) character.
The method formatText must be adding these to the end of the string.
Also, the program Notepad has no formatting functionality such as bold or italic, it simply displays everything in the chosen font, size and style.
If you're adding just \n to insert new lines Notepad will not understand that as it expects \r\n as a line break. Try pasting your text into Notepad++ and you'll see that line breaks are actually there.
It's safer to use Environment.NewLine, which on Windows will give you exactly \r\n.
I have an application which involves building rich text character by character in order to undo it all at once. In order to retain that character's formatting, I select it and then can add the SelectedRtf property to my StringBuilder, however I want to be able to build each Rich Text character into one Rich Text string so that I can undo it in one go.
My question is how can you combine Rich Text strings into one big string while retaining all the formatting for each character.
merge it this way:
richTextBox2.Select(richTextBox2.TextLength, 0);
richTextBox2.SelectedRtf = richTextBox1.Rtf;