How to obtain plain text from a RTF control?

How to obtain plain text from a RTF control?

Postby hua » Wed Oct 19, 2022 3:11 am

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
hua
 
Posts: 1038
Joined: Fri Oct 28, 2005 2:27 am

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

Postby Jimmy » Wed Oct 19, 2022 6:18 am

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
greeting,
Jimmy
User avatar
Jimmy
 
Posts: 1585
Joined: Thu Sep 05, 2019 5:32 am
Location: Hamburg, Germany

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

Postby cnavarro » Wed Oct 19, 2022 6:50 am

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: 6500
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

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

Postby cnavarro » Wed Oct 19, 2022 7:39 am

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
 
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: 6500
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

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

Postby Jimmy » Wed Oct 19, 2022 7:53 am

hi,

what if you "select" hole RTF and copy into Clipboard and paste back to "Memoedit" (TMultiGet ? )
greeting,
Jimmy
User avatar
Jimmy
 
Posts: 1585
Joined: Thu Sep 05, 2019 5:32 am
Location: Hamburg, Germany

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

Postby cnavarro » Wed Oct 19, 2022 7:59 am

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
User avatar
cnavarro
 
Posts: 6500
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

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

Postby hua » Wed Oct 19, 2022 8:14 am

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: 1038
Joined: Fri Oct 28, 2005 2:27 am

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

Postby hua » Wed Dec 28, 2022 8:49 am

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
 
FWH 11.08/FWH 19.12
BCC5.82/BCC7.3
xHarbour/Harbour
hua
 
Posts: 1038
Joined: Fri Oct 28, 2005 2:27 am

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

Postby Antonio Linares » Wed Dec 28, 2022 9:04 am

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
User avatar
Antonio Linares
Site Admin
 
Posts: 41314
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 77 guests