Construct string with multiple fonts - c#

I need to add strings coming from different RichTextBoxes with different fonts into one RichTextBox retaining the original fonts (more typically sometime I get XML format where fonts for substrings is defined.)
Is there a way of constructing this string in memory and then simply puting it in a RichTextBox? If not, is there any other way?

Try this:
richTextBox1.SelectionStart = 0;
richTextBox1.SelectionLength = 10;
richTextBox1.SelectionFont = new Font( "Veradana", 8.25F );
foreach block with different font just repeat the code

You can save anything in RichTextBox in .rtf format using its
SaveFile method within RichTextBoxStreamType.RichText format and If you wish to load saved format , call LoadFile method
Source here

This answer gives a code sample for drawing text with different colors into a picture box. You could easily modify it to draw the text with different fonts as well. If you need the scrollability of the RichTextBox, you could place the picture box on a panel.
Update: since you need to use a RichTextBox, this link shows you how to transform your original XML into RTF that you can load into your RichTextBox. In order to do this, you have to create an XSLT document that describes how to transform the XML into RTF. The link I included gives a sample XSLT document; you would have to modify this XSLT based upon how the different fonts are specified in your original XML document (if you post a sample of your original XML, we could probably help you modify the XSLT accordingly).

Related

Getting Hyperlinks Working in Rich Text Box Using RTF

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.

gibberish text output in word output when generated using the template (.dotx) file

I have collection of RTF text and I need to generate the report in word by pasting this RTF content from collection in loop. I use word template (i.e. .dotx) file as a base template file so as to generate report in structured manner. In base template file, I have some placeholder texts which needs to be replaced with the RTF content. While replacing the placeholder text with RTF content I'm facing following two issues :
maintaining the source formatting i.e. to apply format of RTF content (i.e. font, color, bold, etc.) in generated word report
pasting of RTF text in proper order with proper content
In order to maintain source formatting while copying the RTF content, I use
Range.PasteAndFormat(WdRecoveryType.wdFormatOriginalFormatting)
This ensures that while I copy the RTF content from my collection, original source formatting (i.e. as per RTF text) gets applied. There are few other methods to paste the content i.e. Paste, PasteSpecial but they don't maintain the source formatting and upon pasting, it takes the font as the default font of the base word template file. Problem using the PasteAndFormat method is, generated output it bit gibberish when report is generated using template file while generated output is proper when not generated using template file.
I have created the sample project (available at https://drive.google.com/open?id=1es1aBgewbJvQxmOAQF3FMhx3inu3keVy) which exactly reproduces the problem I face. In sample application, when you enter 1, it will generate the report without using the template file and that is the kind of final report I want. When you enter 2, it generates the report with gibberish text and I'm not able to figure out the reason for such output.
Can anyone pls help me to figure out what is the problem with the sample application code and help me to generate the report with option 2 same as the one generated with option 1?

How to parse HTML text and add it to MigraDoc Document

I need to get a text that's being written by a user (in CKEditor HTML), and then add that text to a MigraDoc document, as a paragraph or whatever I need it to be.
My idea was converting the text to an MDDDL document (in memory) and add it to the document. But I don't know if there are any DLLs that permit that behaviour.
So, my question is, can someone give me pointers or advice on how I could make this happen? Should I parse the HMTL text? If so, to what should I parse it? How can I add it afterwards?
Neither PDFsharp nor MigraDoc can parse HTML, so either write your own code or try to find a third-party library (which may not exist yet).
I would probably convert the HTML directly to MigraDoc document objects in memory.
MigraDoc / PDFSharp can't do this.
But, you could use HtmlAgilityPack nuget and then use its htmlDoc.DocumentNode.Descendants() to pull out the pieces of text from html in a flat list kind of a structure, and node.ParentNode.Name to figure out the tag that the text is wrapped in. And then insert the text into your MigraDoc document with something like .AddFormattedText() and apply custom MigraDoc styles to it - i.e. if the parent tag is "strong" then apply a MigraDoc style where Font.Italic = true; etc..

How to save the contents of a rich textbox without RTF formatting tags?

I just want to save the contents of a rich textbox into a TXT file and keep everything in it in the format that it is (as far as spaces, new lines, and tabs are concerned, but I don't care if colors and fonts are lost), but they're always saved as RTF files.
Even I specify a txt file the results still look like:
({\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}} {\colortbl ;\red0\green128\blue0;\red255\green0\blue0;})
How can I make it just save the contents without those tags?
Use richTextBox1.SaveFile(path, RichTextBoxStreamType.PlainText) instead of RichTextBoxStreamType.RichText

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