Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rich Text: Combining multiple RTF text into One

Status
Not open for further replies.

Sammybobo

Programmer
Apr 4, 2003
87
US
I need to have multiple rich text boxes on one form and combine them together as one large RTF box in another form (or pageframe). When I tried to combine them only the first referenced text shows. Is there a way to do this and will the solution work even when each of the text boxes are formatted differently?
 
According to this page:
An RTF is formatted like this: { header document }
Which seems like the whole document is contained within a pair of braces. If you were to concatenate two together, it would look like this: { header document }{ header document }

so, the first document would be seen, and most parsers would stop at the closing brace matching the first open brace.

In my tests, there are an equal number of "{" and "}" characters in the RTF files, so I think the {HEADER DOCUMENT} formula is recursive, to some extent.

I would think the minimum required would be to remove the trailing "}" from the first file, append the second file, and append a new final "}", but in testing, MS Word didn't like the resulting file... it must've recognized/been confused by the second header.

So, the real trick would seem to be:
1) Ensure that the two RTF files have compatible headers (One function of the header is to define abbreviations for fonts... the abbreviation is used through the rest of the file)
2) Trim the header off the second file
3) insert the second file just before the first file's final "}"

I wasn't able to determine how to locate the end of the header, though...

- Bill

Get the best answers to your questions -- See FAQ481-4875.
 
This is by no means well-vetted code, but it may get you going in the right direction (It works on very simple RTF files, but when I tried it on more complicated ones, the second file was hidden until I manually removed another pair of braces... so it clearly needs work. If you get it working reliably, it'd be nice to have it posted back here for others...):
Code:
PROCEDURE ParseRTF
LPARAMETERS tcRtfContents
CREATE CURSOR cRtfCode ( NestLev I, ;
  Code C(200), Doc M )
  
lnLevel = 0
lcCode  = ''
lcDoc   = ''
llInCode = .f. 
FOR lnI = 1 TO LEN(tcRtfContents)
  lcChr = SUBSTR(tcRtfContents,lnI,1)
  DO CASE
    CASE lcChr='{'
      lnLevel = lnLevel + 1 
      IF NOT EMPTY(lcCode+lcDoc) 
        INSERT INTO cRtfCode ( NestLev, Code, Doc ) ;
          VALUES (lnLevel,lcCode,lcDoc)
        lcCode = ''
        lcDoc  = ''
      ENDIF 
      llInCode = .T. 
      
    CASE lcChr='}'
      lnLevel = lnLevel - 1 
      IF NOT EMPTY(lcCode+lcDoc) 
        INSERT INTO cRtfCode ( NestLev, Code, Doc ) ;
          VALUES (lnLevel,lcCode,lcDoc)
      ENDIF 
      lcCode = ''
      lcDoc  = ''
      
    CASE llInCode AND EMPTY(lcChr)
      llInCode = .F.
      
    CASE llInCode
      lcCode = lcCode + lcChr
      
    OTHERWISE  && NOT llInCode
      lcDoc = lcDoc + lcChr
  ENDCASE
  
ENDFOR
RETURN lnLevel=0
ENDPROC

PROCEDURE ConcatRTF
LPARAMETERS tcOne, tcTwo
lcFirstFile = FILETOSTR(tcOne)
* Trim off final "}"
lnAt = RAT([}],lcFirstFile)
lcFirstFile = left(lcFirstFile,lnAt-1)

* Load second RTF:
lcSecondFile = FILETOSTR(tcTwo)
if not ParseRTF( lcSecondFile )
  ? 'Error!!'
  suspend
endif

* Find the start of the body:
locate for '\pard' $ cRTFcode.doc

* Paste the body onto the first:
lcSecondFile = ''
lnLevel      = cRTFCode.NestLev
SCAN REST
  IF lnLevel>cRTFCode.NestLev
    lcSecondFile = lcSecondFile + [}]
    lnLevel = lnLevel - 1 
  ENDIF 
  if empty(cRTFcode.Code)
    lcSecondFile = lcSecondFile + cRTFcode.doc
  else
    lcSecondFile = lcSecondFile + [{] + cRTFcode.code
    lnLevel      = lnLevel + 1 
    if not empty(cRTFcode.Doc)
      lcSecondFile = lcSecondFile + [ ] + cRTFcode.doc
    endif 
  ENDIF
ENDSCAN
RETURN lcFirstFile+lcSecondFile+'}'
ENDPROC


Simply call it like this to concatenate files:
Code:
STRTOFILE( ConcatRTF('one.rtf','two.rtf'), 'concat.rtf' )

- Bill

Get the best answers to your questions -- See FAQ481-4875.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top