RTF To Text conversion faster

Hello ,

Is there a way to convert some RTF into normal text , it can be done with XOJO ?

I can use textutil on Macos but I hoped that it could be done easier in code and maybe in memory than to write files and convert them and save them back.

thanks in advance.

Check NSAttributedStringMBS class in MBS Xojo Plugins.
There you can read RTF file and just query StringValue.

Public Function RTFtoText(aRTFvalue as String) as String
  ' converts an rtf formatted Text To a pure Text String.
  
  Dim st As new StyledText
  if st<>nil Then
    if IsRTFText( aRTFvalue) Then
      st.RTFData = aRTFvalue
    Else
      st.Text = aRTFvalue
    End If
    Return st.Text
  End If
  
End Function
Public Function IsRTFText(aText as String) as Boolean
  Return aText.BeginsWith("{\rtf1")
  
End Function

it will not work with web app (and may be also not with console) only tested on desktop app.
(as there is no styledtext nor styleruns outside of desktop app ?)

Thanks a lot, I was looking on FTC as it has similar things but so far this is the best. Thanks again.

FTC has a certainly better rtf parser, but it is fully imbricated in the rest of FTC and seems to me impossible to isolate easily…

Well unfortunately none of them work, apparently no matter what I do I see data in the raw RTF but when using your code or FTC code I end up with empty values, so no text, that is weird, if I use macOS textutil from terminal I get the proper data while none of the Xojo parts are working , guess something bad on XOJO side.

I guess a simple thing always fails while on command line it works properly, so I will end up using the command line to write to disk and properly convert that .

Based on this

{\rtf1\deff0{\fonttbl{\f0 Calibri;}{\f1 Segoe UI;}}{\colortbl ;\red0\green0\blue255 ;}{\*\defchp \fs22}{\stylesheet {\ql\fs22 Normal;}{\*\cs1\f1\fs20 Default Paragraph Font;}{\*\cs2\sbasedon1\f1\fs20 Line Number;}{\*\cs3\ul\fs22\cf1 Hyperlink;}{\*\ts4\tsrowd\fs22\ql\tscellpaddfl3\tscellpaddl108\tscellpaddfb3\tscellpaddfr3\tscellpaddr108\tscellpaddft3\tsvertalt\cltxlrtb Normal Table;}{\*\ts5\tsrowd\sbasedon4\fs22\ql\trbrdrt\brdrs\brdrw10\trbrdrl\brdrs\brdrw10\trbrdrb\brdrs\brdrw10\trbrdrr\brdrs\brdrw10\trbrdrh\brdrs\brdrw10\trbrdrv\brdrs\brdrw10\tscellpaddfl3\tscellpaddl108\tscellpaddfr3\tscellpaddr108\tsvertalt\cltxlrtb Table Simple 1;}}{\*\listoverridetable}\nouicompat\splytwnine\htmautsp\sectd\pard\plain\ql{\lang2067\langfe2067\f1\fs20\cf0 Migrated from Test}\f1\fs20\par}

On command line I get

Migrated from Test

While on Xojo code or on FTC I get nothing. blank textArea.

Any ideas why ?

Latest XOJO Latest MacOS.

Thanks

I copy/paste the rtf line into Apple’s TextEdit, saved it as rtf file and load it back. I got “Migrated from Test” (without the quotes, of course).
M1 MBP / Big Sur…

well the idea was to do all in xojo not to use the apps on apple, the rtf data is storred in db and I have few millions of them so doing that manually is not fun.

I think you wanted some test report, so I do with a know (to me) application without writing one with Xojo.

Note that the generated rtf (your code saved to disk) is different vs your rtf code…

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.