How to obtain plain text from a RTF control?

Post Reply
hua
Posts: 1075
Joined: Fri Oct 28, 2005 2:27 am
Has thanked: 1 time
Been thanked: 1 time

How to obtain plain text from a RTF control?

Post 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
FWH 11.08/FWH 19.12
BCC5.82/BCC7.3
xHarbour/Harbour
User avatar
Jimmy
Posts: 1740
Joined: Thu Sep 05, 2019 5:32 am
Location: Hamburg, Germany
Has thanked: 2 times

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

Post 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

//        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
greeting,
Jimmy
User avatar
cnavarro
Posts: 6557
Joined: Wed Feb 15, 2012 8:25 pm
Location: España
Been thanked: 3 times

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

Post 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"
Cristobal Navarro
Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo
El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
User avatar
cnavarro
Posts: 6557
Joined: Wed Feb 15, 2012 8:25 pm
Location: España
Been thanked: 3 times

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

Post by cnavarro »

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

Code: Select all | Expand



#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
 
Cristobal Navarro
Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo
El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
User avatar
Jimmy
Posts: 1740
Joined: Thu Sep 05, 2019 5:32 am
Location: Hamburg, Germany
Has thanked: 2 times

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

Post by Jimmy »

hi,

what if you "select" hole RTF and copy into Clipboard and paste back to "Memoedit" (TMultiGet ? )
greeting,
Jimmy
User avatar
cnavarro
Posts: 6557
Joined: Wed Feb 15, 2012 8:25 pm
Location: España
Been thanked: 3 times

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

Post by cnavarro »

Yes, that is another possibility that I was studying
Cristobal Navarro
Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo
El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
hua
Posts: 1075
Joined: Fri Oct 28, 2005 2:27 am
Has thanked: 1 time
Been thanked: 1 time

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

Post by hua »

Could we access DATA oRtf.text mentioned here? https://stackoverflow.com/questions/595 ... n-rtf-text
FWH 11.08/FWH 19.12
BCC5.82/BCC7.3
xHarbour/Harbour
hua
Posts: 1075
Joined: Fri Oct 28, 2005 2:27 am
Has thanked: 1 time
Been thanked: 1 time

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

Post 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


#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
 
FWH 11.08/FWH 19.12
BCC5.82/BCC7.3
xHarbour/Harbour
User avatar
Antonio Linares
Site Admin
Posts: 42520
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 31 times
Been thanked: 75 times
Contact:

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

Post 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() ?
regards, saludos

Antonio Linares
www.fivetechsoft.com
Post Reply