Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert rtf to plain text
#1
how to convert rtf to plain text?
pi
#2
here's what I do.

Macro Unformat Clipboard
Code:
Copy      Help
spe
str a b
a.getclip
a.setclip
'Cv
An old blog on QM coding and automation.

The Macro Hook
#3
that does not work.

i have rtf code which needs to be converted.

Code:
Copy      Help
{\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\fnil\fcharset0 Segoe UI;}}
\viewkind4\uc1\pard\lang1031\f0\fs18 DGA Genius 6 ausgeschrieben.\par
Gesch\'fctzt bei DGA von: \par
Heldner Industrievertretungen\par
K\'f6nigsbr\'fccker Landstr. 25\par
01109 Dresden\par
Tel:    +49 (351) 8902301\par
Fax:   +49 (351) 8902302\par
Funk: +49 (172) 3505656\par
01723505656@vodafone.de <mailto:01723505656@vodafone.de>\par
\par
Ansprechpartner: Hr. Dietmar Heldner\par
}
pi
#4
oh, mine was assuming you had the text itself copied into the clipboard before stripping it.
An old blog on QM coding and automation.

The Macro Hook
#5
this should work

int hwnd=CreateWindowEx(0 "RichEdit20W" 0 0 ...) ;;create rich edit control
str file=... ;;temp file
rtf.setfile(file)
RichEditLoad(hwnd file) ;;load rtf into the control
text.getwintext(hwnd) ;;get text from the control
DestroyWindow hwnd
#6
that rtf output comes from my csharp application, i tried a 3rd party component. now some of my text has such rtf code..
pi
#7
can that be done without any dialog?
string rtf=s.get.sel
out rtf.plainText
pi
#8
Create hidden rich text control without parent dialog.

Other solution would be to use Word through COM. But it would run hidden Word process temporarily.
#9
i thought that you have a simple stripper class.
similar to removing all html tags from _s
pi
#10
I tried this, but it doesnt work

Macro Macro23
Code:
Copy      Help
int hwnd=CreateWindowEx(0 "RichEdit20W" 0 0 0 0 0 0 0 0 0 0) ;;create rich edit control
str file_tmp="$desktop$\t.txt" ;;temp file
str file_rtf="$desktop$\b.rtf"
str rtf.getfile(file_rtf)
rtf.setfile(file_tmp)
RichEditLoad(hwnd file_tmp) ;;load rtf into the control
str text.getwintext(hwnd) ;;get text from the control
DestroyWindow hwnd
out text

But I think the hwnd isn't properly declared.
(or did I mis something else?)
#11
Function RtfToText
Code:
Copy      Help
;/
function $rtf str&text [flags] ;;flags: 1 rtf is file

;Converts RTF text to simple text.

;rtf - RTF text or file path.
;text - receives simple text.


int hwnd=CreateWindowExW(0 L"RichEdit20W" 0 WS_CHILD|ES_MULTILINE 0 0 0 0 HWND_MESSAGE 0 _hinst 0) ;;note: need WS_CHILD, else getwintext gets empty text

if flags&1
,if(!RichEditLoad(hwnd rtf)) end ERR_FAILED
else if !empty(rtf)
,SETTEXTEX t
,if(!SendMessageW(hwnd EM_SETTEXTEX &t rtf)) end ERR_FAILED

text.getwintext(hwnd)
DestroyWindow hwnd

test
Macro Macro2471
Code:
Copy      Help
out
str file_rtf="$desktop$\b.rtf"
str rtf.getfile(file_rtf)
;out rtf;ret

str text
RtfToText rtf text
;RtfToText file_rtf text 1
out text

Function RtfToText2
Code:
Copy      Help
;/
function $rtf str&text [flags] ;;flags: 1 rtf is file

;Converts RTF text to simple text.

;rtf - RTF text or file path.
;text - receives simple text.

;REMARKS
;The same as RtfToText. Uses EM_STREAMIN instead of EM_EM_SETTEXTEX.


int hwnd=CreateWindowExW(0 L"RichEdit20W" 0 WS_CHILD|ES_MULTILINE 0 0 0 0 HWND_MESSAGE 0 _hinst 0) ;;note: need WS_CHILD, else getwintext gets empty text

if flags&1
,if(!RichEditLoad(hwnd rtf)) end ERR_FAILED
else if !empty(rtf)
,INTLPSTR k.s=rtf; k.i=len(rtf)
,EDITSTREAM e.pfnCallback=&sub.EditStreamCallback; e.dwCookie=&k
,SendMessageW(hwnd EM_STREAMIN SF_RTF &e)

text.getwintext(hwnd)
DestroyWindow hwnd


#sub EditStreamCallback
function# dwCookie !*pbBuff cb *pcb

INTLPSTR& k=+dwCookie
int n=iif(cb<k.i cb k.i)
if(n) memcpy pbBuff k.s n; k.i-n; k.s+n
*pcb=n
#12
Wow! Thank you!!!
#13
I think this post write very good!


Forum Jump:


Users browsing this thread: 1 Guest(s)