Link to home
Start Free TrialLog in
Avatar of darek1211
darek1211Flag for United States of America

asked on

ASP.NET writing RTF code, extra white spaces in source text create a "Â" character in document.

I'm writing an RTF file (report), and am running into an issue where (what appears to be white spaces in the SQL record) multiple spaces turn into the "Â" letter/symbol.

So for example in SQL (varchar(MAX)) I see:

<p>Elem- Reading comprehension (quad Dates)<br />

and through the code below I see (in the resulting rtf/doc flie i write):

Elem- Reading comprehension  (quad Dates)


(Sustain past efforts)<br />
            Elementary needs to clarify actions and results- may back away from this until ICC

(Sustain past efforts)
            Elementary needs to clarify actions and results- may back away from this until ICC

I'm not sure what it is that i'm missing.  I'm thinking perhaps those aren't white spaces stored in there and RTF is interpreting the actual codes differently.  So perhaps I need to add something more to my replace function below?

When i open the resulting rtf file and view the code for it, I see no mention of any special characters at those locations, just white spaces.
   Private Sub CreateReportCustom()
	'edited for brevity
        'compile sql string
        Session("SQLString") = PutSQLStringTogether()
        If Session("SQLStringError") = False Then
            Dim sqlcmd As New SqlCommand(Session("SQLString"), conn)
            Dim da As SqlDataAdapter = New SqlDataAdapter(sqlcmd)
            Dim DS As DataSet = New DataSet
            'grab matching data
                da.Fill(DS, "ResultTable")
            Catch ex As Exception
            End Try
            'check if DS has data then write the header
            If DS.Tables("ResultTable").Rows.Count > 0 Then
                For x As Integer = 0 To DS.Tables("ResultTable").Rows.Count - 1
                    With DS.Tables("ResultTable")
                        If x = 0 Then
                            '//////////////////////// first row write district label
                            MyFile.WriteLine("\par\fs24\cf0\ql\b\cf2 " + .Rows(x)("District").ToString() + " \b0\par")
                            MyFile.WriteLine("\par\fs18\cf0\ql\b NEEDS: \b0\cf2 " + Trim(ReplaceCharacters(.Rows(x)("Needs").ToString())) + " \par")
                        End If
                    End With
                'closing RTF statements
                MyFile.WriteLine("}")                  'End Table
                'close the RTF string and file
                MyFile.WriteLine("}")                 'finally closes the rtf codes, file is readable after this
                lblSearchResult.Text += ", Closed file"
            End If
        End If
    End Sub
    Private Sub WriteRTFdocHeader(ByVal filename As String)
        txtFileName = DateTime.Now.Year.ToString + DateTime.Now.Millisecond.ToString + filename
        path = Server.MapPath(".") & "\" & txtFileName
        If File.Exists(path) Then
        End If
        MyFile = File.AppendText(path)
        MyFile.WriteLine("{\rtf1")        'CAN ONLY WRITE ONCE FOR THE WHOLE DOCUMENT AT THE START
        'Write the color table (for use in background and foreground colors)
        sRTF = "{\colortbl;\red0\green0\blue0;\red0\green0\blue255;" & _
         "\red0\green255\blue255;\red0\green255\blue0;\red255\green0\blue255;\red255\green0\blue0;" & _
        'Write the font table (only care about arial)
        sRTF = "\deff1{\fonttbl\f1{\fswiss};}"       
        'set page margins in twips
        'Write the title and author for the document properties
        MyFile.WriteLine("{\info{\title AEA13 Partnership Plan Report}" & _
          "{\author Loess Hills AEA 13 - AER}}")
        MyFile.WriteLine("{\header\pard{\fs24{" & _
        "\trowd\trhdr\trgaph0\trleft0\trrh0\trftsWidth3" & _
        "\cellx10800" & _
        "\pard\intbl\qc\b\ Super Duper Report \b0\cell" & _
        'write the page footer
        MyFile.WriteLine("{\footer\pard\qc\brdrt\brdrs\brdrw10\brsp100" & _
          "\fs18" + txtUpdatedStamp + "    /   Page " & _
          "{\field{\*\fldinst PAGE}{\fldrslt 1}} of " & _
          "{\field{\*\fldinst NUMPAGES}{\fldrslt 1}}\par}")
    End Sub
   Private Function ReplaceCharacters(ByVal TextToChange As String)
        ' various typographic quote characters
        Const LeftSingleQuote As Char = ChrW(&H2018)
        Const RightSingleQuote As Char = ChrW(&H2019)
        Const LeftDoubleQuote As Char = ChrW(&H201C)
        Const RightDoubleQuote As Char = ChrW(&H201D)
        Dim newtext As String = TextToChange
        'newtext = newtext.Replace(ControlChars.NewLine, " \line ")
        newtext = newtext.Replace("<p>", "")
        newtext = newtext.Replace("</p>", "")
        newtext = newtext.Replace("<br />", " \line ")
        newtext = newtext.Replace(";", " \line ")
        'newtext = newtext.Replace("  ", "\~ ")
        newtext = newtext.Replace(LeftDoubleQuote, " \ldblquote ")
        newtext = newtext.Replace(RightDoubleQuote, " \rdblquote ")
        newtext = newtext.Replace(LeftSingleQuote, " \lquote ")
        newtext = newtext.Replace(RightSingleQuote, " \rquote ")
        Return newtext
    End Function

Open in new window

Avatar of MonkeyPushButton
Flag of United Kingdom of Great Britain and Northern Ireland image

My hunch would be that it's an encoding problem.

You're writing unicode strings to your file, and the file viewer you're using is most likely interpreting the file using a different encoding. If you're using notepad to view the resulting file, try changing

MyFile = File.AppendText(path)


MyFile = StreamWriter(path, System.Text.Encoding.GetEncoding("windows-1252"))
Avatar of MonkeyPushButton
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of darek1211


Thank you Monkey :)
you were correct, it was an encoding issue.  I used:

MyFile = New StreamWriter(path, True, System.Text.Encoding.GetEncoding("windows-1252"))

and the text came out perfectly!
Thank you, i will now keep my remaining hair.
Avatar of learningnet

hello folks,

i have a similar problem i think, please can someone look into this for me ?