{"id":55,"date":"2011-04-15T08:50:19","date_gmt":"2011-04-15T06:50:19","guid":{"rendered":"http:\/\/hen.co.za\/blog\/?p=55"},"modified":"2011-04-15T08:50:19","modified_gmt":"2011-04-15T06:50:19","slug":"rtfbuilder","status":"publish","type":"post","link":"https:\/\/hen.co.za\/blog\/2011\/04\/rtfbuilder\/","title":{"rendered":"RTFBuilder"},"content":{"rendered":"<p>Every so often you come across a requirement that can be solved by an &#8216;old&#8217; technology. Yes, us old &#8216;bru&#8217;s&#8217; still can make a difference here and there&#8230; I needed a way to display formatted data that can also be easily edited, saved, printed, exported. Sure, html can be used as well but it is sometimes a shlep manipulating the data inside the application and it is sorta bloated. Years ago &#8211; even before html existed (or was widely used) there was a standard document format called Rich Text Format. It was compact, can be read\/written by most word processors, and do lots of funky stuff. Even today Visual Studio has a control that specifically make use of this format.<\/p>\n<p>Unlike html the rtf raw format isn&#8217;t exactly easy to read or understand but you don&#8217;t need to be an expert to use it. In VS you can simply use the the RTF control to programatically add and format text (on a side-note the format also allow embedding images and other stuff). This works fine for smallish documents. If however you need to create a big or long document you need to go &#8216;raw&#8217; or use this library I discovered on <a title=\"RTFBuilder\" href=\"http:\/\/www.codeproject.com\/KB\/string\/RTFBuilder.aspx\" target=\"_blank\">Codeproject<\/a>.\u00a0 This library actually implements a way so you can build up a document similar to a StringBuilder &#8211; this makes it very fast. Using it is also fairly easy as it implements an Method based formatting concept. This is best explained with an example:<\/p>\n<pre style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">RTFBuilder\u00a0<\/span>rtfBuilder\u00a0=\u00a0<span style=\"color: #0000ff;\">new\u00a0<\/span>RTFBuilder();\r\n<span style=\"color: #008000;\">\/\/the following line adds a line of text using the font Courier New size 10<\/span>\r\nrtfBuilder.Font(<span style=\"color: #0000ff;\">RTFFont<\/span>.CourierNew).FontSize(10).AppendLine(\"Some text\");\r\n<span style=\"color: #008000;\">\/\/the following line adds a bold line of text<\/span>\r\nrtfBuilder.FontStyle(<span style=\"color: #0000ff;\">FontStyle<\/span>.Bold).AppendLine(\"Some bold text\");\r\n<span style=\"color: #008000;\">\/\/the following line write blue text on a black background<\/span>\r\nrtfBuilder.ForeColor(<span style=\"color: #0000ff;\">Color<\/span>.Blue).BackColor(<span style=\"color: #0000ff;\">Color<\/span>.Black).AppendLine(\"Blue on black\");\r\n\r\n<span style=\"color: #008000;\">\/\/then if you want to see what the raw rtf text looks like<\/span>\r\nstring\u00a0rtfData\u00a0=\u00a0rtfBuilder.ToString();\r\n<span style=\"color: #008000;\">\/\/then finally you can set the rtftext of an RTFControl like this:<\/span>\r\nrtfControl.Rtf\u00a0=\u00a0rtfData;<\/pre>\n<p>Really simple hey?<\/p>\n<p>So what is this good for? Well since the content is now inside an rtf control it can now be edited by a user &#8211; yes that is right, the rtf control allows for editing the content just like a plain old text box except you can also change the formatting &#8211; in fact, using the rtf control you can build a fully working word processor since that is what it was intended to do! Once you&#8217;re done editing or formatting the control allows for saving or easily exporting the content. Printing is still a bit tricky but even for that there are examples on the web. Remember that you can copy &amp; paste formatted text from and to the rtf control as well. Now, try to do all that with html and a web browser control! hehe<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Every so often you come across a requirement that can be solved by an &#8216;old&#8217; technology. Yes, us old &#8216;bru&#8217;s&#8217; still can make a difference here and there&#8230; I needed a way to display formatted data that can also be &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/hen.co.za\/blog\/2011\/04\/rtfbuilder\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-55","post","type-post","status-publish","format-standard","hentry","category-development"],"_links":{"self":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/55","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/comments?post=55"}],"version-history":[{"count":6,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/55\/revisions"}],"predecessor-version":[{"id":61,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/55\/revisions\/61"}],"wp:attachment":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/media?parent=55"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/categories?post=55"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/tags?post=55"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}