Search code examples
c#htmlrtf

Stripping rtf string to return html data in c#


I have the below rtf string

{\rtf1\ansi\ansicpg1252\fromhtml1 \fbidis \deff0{\fonttbl
{\f0\fswiss Arial;}
{\f1\fmodern Courier New;}
{\f2\fnil\fcharset2 Symbol;}
{\f3\fmodern\fcharset0 Courier New;}}
{\colortbl\red0\green0\blue0;\red0\green0\blue255;}
\uc1\pard\plain\deftab360 \f0\fs24 
{\*\htmltag19 <html>}
{\*\htmltag34 <head>}
{\*\htmltag1 \par }
{\*\htmltag1 \par }
{\*\htmltag41 </head>}
{\*\htmltag2 \par }
{\*\htmltag50 <body>}
{\*\htmltag0 \par }
{\*\htmltag96 <div>}\htmlrtf {\htmlrtf0 
{\*\htmltag112 <br>}\htmlrtf \par
\htmlrtf0 
{\*\htmltag0 \par }
{\*\htmltag112 <br>}\htmlrtf \par
\htmlrtf0 
{\*\htmltag0 \par }
{\*\htmltag112 <br>}\htmlrtf \par
\htmlrtf0 
{\*\htmltag0 \par }
{\*\htmltag96 <div style="width:100%;height:20px;">}\htmlrtf {\htmlrtf0 {\*\htmltag64}\htmlrtf {\htmlrtf0 
{\*\htmltag148 <span style="white-space:nowrap;color:#5F5F5F;opacity:.36;">}\htmlrtf {\htmlrtf0 ________________________________________________________________________________
{\*\htmltag156 </span>}\htmlrtf }\htmlrtf0 
{\*\htmltag4 \par }\htmlrtf  \htmlrtf0 {\*\htmltag72}\htmlrtf\par}\htmlrtf0

{\*\htmltag104 </div>}\htmlrtf }\htmlrtf0 
{\*\htmltag0 \par }
{\*\htmltag96 <div class="me-email-text" lang="en-US" style="color:#252424;font-family:'Segoe UI','Helvetica Neue',Helvetica,Arial,sans-serif;">}\htmlrtf {\htmlrtf0 
{\*\htmltag0 \par }
{\*\htmltag96 <div style="margin-top:24px;margin-bottom:20px;">}\htmlrtf {\htmlrtf0 {\*\htmltag64}\htmlrtf {\htmlrtf0 
{\*\htmltag148 <span style="font-size:24px;color:#252424;">}\htmlrtf {\htmlrtf0 Microsoft Teams meeting
{\*\htmltag156 </span>}\htmlrtf }\htmlrtf0 
{\*\htmltag4 \par }\htmlrtf  \htmlrtf0 {\*\htmltag72}\htmlrtf\par}\htmlrtf0

{\*\htmltag104 </div>}\htmlrtf }\htmlrtf0 
{\*\htmltag0 \par }
{\*\htmltag96 <div style="margin-bottom:20px;">}\htmlrtf {\htmlrtf0 
{\*\htmltag0 \par }
{\*\htmltag96 <div style="margin-top:0px;margin-bottom:0px;font-weight:bold;">}\htmlrtf {\htmlrtf0 {\*\htmltag64}\htmlrtf {\htmlrtf0 
{\*\htmltag148 <span style="font-size:14px;color:#252424;">}\htmlrtf {\htmlrtf0 Join on your computer or mobile app
{\*\htmltag156 </span>}\htmlrtf }\htmlrtf0 
{\*\htmltag4 \par }\htmlrtf  \htmlrtf0 {\*\htmltag72}\htmlrtf\par}\htmlrtf0

{\*\htmltag104 </div>}\htmlrtf }\htmlrtf0 
{\*\htmltag0 \par }{\*\htmltag64}\htmlrtf {\htmlrtf0 {}
{\*\htmltag84 <a href="https://teams.microsoft.com/l/meetup-join/19%3ameeting_NmEzYjM0YTAtMzljNC00YWUyLTk0NWQtYmQxYTAxNWRhOWFi%40thread.v2/0?context=%7b%22Tid%22%3a%2204930c22-8e5a-4c3c-88d9-5d040b723d74%22%2c%22Oid%22%3a%2265834454-14e9-4f1e-9b41-3a214f3f285c%22%7d" class="me-email-headline" style="font-size:14px;font-family:'Segoe UI Semibold','Segoe UI','Helvetica Neue',Helvetica,Arial,sans-serif;text-decoration:underline;color:#6264a7;" title="https://teams.microsoft.com/l/meetup-join/19%3ameeting_nmezyjm0ytatmzljnc00ywuyltk0nwqtymqxytaxnwrhowfi%40thread.v2/0?context=%7b%22tid%22%3a%2204930c22-8e5a-4c3c-88d9-5d040b723d74%22%2c%22oid%22%3a%2265834454-14e9-4f1e-9b41-3a214f3f285c%22%7d" target="_blank" rel="noreferrer noopener">}\htmlrtf {\field{\*\fldinst{HYPERLINK "https://teams.microsoft.com/l/meetup-join/19%3ameeting_NmEzYjM0YTAtMzljNC00YWUyLTk0NWQtYmQxYTAxNWRhOWFi%40thread.v2/0?context=%7b%22Tid%22%3a%2204930c22-8e5a-4c3c-88d9-5d040b723d74%22%2c%22Oid%22%3a%2265834454-14e9-4f1e-9b41-3a214f3f285c%22%7d" \\n}}{\fldrslt\cf1\ul \htmlrtf0 Click
{\*\htmltag4 \par }\htmlrtf  \htmlrtf0 
{\*\htmltag4  }here to join the meeting\htmlrtf }\htmlrtf0 \htmlrtf }\htmlrtf0 
{\*\htmltag92 </a>} {\*\htmltag72}\htmlrtf\par}\htmlrtf0

{\*\htmltag104 </div>}\htmlrtf }\htmlrtf0 
{\*\htmltag0 \par }
{\*\htmltag96 <div style="margin-bottom:24px;margin-top:20px;">}\htmlrtf {\htmlrtf0 {\*\htmltag64}\htmlrtf {\htmlrtf0 {}
{\*\htmltag84 <a href="https://aka.ms/JoinTeamsMeeting" class="me-email-link" style="font-size:14px;text-decoration:underline;color:#6264a7;font-family:'Segoe UI','Helvetica Neue',Helvetica,Arial,sans-serif;" title="https://aka.ms/jointeamsmeeting" target="_blank" rel="noreferrer noopener">}\htmlrtf {\field{\*\fldinst{HYPERLINK "https://aka.ms/JoinTeamsMeeting" \\n}}{\fldrslt\cf1\ul \htmlrtf0 Learn
{\*\htmltag4 \par }\htmlrtf  \htmlrtf0 
{\*\htmltag4  }More\htmlrtf }\htmlrtf0 \htmlrtf }\htmlrtf0 
{\*\htmltag92 </a>} | {}
{\*\htmltag84 <a href="https://teams.microsoft.com/meetingOptions/?organizerId=65834454-14e9-4f1e-9b41-3a214f3f285c&amp;tenantId=04930c22-8e5a-4c3c-88d9-5d040b723d74&amp;threadId=19_meeting_NmEzYjM0YTAtMzljNC00YWUyLTk0NWQtYmQxYTAxNWRhOWFi@thread.v2&amp;messageId=0&amp;language=en-US" class="me-email-link" style="font-size:14px;text-decoration:underline;color:#6264a7;font-family:'Segoe UI','Helvetica Neue',Helvetica,Arial,sans-serif;" title="https://teams.microsoft.com/meetingoptions/?organizerid=65834454-14e9-4f1e-9b41-3a214f3f285c&amp;tenantid=04930c22-8e5a-4c3c-88d9-5d040b723d74&amp;threadid=19_meeting_nmezyjm0ytatmzljnc00ywuyltk0nwqtymqxytaxnwrhowfi@thread.v2&amp;messageid=0&amp;language=en-us" target="_blank" rel="noreferrer noopener">}\htmlrtf {\field{\*\fldinst{HYPERLINK "https://teams.microsoft.com/meetingOptions/?organizerId=65834454-14e9-4f1e-9b41-3a214f3f285c&tenantId=04930c22-8e5a-4c3c-88d9-5d040b723d74&threadId=19_meeting_NmEzYjM0YTAtMzljNC00YWUyLTk0NWQtYmQxYTAxNWRhOWFi@thread.v2&messageId=0&language=en-US" \\n}}{\fldrslt\cf1\ul \htmlrtf0 
{\*\htmltag4 \par }Meeting options\htmlrtf }\htmlrtf0 \htmlrtf }\htmlrtf0 
{\*\htmltag92 </a>} {\*\htmltag72}\htmlrtf\par}\htmlrtf0

{\*\htmltag104 </div>}\htmlrtf }\htmlrtf0 
{\*\htmltag0 \par }
{\*\htmltag104 </div>}\htmlrtf }\htmlrtf0 
{\*\htmltag0 \par }
{\*\htmltag96 <div style="font-size:14px;margin-bottom:4px;font-family:'Segoe UI','Helvetica Neue',Helvetica,Arial,sans-serif;">}\htmlrtf {\htmlrtf0 
{\*\htmltag0 \par }
{\*\htmltag104 </div>}\htmlrtf }\htmlrtf0 
{\*\htmltag0 \par }
{\*\htmltag96 <div style="font-size:12px;">}\htmlrtf {\htmlrtf0 
{\*\htmltag104 </div>}\htmlrtf }\htmlrtf0 
{\*\htmltag0 \par }
{\*\htmltag104 </div>}\htmlrtf }\htmlrtf0 
{\*\htmltag0 \par }
{\*\htmltag96 <div style="width:100%;height:20px;">}\htmlrtf {\htmlrtf0 {\*\htmltag64}\htmlrtf {\htmlrtf0 
{\*\htmltag148 <span style="white-space:nowrap;color:#5F5F5F;opacity:.36;">}\htmlrtf {\htmlrtf0 ________________________________________________________________________________
{\*\htmltag156 </span>}\htmlrtf }\htmlrtf0 
{\*\htmltag4 \par }\htmlrtf  \htmlrtf0 {\*\htmltag72}\htmlrtf\par}\htmlrtf0

{\*\htmltag104 </div>}\htmlrtf }\htmlrtf0 
{\*\htmltag0 \par }
{\*\htmltag96 <div>}\htmlrtf {\htmlrtf0 
{\*\htmltag104 </div>}\htmlrtf }\htmlrtf0 
{\*\htmltag0 \par }
{\*\htmltag58 </body>}
{\*\htmltag2 \par }
{\*\htmltag27 </html>}
{\*\htmltag3 \par }}

