[.NET 3.5+] RtfBuilder (Build Custom RTF Strings Similar To StringBuilder)-VBForums
Results 1 to 5 of 5

Thread: [.NET 3.5+] RtfBuilder (Build Custom RTF Strings Similar To StringBuilder)

  1. #1

    Thread Starter
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    [.NET 3.5+] RtfBuilder (Build Custom RTF Strings Similar To StringBuilder)

    The picture and code should be pretty self-explanatory. For this to work you will need to add a reference to the .dll that gets compiled from the attached project. The .dll namespace is "System.Text.Rtf" and all the types are contained in there. I am OCD about namespaces and how references appear in the IDE so I try to always extend an existing Windows namespace so it looks nice .

    I will not help you if you post that it simply "doesn't work".

    This is the code that produces the example:
    Code:
    Imports System.Text.Rtf
    
    Public Class Form1
    
        Private Sub demoButton_Click(ByVal sender As System.Object, _
                                     ByVal e As System.EventArgs) _
                                     Handles demoButton.Click
    
            Using builder = New RtfBuilder(MyBase.Font, MyBase.ForeColor)
    
                With builder
                    .Append("This class can help you build ")
    
                    '//new font gets added automatically
                    .Append("basic RTF strings.", FontStyle.Bold Or _
                                                  FontStyle.Underline Or _
                                                  FontStyle.Italic)
                    .AppendLine(2)
                    .AppendLine("This class has been fully documented and is" & _
                                " built using the StringBuilder as a template.")
                    .AppendLine()
    
                    '//new font and color gets added automatically
                    .AppendLine("*Notes*", FontStyle.Italic, Color.Blue)
    
                    .AppendLine("   1) the builder does not take references to " & _
                                       "any fonts you add, instead it creates a copy of them")
                    .Append("   2) because of this, you should take care to " & _
                                       "always call ")
                    .Append("Dispose", FontStyle.Underline, Color.Blue)
                    .Append(" or use a ")
                    .Append("Using", FontStyle.Underline, Color.Blue)
                    .AppendLine(" block.")
                    .Append("   3) currently functionality is limited to ")
                    .Append("fonts, ", FontStyle.Bold Or FontStyle.Italic Or FontStyle.Underline)
                    .Append(" t", Color.Red)
                    .Append("e", Color.Orange)
                    .Append("x", Color.Yellow)
                    .Append("t ", Color.Green)
                    .Append("c", Color.Blue)
                    .Append("o", Color.Indigo)
                    .Append("l", Color.Violet)
                    .Append("o", Color.Brown)
                    .Append("r", Color.Gray)
                    .Append(", and ")
                    .Append("text highlighting", FontStyle.Regular, SystemColors.ControlText, _
                            Color.White, Color.Yellow)
                    .AppendLine(".")
                    .AppendLine(2)
                    .AppendLine("Some of the methods include:")
    
                    Dim blue = .Header.Colors.IndexOf(Color.Blue)
    
                    '//if you know a bit about how RTF works, you can add specific RTF
                    .AppendRtf("    \ul\cf{0}Append\par\ulnone", blue)
                    .AppendRtf("    \ul\cf{0}AppendFormat\par\ulnone", blue)
                    .AppendRtf("    \ul\cf{0}AppendLine\par\ulnone", blue)
                    .AppendRtf("    \ul\cf{0}AppendRtf\par\ulnone", blue)
    
                    .AppendLine()
                    .Append("Obviously, these are not the ")
                    .Append("only", FontStyle.Bold)
                    .Append(" available methods.")
                End With
    
                Me.RichTextBox1.Rtf = builder.ToString()
            End Using
    
        End Sub
    
    End Class
    Any decent RTF parser should be able to convert the outputted RTF to the same as the above. All of the code is fully documented. The builder is designed to use the Western European code page and uses the ANSI specification. If you want to learn about RTF and how the document and control words work, look here.

    If you want to append specific RTF use the AppendRtf method, otherwise, the other methods will escape the special characters.

    Any problems let me know.
    Attached Images Attached Images  
    Attached Files Attached Files

  2. #2
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 2008
    Location
    On the Internet
    Posts
    3,250

    Re: [.NET 3.5+] RtfBuilder (Build Custom RTF Strings Similar To StringBuilder)

    ...whoa...it looks like you've been busy! Great little codebank addition!

  3. #3

    Thread Starter
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: [.NET 3.5+] RtfBuilder (Build Custom RTF Strings Similar To StringBuilder)

    Quote Originally Posted by formlesstree4 View Post
    ...whoa...it looks like you've been busy! Great little codebank addition!
    Thanks!

  4. #4
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [.NET 3.5+] RtfBuilder (Build Custom RTF Strings Similar To StringBuilder)

    This looks very useful! I don't have any use for it at the moment but I'll keep it in mind if I ever do need to use RTF
    Good coding too, kept to common coding standards, very easy to read, useful XML comments and a LOT of overloads for all of the Append methods, great job!

  5. #5
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: [.NET 3.5+] RtfBuilder (Build Custom RTF Strings Similar To StringBuilder)

    That looks great! Nice work, and I especially like the namespace

    (I'll definitely rate you for it when I can.)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.