I want to replace some text in a docx file, and i'm using XCeed Docx lib, i want to ask if there is any way to replace the text with a new formatted text? For exemple i have the %value% (normal text) and i want to replace it with bold text to replace only the text i use the function document.ReplaceText(%value%, "Bold text"). Remark not all the replace text need to be bold and i do not know if the next is bold or not, i need the bold text only in some situations.
try this:
Formatting formatting = new Formatting();
formatting.Bold = true;
doc.ReplaceText("%value%", "Bold Text", false, System.Text.RegularExpressions.RegexOptions.None, formatting);
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 try to set text and a predefined style to a bookmark. Text is fine, but the style is not set. Whats wrong with this source?:
Word.Application word = new Word.Application();
word.Visible = true;
Word.Document doc = word.Documents.Open("bookmark.dotx");
doc.Activate();
Word.Paragraph paragraph = doc.Bookmarks["navigatorHeadlineBookmark"].Range.Paragraphs.Add();
paragraph.Range.Text = "hello headline";
paragraph.Range.set_Style("navigatorHeadline");
//Debug
paragraph.Range.Select(); //selects the expected text (hello headline)
Word.Selection selection = word.Selection;
selection.set_Style("navigatorHeadline"); //style is not set :-(
Summarizing the steps to trouble-shoot that finally led to the answer:
1. First, make sure the bookmark is in the document and contains the correct text/range.
2. Check that the style name is spelled correctly, keeping in mind that Word handles style names as case-sensitive
3. Direct formatting and character styles will override the formatting of a paragraph style.
This last proved to be the reason style formatting was not appearing: the text was formatted with the Hyperlink style. This is a character style, so was overriding the formatting from the style specified in the code.
In this case, the character style must be removed in order to see the formatting from the paragraph style. Programmatically, this can be done EITHER
By applying the character style "Default Paragraph Font" to the Range
or
By using one of the "clear formatting" methods of the Selection object. Which to choose depends on the exact behavior desired. All formatting can be cleared, all direct formatting can be cleared, or the character style can be cleared (which would give the same result as option 1): Selection.ClearCharacterStyle.
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 want to append text, for example "World" in a textbox, but not with the Windows.Forms functions for RichTextBox. Instead i want to specify position and the string that i want to insert into Rtf property of the RichTextBox! So, is it possible to update the rtf string directly ?
string rtfStuffs = this.richTextBox1.Rtf;
// Edit as you see fit...
this.richTextBox1.Rtf = rtfStuffs;
I have a multi-lined TextBox. I have also an Input box used in specifying a Regex string that is used in formatting the text that is entered in the TextBox.
Regex.Replace replaces strings. What I want is to apply some styling to the found matched as using Bold, Italic and change its ForeColor.
For Example:
Text in TextBox:
Change all text in the multi-lined
text box that matches the text inside
the input box's text box
String in Input Box/Regex:
text
And I have:
RegexOptions.IgnoreCase
The desired result:
Change all text in the multi-lined
text box that matches the text inside the input box's text box
You need to use a RichTextBox for this. First use the RegEx to find the indexes of the sub strings and then use SelectionStart and SelectionEnd to select that text in the RTB and SelectionFont and SelectionColor to make it bold and colored.