Results 1 to 5 of 5

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

Threaded View

  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

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
  •  



Click Here to Expand Forum to Full Width