Wednesday, February 16, 2011

Converting RTF to TXT in SSRS - Removing Funny Letters

For Example: We have blog field in a table and from there we are getting some different kind of data like...

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}{\f1\fnil Microsoft Sans Serif;}} \viewkind4\uc1\pard\f0\fs17


First Way:

public function TrimJDETags(strText as string) as String
strText = mid(strText,instr(strtext,"\fs20")+6)
strText = replace(strText, "\fs22", "")
strText = replace(strText, "d", "")
strText = replace(strText, "\ltrpar", "")
strText = replace(strText, "}", "")
strText = replace(strText, "\pard", "")
strText = replace(strText, "\slmult1", "")
strText = replace(strText, "\sl276", "")
strText=replace(strText,"^Text^","")
strText= replace(strText," ","")
strText = replace(strText,"^","")
strText = replace(strText, "\par }","")
strText = replace(strText, "\par", vbcr)
strText = replace(strText, "\tab", vbtab)
strText = replace(strText, "\b0" , "")
strText = replace(strText, "\ul" , "")
strText = replace(strText, "\b", vblf)
strText = replace(strText, "\cf0", "")
strText = replace(strText, "\cf1", "")
strText = replace(strText, "\i0", "")
strText = replace(strText, "\i", "")
strText = replace(strText, "\f0", "")
strText = replace(strText, "\f1", "")
strText = replace(strText, "\f20", "")
strText = replace(strText, "\f24", "")
strText = replace(strText, "\f2", "")
strText = replace(strText, "\f3", "")
strText = replace(strText, "\fs20", "")
strText = replace(strText, "\fs24", "")
strText = replace(strText, "\super", "")
strText = replace(strText, "\nosuper", "")
strText = replace(strText, "\cf2none" , "")
strText = replace(strText, "\cf2" , "")
strText = replace(strText, "\cf3" , "")
strText = replace(strText, "}" , "")
TrimJDETags = strText
end function

Second Method:
But for this Function need to add Assembly System.Windows.Form and also need to modify the config file rssrvpolicy (From Execution to Full Trust) in Report server folder.

Code Modified Section:

codegroup class="UnionCodeGroup" description="This code group grants default permissions for code in report expressions and Code element. " version="1" permissionsetname="FullTrust" name="Report_Expressions_Default_Permissions" imembershipcondition class="StrongNameMembershipCondition" version="1" publickeyblob="0024000004800000940000000602000000240000525341310004000001000100512C8E872E28569E733BCB123794DAB55111A0570B3B3D4DE3794153DEA5EFB7C3FEA9F2D8236CFF320C4FD0EAD5F677880BF6C181F296C751C5F6E65B04D3834C02F792FEE0FE452915D44AFE74A0C27E0D8E4B8D04EC52A8E281E01FF47E7D694E6C7275A09AFCBFD8CC82705A06B20FD6EF61EBBA6873E29C8C0F2CAEDDA2">
codegroup
codegroup class="FirstMatchCodeGroup" description="This code group grants MyComputer code Execution permission. " version="1" permissionsetname="FullTrust"
imembershipcondition class="ZoneMembershipCondition" version="1" zone="MyComputer" rtf="input.Trim">


Need to Use this Function in the Report:
Public Shared Function ConvertRtfToText(ByVal input As String) As String
Try
Dim returnValue As String = String.Empty
Using converter As New System.Windows.Forms.RichTextBox()
converter.Rtf = input.Trim
returnValue = converter.Text
End Using
Return returnValue
catch e as exception
Return e.message
finally
End try
End Function