I have an RDLC that among other things displays text coming in that is captured in SharePoint, using a custom control (TinyMCE) that exposes limited formatting options to users.

The SSRS local report renderer supports a subset of HTML (see details here) and has limited configurability. It has some rendering behaviours that need to be worked around (e.g. if there are <p> tags, it adds some vertical padding above and below the text in the report). I am targeting PDF for reports.


Below is a function that I have embedded in the RDLC to accept a string (an HTML fragment in most cases), do the following, and return the transformed string for use in a text field within the report:

  • Convert to ASCII (if I don't do this, sometimes the first line of text appears with no spaces)
  • Do some replacement of tags as detailed in code comments
  • Decode HTML character entities


  • Performance is my main concern at this point.
  • I suspect the regexes are overkill, certainly from a performance perspective. I've read a bit about backtracking issues but not sure if this code is subject to it.
  • I am not concerned about the suitability of regex to parse HTML in this case, as it will always be well-formed. I'm aware of the normal wise counsel around doing this in general cases.

Public Function HTMLHelper(input As String) As String

Dim r As String = ""

If String.IsNullOrWhiteSpace(input) = False Then
    ' transform string to Ascii encoding (addresses problem where the first line rendered to PDF hasnospacesinitatall)
    r = System.Text.Encoding.ASCII.GetString(
                            New System.Text.EncoderReplacementFallback(String.Empty),
                            New System.Text.DecoderReplacementFallback()

    ' transformations for tags that are troublesome in SSRS
    r = System.Text.RegularExpressions.Regex.Replace(r, "(?i)</?div[^>]*>", "") ' remove DIV tags
    r = System.Text.RegularExpressions.Regex.Replace(r, "(?i)<p[^>]*>", "") ' remove opening P tags
    r = System.Text.RegularExpressions.Regex.Replace(r, "(?i)</p[^>]*>\s*(?=\Z)", "") ' remove final closing P tag
    r = System.Text.RegularExpressions.Regex.Replace(r, "(?i)</p[^>]*>", "<br>") ' replace remaining closing P tags with BR

    ' decode HTML character entities (like "&#58;" resolves to ":")
    r = System.Net.WebUtility.HtmlDecode(r)
End If

Return r

End Function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.