I needed a program to convert some RTF files to plain text I seen a few but they did not do what I wanted however I decided to make my own little function. This code will take in a input RTF file and convert it to plain text as this is all I really wanted.
If I have time I try and make a proper GUI for this function.
Anyway use the code as you like hope it be of some use, Comments and suggestions welcome..
vbnet Code:
Imports System.Text
Imports System.IO
Public Class Form1
Private Function rtf2txt(ByVal Source As String, ByVal Output As String) As Boolean
Dim rbox As New RichTextBox()
Dim sb As New StringBuilder()
Dim sw As StreamWriter
If Not File.
Exists(Source
) Then Return False
Else
'Open richtext source file.
rbox.LoadFile(Source, RichTextBoxStreamType.RichText)
'Append text to string builder.
sb.Append(rbox.Text).Replace(vbLf, vbCrLf)
Try
'Create new output file.
sw = New StreamWriter(Output)
'Write plain text
sw.Write(sb.ToString())
'Close file.
sw.Close()
'Clear up
sb = Nothing
Catch ex As Exception
Return False
End Try
End If
'Return good result.
Return True
End Function
vbnet Code:
Private Sub cmdConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdConvert.Click
'Example.
Dim Done As Boolean = rtf2txt("C:\out\data\Document.rtf", "C:\out\data\Document.txt")
'Just show user if convert was done ok.
MessageBox.Show("Convert ok = " & Done.ToString(), "rtf2txt", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Sub