0

I need to subtract text from string with rtf format.

I have a string in rtf format

std::wstring sString= L"{\\rtf1\\ansi\\ansicpg1252\\deff0\\nouicompat\\deflang2057{\\fonttbl{\\f0\\fnil\\fprq12\\fcharset0 Times New Roman;}}\r\n{\\*\\generator Riched20 10.0.19041}\\viewkind4\\uc1 \r\n\\pard\\f0\\fs24 HELLO\\par\r\n}\r\n"

How can I get a string "HELLO" from variable sString?

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48

1 Answers1

0

Since you are on Windows (and assuming you're not looking for a cross-platform solution), there is a way to do this without using any external libraries. What you do is:

  1. Create an invisible RICHEDIT child window. Since this should be a child window, it does need a valid parent but it doesn't matter much what.

  2. Send it an EM_STREAMIN message with the SF_RTF flag set to load up your RTF. The data is actually requested from a callback function, so a bit of trickery will be needed there.

  3. Send it an EM_STREAMOUT message with the SF_TEXT flag set to retrieve the corresponding plain text. This will call your designated callback function repeatedly until there is nothing more to retrieve, so you'll probably want to append the data to a std::string or similar each time through.

  4. Destroy the window you created at step 1 (or you can keep it kicking around, if you plan to re-use it).

It's a bit fiddly to get it all right, but it's not that difficult. Be prepared to do a bit of background reading.

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48