the above string should be an html string writing in rtf format is there any c# code I can use to string the rtf tags and return the html?

I have used this but it didn't return the preferred results: https://chrisbenard.net/2014/08/20/extract-text-from-rtf-in-c-net/


Solution

  • Probably the easiest way is to use a RichTextBox control from System.Windows.Forms:

    using System.Windows.Forms;
    
    // ---
    
    public static string ParseRTF(string rtf)
    {
        return new RichTextBox { Rtf = rtf }.Text;
    }
    

    Sample usage would look something like below, where the rtf string from your sample is saved in a text file called "c:\temp\test.rtf":

    static void Main(string[] args)
    {
        string parsedRTF = ParseRTF(File.ReadAllText(@"c:\temp\test.rtf"));
    
        Console.WriteLine(parsedRTF);
        Console.ReadKey();
    }
    

    Output:

    ________________________________________________________________________________ 
    Microsoft Teams meeting 
    Join on your computer or mobile app 
    Click here to join the meeting <https://teams.microsoft.com/l/meetup-join/19%3ameeting_NmEzYjM0YTAtMzljNC00YWUyLTk0NWQtYmQxYTAxNWRhOWFi%40thread.v2/0?context=%7b%22Tid%22%3a%2204930c22-8e5a-4c3c-88d9-5d040b723d74%22%2c%22Oid%22%3a%2265834454-14e9-4f1e-9b41-3a214f3f285c%22%7d> 
    Learn More <https://aka.ms/JoinTeamsMeeting> | Meeting options <https://teams.microsoft.com/meetingOptions/?organizerId=65834454-14e9-4f1e-9b41-3a214f3f285c&tenantId=04930c22-8e5a-4c3c-88d9-5d040b723d74&threadId=19_meeting_NmEzYjM0YTAtMzljNC00YWUyLTk0NWQtYmQxYTAxNWRhOWFi@thread.v2&messageId=0&language=en-US>
    ________________________________________________________________________________
    

    Read more here: How to: Convert RTF to Plain Text (C# Programming Guide)