locked
Print formatted RichTextbox text using RDLC reports. RRS feed

  • Question

  • Hi,

    I want to print the contents of the text area (richtextbox) using rdlc reporting. I am doing this in .net framework 4.0 (not WPF). After googling I got to know that rdlc does not support printing of the rich text contents but it can print the HTNL text.

    Please can anybody help me to convert the RTF text in to HTML so I can print the formatted text.

     

    Thanks in advance

    Regards

    Shyam Gadge

     

    • Moved by CoolDadTx Thursday, December 22, 2011 2:51 PM Winforms related (From:Visual C# General)
    Thursday, December 22, 2011 9:03 AM

Answers

  • Hi ,Shyam Gadge
    I have found this sample in code project.com

    Basically, just throw this function into your VB.NET project. You'll need to include a reference to the Microsoft Word 12.0 Object Library (COM object). Other Word libraries may do just as well, but this is how I've used it.

     

    Public Function sRTF_To_HTML(ByVal sRTF As String) As String
        'Declare a Word Application Object and a Word WdSaveOptions object
        Dim MyWord As Microsoft.Office.Interop.Word.Application
        Dim oDoNotSaveChanges As Object = _
             Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges
        'Declare two strings to handle the data
        Dim sReturnString As String = ""
        Dim sConvertedString As String = ""
        Try
            'Instantiate the Word application,
            ‘set visible to false and create a document
            MyWord = CreateObject("Word.application")
            MyWord.Visible = False
            MyWord.Documents.Add()
            'Create a DataObject to hold the Rich Text
            'and copy it to the clipboard
            Dim doRTF As New System.Windows.Forms.DataObject
            doRTF.SetData("Rich Text Format", sRTF)
            Clipboard.SetDataObject(doRTF)
            'Paste the contents of the clipboard to the empty,
            'hidden Word Document
            MyWord.Windows(1).Selection.Paste()
            '…then, select the entire contents of the document
            'and copy back to the clipboard
            MyWord.Windows(1).Selection.WholeStory()
            MyWord.Windows(1).Selection.Copy()
            'Now retrieve the HTML property of the DataObject
            'stored on the clipboard
            sConvertedString = _
                 Clipboard.GetData(System.Windows.Forms.DataFormats.Html)
            'Remove some leading text that shows up in some instances
            '(like when you insert it into an email in Outlook
            sConvertedString = _
                 sConvertedString.Substring(sConvertedString.IndexOf("<html"))
            'Also remove multiple  characters that somehow end up in there
            sConvertedString = sConvertedString.Replace("Â", "")
            '…and you're done.
            sReturnString = sConvertedString
            If Not MyWord Is Nothing Then
                MyWord.Quit(oDoNotSaveChanges)
                MyWord = Nothing
            End If
        Catch ex As Exception
            If Not MyWord Is Nothing Then
                MyWord.Quit(oDoNotSaveChanges)
                MyWord = Nothing
            End If
            MsgBox("Error converting Rich Text to HTML")
        End Try
        Return sReturnString
    End Function
    

     

    Ref:

    http://www.codeproject.com/KB/string/EasyRTF_To_HTML_.aspx

    http://www.codeproject.com/KB/vb/RTFToHTML.aspx
    By
    A pathfinder..
    JoSwa..


    If you find an answer helpful, click the helpful button. If you find an answer to your question, mark it as the answer.

    Thursday, December 22, 2011 10:00 AM
  • https://skydrive.live.com/redir.aspx?cid=bb789f72272d4858&resid=BB789F72272D4858!672&parid=BB789F72272D4858!619

    This demo is based on this sample code: http://code.msdn.microsoft.com/Converting-between-RTF-and-aaa02a6e

    https://skydrive.live.com/redir.aspx?cid=bb789f72272d4858&resid=BB789F72272D4858!673&parid=BB789F72272D4858!619

    This demo is convert from this article: http://www.codeproject.com/KB/vb/RTFToHTML.aspx

    Then you can use those codes to convert RTF to HTML just according to the .Net Framework without third products dependence.


    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by Mike Dos Zhang Wednesday, December 28, 2011 6:09 AM
    Friday, December 23, 2011 1:42 PM
  • Hi,

    I got one more very good solution for converting RTF text to HTML. However it didnt worked for me because it is written in framework 2.0 and it is very big code and some other reasons.

    I thought it may help others who need it.

    http://www.codeproject.com/KB/recipes/RtfConverter.aspx

     

    Thanks and Regards

    Shyam Gadge

    Tuesday, January 3, 2012 5:29 AM

All replies

  • Hi ,Shyam Gadge
    I have found this sample in code project.com

    Basically, just throw this function into your VB.NET project. You'll need to include a reference to the Microsoft Word 12.0 Object Library (COM object). Other Word libraries may do just as well, but this is how I've used it.

     

    Public Function sRTF_To_HTML(ByVal sRTF As String) As String
        'Declare a Word Application Object and a Word WdSaveOptions object
        Dim MyWord As Microsoft.Office.Interop.Word.Application
        Dim oDoNotSaveChanges As Object = _
             Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges
        'Declare two strings to handle the data
        Dim sReturnString As String = ""
        Dim sConvertedString As String = ""
        Try
            'Instantiate the Word application,
            ‘set visible to false and create a document
            MyWord = CreateObject("Word.application")
            MyWord.Visible = False
            MyWord.Documents.Add()
            'Create a DataObject to hold the Rich Text
            'and copy it to the clipboard
            Dim doRTF As New System.Windows.Forms.DataObject
            doRTF.SetData("Rich Text Format", sRTF)
            Clipboard.SetDataObject(doRTF)
            'Paste the contents of the clipboard to the empty,
            'hidden Word Document
            MyWord.Windows(1).Selection.Paste()
            '…then, select the entire contents of the document
            'and copy back to the clipboard
            MyWord.Windows(1).Selection.WholeStory()
            MyWord.Windows(1).Selection.Copy()
            'Now retrieve the HTML property of the DataObject
            'stored on the clipboard
            sConvertedString = _
                 Clipboard.GetData(System.Windows.Forms.DataFormats.Html)
            'Remove some leading text that shows up in some instances
            '(like when you insert it into an email in Outlook
            sConvertedString = _
                 sConvertedString.Substring(sConvertedString.IndexOf("<html"))
            'Also remove multiple  characters that somehow end up in there
            sConvertedString = sConvertedString.Replace("Â", "")
            '…and you're done.
            sReturnString = sConvertedString
            If Not MyWord Is Nothing Then
                MyWord.Quit(oDoNotSaveChanges)
                MyWord = Nothing
            End If
        Catch ex As Exception
            If Not MyWord Is Nothing Then
                MyWord.Quit(oDoNotSaveChanges)
                MyWord = Nothing
            End If
            MsgBox("Error converting Rich Text to HTML")
        End Try
        Return sReturnString
    End Function
    

     

    Ref:

    http://www.codeproject.com/KB/string/EasyRTF_To_HTML_.aspx

    http://www.codeproject.com/KB/vb/RTFToHTML.aspx
    By
    A pathfinder..
    JoSwa..


    If you find an answer helpful, click the helpful button. If you find an answer to your question, mark it as the answer.

    Thursday, December 22, 2011 10:00 AM
  • I am writing to check the status of the issue on your side. 
    What about this problem now? 
    Would you mind letting us know the result of the suggestions?

    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us
    Friday, December 23, 2011 6:02 AM
  • Hi,

    Thank you for immediate reply.

    I will definitely like sharing the result with you all and thanks Mike Dos Zhang for looking in to it.

    Thanks Jo Swa for suggesting the sample, but I already tried this sample and it works fine but it uses a COM dll (Office.dll and Microsoft.Office.Interop) and it have some other issues like dependencies and licensing. That’s why I cannot use this solution.

    Please suggest me if you have any other workaround.

    Thanks in advance.

     

    Shyam Gadge.

    • Marked as answer by Gadge Shyam Monday, December 26, 2011 7:02 AM
    • Unmarked as answer by Gadge Shyam Monday, December 26, 2011 7:02 AM
    Friday, December 23, 2011 8:04 AM
  • https://skydrive.live.com/redir.aspx?cid=bb789f72272d4858&resid=BB789F72272D4858!672&parid=BB789F72272D4858!619

    This demo is based on this sample code: http://code.msdn.microsoft.com/Converting-between-RTF-and-aaa02a6e

    https://skydrive.live.com/redir.aspx?cid=bb789f72272d4858&resid=BB789F72272D4858!673&parid=BB789F72272D4858!619

    This demo is convert from this article: http://www.codeproject.com/KB/vb/RTFToHTML.aspx

    Then you can use those codes to convert RTF to HTML just according to the .Net Framework without third products dependence.


    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by Mike Dos Zhang Wednesday, December 28, 2011 6:09 AM
    Friday, December 23, 2011 1:42 PM
  • Hi,

    I got one more very good solution for converting RTF text to HTML. However it didnt worked for me because it is written in framework 2.0 and it is very big code and some other reasons.

    I thought it may help others who need it.

    http://www.codeproject.com/KB/recipes/RtfConverter.aspx

     

    Thanks and Regards

    Shyam Gadge

    Tuesday, January 3, 2012 5:29 AM
  • Thanks for your sharing!

    Best wishes,


    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, January 3, 2012 6:21 AM