Getting Hyperlinks Working in Rich Text Box Using RTF - c#

I am trying to format a hyperlink in a Rich Text Box using the Rich Text Format. I can get basic formatting working thanks to this answer, for example making text bold. However I cannot get the RTF formatted hyperlink to work. I found an example of making an RTF link here. However, when I try to put this in the Rich Text Box as seen below, it causes my application to crash. Any suggestions as to what i'm missing here?
string my_hyperlink_text = #"{\field{\*\fldinst HYPERLINK \"http://www.google.com/\"}{\fldrslt Google}}"
if (rtbControl is RichTextBox & rtbControl.Name == "name_of_control") // Making sure the control is a RichTextBox
{
RichTextBox rtb = rtbControl as RichTextBox;
rtb.Rtf = my_hyperlink_text;
}

An easy way for getting rtfs to work is to write your text in Microsoft word, copy & paste it to Wordpad and the saving it as a RTF from there.
The detour with MS Word is needed, because WordPad does not support entering links in the UI, although it handles them correctly when they come from other sources, like the clipboard. Also, MS Word creates massively bloated rtf.
The rtf file you create this way can then be opened in any text editor and can be used as a string constant in your program.
In your case, I suppose that the prefix and maybe the color table are missing and are causing the problem.
By the way: Wordpad is not much more than a wrapper around the Windows rtf control, i.e. the same control that you are using in your code.

Related

Unwanted characters in Acrobat PDF conversion of auto-detected Word fillable fields. Deleting fillable field characters using iTextSharp5

Acrobat DC, Office 365, iTextSharp5, Win10 Pro 64-bit
I have a Word document containing several pages of text and one empty TextBox in between two of the lines. I am attempting to use the Acrobat "Prepare Form" feature to convert that document to PDF with the TextBox as a fillable field, and Acrobat has no problem auto detecting the TextBox and making it fillable. The problem, however, is that the converted TextBox contains text from either the line of text above it or below it.
I've read that this is caused by placing the TextBox too close to those lines in the Word document and sure enough, by leaving three or four empty lines of space above and below the text box the issue goes away. However, that's an unacceptable amount of wasted space. I tried putting continuous section breaks above and below the TextBox in Word as well as typing spaces in the TextBox but that doesn't help. I also tried it with a 1x1 table instead of a TextBox but the same problem occurred.
I then tried deleting the unwanted text from the PDF TextBox field and saving it that way, which appeared to be a reasonable solution. However, when I used an iTextSharp5 program to detect the PDF's fillable fields it could no longer detect the empty field. I wouldn't mind leaving the original unwanted text in the PDF TextBox field if there were some way to remove it with iTextSharp, but it doesn't seem to have that ability.
Because I have many Word documents to convert to fillable PDF's and might need to update them occasionally, it simply isn't practical for me to manually add the fillable fields to the converted PDFs each time an update is needed. Any suggestions are welcome :-)

How to retrieve the Font of word from RichTextBox in Winforms?

I am developing an app in WinForms using C#. It has a small window that includes a RichTextBox. The user can write in the RichTextBox and by pressing ctrl+b and ctrl+i they can change the font to bold or italic. When the application is closed down the text is saved. When the application is restarted the text is stored into the RichTextBox again. The problem is I cannot store the font the user was writing with. If a user had a word in bold for example, after the app restarts the word is not bold anymore. Is there a way to store the state of a word ?
The RTF property of the RichTextBox returns the formatted text, so that's what you need to store:
You can use this property .. to extract the text of the control with
the specified RTF formatting defined in the text of the control.
As #stuartd mentioned the RTF propert can be used to solve the aforementioned problem. I store the myRichTextBox.Rtf property in a string and then in a file. After app restarts I read the file and assign the read value to myRichTextBox.Rtf .

Create a word template and replace content control text with code using Open XML

I'm trying to create a word template with place holders (content controls), I then want to replace the text within these place holders using Open XML but I can't do it.
So within word I've entered Design Mode and added a Rich Text Box content control. Then using the properties I've set the Title/Tag to textBuildingName for example.
So I now want to change the text of that place holder from my C# code using Open XML. However I don't really know how to reference the place holder in code. So all I have so far is this:
using (WordprocessingDocument doc = WordprocessingDocument.Open(docPath, true))
{
//What do I do here to get the content control by name
//so I can do something like this:
//textBuildingName.Text="This is my replacement text from my application";
}
Any advice on this would be greatly received, I can't find any good tutorials on the web.
I'm using Visual Studio 2012 and Word 2013
I use Word 2007, but this might also work for newer versions (word 2010 works as well): I use the content control toolkit which is a easy way of connecting your rich text boxes with the correct xml tags.
http://dbe.codeplex.com/
Hope this helps

Rich textbox that displays html like a web browser control

I am displaying text in a rich textbox and i want it to show the html formatting on the text. Is there a way to make a rich textbox display html.
If you push the button on the following link you will see how my out put displays, i would like it done in a rich textbox.
http://neil.fraser.name/software/diff_match_patch/svn/trunk/demos/demo_diff.html
Is there a way to build a rich textbox that can display html???
RTF encoding is different from HTML.
If not, then you need to write your own HTML to RTF converter or find something similar.
Writing Your Own RTF Converter. This guy gives a great breakdown of how the program works along with details of the conversion.
An Extended richtextbox control, Check this out. It may solve your solution

Header in richtext format and manipulating the rtf file

What is header in richtextbox format?? And how can we manipulate the rtf file.
To see how an RTF document looks in its raw form, save some text in an RTF-aware editor like wordpad, and then open it again in a plain text editor. For example, this RTF document:
Hello,
world!
becomes this when opened in plain text mode:
{\rtf1\ansi\ansicpg1252\deff0\deflang1030{\fonttbl{\f0\fswiss\fcharset0 Arial;}}
{\*\generator Msftedit 5.41.21.2509;}\viewkind4\uc1\pard\f0\fs20 Hello,\par
\b world\b0 !\par
}
Notice that there is a lot more text in the raw version than is visible when opened in an RTF-aware editor. If you want to manipulate the RTF document using raw string operations, you have to be careful to preserve the structure of this document otherwise it may become unreadable. It would be better to use an RTF parser to modify the document to avoid accidentally breaking it.
See the v1.6 RTF specification, including the Header part.
Obviously this relates to your previous questions today: again, I suggest that instead of trying to have a single document with multiple sections, you create separate documents.

Categories