Page 1 of 1

How to obtain plain text from a RTF control?

PostPosted: Wed Oct 19, 2022 3:11 am
by hua
Which DATA or METHOD in a rtf class should be used to get the content in plain text (without changing the actual content)?
TIA

Re: How to obtain plain text from a RTF control?

PostPosted: Wed Oct 19, 2022 6:18 am
by Jimmy
hi,

i have search but not found Constant EM_SETTEXTMODE under FiveWin :o

you can try this HB_FUNC()
Code: Select all  Expand view
//        RichEditBox_SetRTFTextMode ( hWndControl, lRTF )
HB_FUNC ( RICHEDITBOX_SETRTFTEXTMODE )
{
   HWND hWndControl = (HWND) HMG_parnl (1);
   BOOL lRTF        = ( HB_ISLOG (2) ? (BOOL) hb_parl (2) : TRUE );
   LONG Mode        = ( lRTF ? TM_RICHTEXT : TM_PLAINTEXT ) | TM_MULTILEVELUNDO | TM_MULTICODEPAGE;
   SendMessage ( hWndControl, EM_SETTEXTMODE, (WPARAM) Mode, 0 );
}
 

mode can be
TM_PLAINTEXT
TM_RICHTEXT

https://learn.microsoft.com/de-de/windows/win32/controls/em-settextmode

Re: How to obtain plain text from a RTF control?

PostPosted: Wed Oct 19, 2022 6:50 am
by cnavarro
Dear Jimmy
The function that you indicate can only be used if the control is empty, that is, if it does not already contain any text, to indicate to the control the type of text that it is going to contain.
(Correct me if my opinion is not correct)

From:
https://learn.microsoft.com/en-us/windo ... ettextmode
Sets the text mode or undo level of a rich edit control. The message fails if the control contains any text.


What the companion requests is to be able to remove all the formatting tags used by the control and obtain the text contained in the control without any formatting ( plain text ).
I will study if it is possible, although the method does exist
METHOD SaveAsHtml( cFile ) CLASS TRichEdit5

that is possible, make it easy to get the text

There is also the possibility of using Word ( OLE ) which has the function "SAVE AS TEXT WITHOUT FORMAT"

Re: How to obtain plain text from a RTF control?

PostPosted: Wed Oct 19, 2022 7:39 am
by cnavarro
This is a function that does a good conversion (still not perfect though)

Code: Select all  Expand view


#include "Fivewin.ch"

Function Main()

   ? SaveRtfAsTxt( ".\Tutorial2.rtf" )

Return nil

//----------------------------------------------------------------------------//

Function SaveRtfAsTxt( cFile )

   local aTags
   local cTag
   local cRtf
   local aElems
   local x
   local y

   if File( cFile )
      cRtf   := hb_MemoRead( cFile )
      aTags  := hb_RegExAll( "{\*?\\.+(;})|\s?\\[A-Za-z0-9]+|\s?{\s?\\[A-Za-z0-9]+\s?|\s?}\s?", cRtf )
      XBrowse( aTags )
      For each aElems in aTags
         For x = 1 to Len( aElems )
            if Valtype( aElems[ x ] ) = "A"
               For y = 1 to Len( aElems[ x ] )
                  if Valtype( aElems[ x ][ y ] ) = "C"
                     cTag := aElems[ x ][ y ]
                     if Empty( At( "\line", cTag ) )
                        cRtf := StrTran( cRtf, cTag, " " )
                     else
                        cRtf := StrTran( cRtf, cTag, CRLF )
                     endif
                  else
               
                  endif
               Next y
            else
               cTag  := aElems[ x ]
               if Empty( At( "\line", cTag ) )
                  cRtf := StrTran( cRtf, cTag, " " )
               else
                  cRtf := StrTran( cRtf, cTag, CRLF )
               endif
            endif
         Next x
      Next
   endif

Return cRtf
 

Re: How to obtain plain text from a RTF control?

PostPosted: Wed Oct 19, 2022 7:53 am
by Jimmy
hi,

what if you "select" hole RTF and copy into Clipboard and paste back to "Memoedit" (TMultiGet ? )

Re: How to obtain plain text from a RTF control?

PostPosted: Wed Oct 19, 2022 7:59 am
by cnavarro
Yes, that is another possibility that I was studying

Re: How to obtain plain text from a RTF control?

PostPosted: Wed Oct 19, 2022 8:14 am
by hua
Could we access DATA oRtf.text mentioned here? https://stackoverflow.com/questions/595 ... n-rtf-text

Re: How to obtain plain text from a RTF control?

PostPosted: Wed Dec 28, 2022 8:49 am
by hua
Thanks Cristobal.
I have tested this function and it is able to meet my need.
I just added a line at the end that strip out chr(0) because for some reason some of the RTF text that I used for testing has that embedded in it, causing empty() to always returns .f. eventhough there is nothing that can be seen.

cnavarro wrote:This is a function that does a good conversion (still not perfect though)

Code: Select all  Expand view


#include "Fivewin.ch"

Function Main()

   ? SaveRtfAsTxt( ".\Tutorial2.rtf" )

Return nil

//----------------------------------------------------------------------------//

Function SaveRtfAsTxt( cFile )

   local aTags
   local cTag
   local cRtf
   local aElems
   local x
   local y

   if File( cFile )
      cRtf   := hb_MemoRead( cFile )
      aTags  := hb_RegExAll( "{\*?\\.+(;})|\s?\\[A-Za-z0-9]+|\s?{\s?\\[A-Za-z0-9]+\s?|\s?}\s?", cRtf )
      XBrowse( aTags )
      For each aElems in aTags
         For x = 1 to Len( aElems )
            if Valtype( aElems[ x ] ) = "A"
               For y = 1 to Len( aElems[ x ] )
                  if Valtype( aElems[ x ][ y ] ) = "C"
                     cTag := aElems[ x ][ y ]
                     if Empty( At( "\line", cTag ) )
                        cRtf := StrTran( cRtf, cTag, " " )
                     else
                        cRtf := StrTran( cRtf, cTag, CRLF )
                     endif
                  else
               
                  endif
               Next y
            else
               cTag  := aElems[ x ]
               if Empty( At( "\line", cTag ) )
                  cRtf := StrTran( cRtf, cTag, " " )
               else
                  cRtf := StrTran( cRtf, cTag, CRLF )
               endif
            endif
         Next x
      Next
   endif

Return cRtf
 

Re: How to obtain plain text from a RTF control?

PostPosted: Wed Dec 28, 2022 9:04 am
by Antonio Linares
hua wrote:Which DATA or METHOD in a rtf class should be used to get the content in plain text (without changing the actual content)?
TIA


Have you tried using oRichEdit:GetText() ?