Using InsertTable( nRows, nCols )method of Trichedit
How insert data info on each cells?
For a sample I have an array aschema is 3 rows with 10 columns
How create a table on Rtf with this array ?
Function CreateRtf()
LOCAL nRow, nCol
LOCAL cFileName := "table.rtf"
LOCAL oFile := FCreate(cFileName, 0)
// RTF Header
FWrite(oFile, "{\rtf1\ansi")
FOR nRow := 1 TO 4
// Start a new row
FWrite(oFile, "{\trowd")
// Define cell positions and borders for 10 columns
FOR nCol := 1 TO 10
FWrite(oFile, "\clbrdrt\brdrw10\brdrs")
FWrite(oFile, "\clbrdrl\brdrw10\brdrs")
FWrite(oFile, "\clbrdrr\brdrw10\brdrs")
FWrite(oFile, "\clbrdrb\brdrw10\brdrs")
FWrite(oFile, "\cellx" + LTrim(Str(nCol * 1000)))
NEXT
// Fill cells with data
FOR nCol := 1 TO 10
FWrite(oFile, "\intbl Cell " + LTrim(Str(nRow)) + "-" + LTrim(Str(nCol)) + "\cell")
NEXT
// End of the row
FWrite(oFile, "\row}")
NEXT
// RTF Footer
FWrite(oFile, "}")
// Close the file
FClose(oFile)
? "RTF file created:", cFileName
RETURN
anserkk wrote:Please try this code. Modify it according to your requirements.
- Code: Select all Expand view
Function CreateRtf()
LOCAL nRow, nCol
LOCAL cFileName := "table.rtf"
LOCAL oFile := FCreate(cFileName, 0)
// RTF Header
FWrite(oFile, "{\rtf1\ansi")
FOR nRow := 1 TO 4
// Start a new row
FWrite(oFile, "{\trowd")
// Define cell positions and borders for 10 columns
FOR nCol := 1 TO 10
FWrite(oFile, "\clbrdrt\brdrw10\brdrs")
FWrite(oFile, "\clbrdrl\brdrw10\brdrs")
FWrite(oFile, "\clbrdrr\brdrw10\brdrs")
FWrite(oFile, "\clbrdrb\brdrw10\brdrs")
FWrite(oFile, "\cellx" + LTrim(Str(nCol * 1000)))
NEXT
// Fill cells with data
FOR nCol := 1 TO 10
FWrite(oFile, "\intbl Cell " + LTrim(Str(nRow)) + "-" + LTrim(Str(nCol)) + "\cell")
NEXT
// End of the row
FWrite(oFile, "\row}")
NEXT
// RTF Footer
FWrite(oFile, "}")
// Close the file
FClose(oFile)
? "RTF file created:", cFileName
RETURN
// Insert a paragraph
FWrite(oFile, "This is a sample paragraph containing whatever text you want to add.\par")
// Table start
// RTF Header with font table and color table
FWrite(oFile, "{\rtf1\ansi")
FWrite(oFile, "{\fonttbl{\f0 Arial;}}") // Font table definition, Arial is at index 0
FWrite(oFile, "{\colortbl;\red0\green0\blue255;}") // Color table definition, Blue color is at index 1
// Insert a paragraph with specific font and color
FWrite(oFile, "\f0\cf1 This is a sample paragraph in Arial font and blue color.\par")
// Reset to default font and color
FWrite(oFile, "\f0\cf0 ")
// For BMP
FWrite(oFile, "\intbl{\pict\wmetafile8\picw" + LTrim(Str(nImageWidth)) + "\pich" + LTrim(Str(nImageHeight)) + "\picwgoal" + LTrim(Str(nImageWidthGoal)) + "\pichgoal" + LTrim(Str(nImageHeightGoal)) + " " + cHexImage + "}\cell")
//For JPG
FWrite(oFile, "\intbl{\pict\jpegblip\picw" + LTrim(Str(nImageWidth)) + "\pich" + LTrim(Str(nImageHeight)) + "\picwgoal" + LTrim(Str(nImageWidthGoal)) + "\pichgoal" + LTrim(Str(nImageHeightGoal)) + " " + cHexImage + "}\cell")
anserkk wrote:To insert a paragraph before the table in the RTF document, you can simply write the text before starting the table construction in the RTF code. In RTF, paragraphs are generally marked by the \par control word.
- Code: Select all Expand view
// Insert a paragraph
FWrite(oFile, "This is a sample paragraph containing whatever text you want to add.\par")
// Table start
To use different color and fonts in the RTF file, you need to define color and fonts
- Code: Select all Expand view
// RTF Header with font table and color table
FWrite(oFile, "{\rtf1\ansi")
FWrite(oFile, "{\fonttbl{\f0 Arial;}}") // Font table definition, Arial is at index 0
FWrite(oFile, "{\colortbl;\red0\green0\blue255;}") // Color table definition, Blue color is at index 1
// Insert a paragraph with specific font and color
FWrite(oFile, "\f0\cf1 This is a sample paragraph in Arial font and blue color.\par")
// Reset to default font and color
FWrite(oFile, "\f0\cf0 ")
I understand that to insert an image file, first, you need to convert the image file to a Hexa decimal string and then insert it.
- Code: Select all Expand view
// For BMP
FWrite(oFile, "\intbl{\pict\wmetafile8\picw" + LTrim(Str(nImageWidth)) + "\pich" + LTrim(Str(nImageHeight)) + "\picwgoal" + LTrim(Str(nImageWidthGoal)) + "\pichgoal" + LTrim(Str(nImageHeightGoal)) + " " + cHexImage + "}\cell")
//For JPG
FWrite(oFile, "\intbl{\pict\jpegblip\picw" + LTrim(Str(nImageWidth)) + "\pich" + LTrim(Str(nImageHeight)) + "\picwgoal" + LTrim(Str(nImageWidthGoal)) + "\pichgoal" + LTrim(Str(nImageHeightGoal)) + " " + cHexImage + "}\cell")
Function CreateRtf(aTable,c1,c2,oFile)
LOCAL nRow, nCol
// RTF Header
FWrite(oFile, "{\rtf1\ansi")
// RTF Header with font table and color table
FWrite(oFile, "{\fonttbl{\f0 Arial;}}") // Font table definition, Arial is at index 0
// Insert a paragraph
FWrite(oFile, c1+"\par")
// Insert a paragraph with specific font and color
FWrite(oFile, "\f0\cf1 "+c2+"\par")
// Color table definition, Blue color is at index 1
FWrite(oFile, "{\colortbl;\red0\green0\blue255;}")
FWrite(oFile,"\f"+"Arial"+"\fs"+"50")
FOR nRow := 1 TO 3
// Start a new row
FWrite(oFile, "{\trowd")
// Define cell positions and borders for 10 columns
FOR nCol := 1 TO 9
FWrite(oFile, "\clbrdrt\brdrw10\brdrs")
FWrite(oFile, "\clbrdrl\brdrw10\brdrs")
FWrite(oFile, "\clbrdrr\brdrw10\brdrs")
FWrite(oFile, "\clbrdrb\brdrw10\brdrs")
FWrite(oFile, "\cellx" + LTrim(Str(nCol * 1000)))
NEXT
// Fill cells with data
FOR nCol := 1 TO 9
FWrite(oFile, "\intbl " +aTable[nRow][nCol] + "\cell")
NEXT
// Reset to default font and color
FWrite(oFile, "\f0\cf0 ")
// End of the row
FWrite(oFile, "\row}")
NEXT
RETURN
MMK wrote:Twenty seven years ago Thomas R. Marchione wrote a class for creation rtf of files
// Copyright: (C) 01/28/97 1997, Thomas R. Marchione
oRTF := SetRT( cOutFile )
// Use this to write an entire paragraph, with optional formatting.
NEW PARAGRAPH oRTF TEXT 'OOO «Text in aligned left»';
FONTNUMBER 1 ;
APPEARANCE BOLD_OFF+ITALIC_OFF+CAPS_OFF;
FONTSIZE 12 ;
FONTCOLOR CLR_WHITE,CLR_NBLUE ;
ALIGN LEFT ;
SPACEBEFORE .12 ;
SETDEFAULT
// Use this to begin a new table
DEFINE NEWTABLE oRTF ; // Specify the RTF object
ALIGN LEFT ; // Center table horizontally on page
FONTNUMBER 2 ; // Use font #2 for the body rows
FONTSIZE 10 ; // Use 10 Pt. font for the body rows
CELLAPPEAR BOLD_OFF ; // Normal cells unbolded
CELLHALIGN LEFT ; // Text in normal cells aligned left
COLUMNS 10 ; // Table has n Columns
CELLWIDTHSээ ancho1 ; // Array of column widths
ROWHEIGHT .25 ; // Minimum row height is .25"
ROWBORDERS DOTTED ;
CELLBORDERS SINGLE ; // Outline cells with thin border
COLSHADE aMarc1; // Sombras en columnas
HEADERROWS 1; // dos lineas
HEADER {"","","Cartella n. 3","","","","","Mario","","" };
HEADERAPPEAR BOLD_ON;
HEADERHALIGN CENTER ;
HEADERFONTSIZE 10
HEADERSHADE 0;
HEADERJOIN {{1,6},{7,9} },{}; // Aqui cascaba el MSWORD. Las celdas
DEFINE CELL FORMAT oRTF ;
CELLSHADE aMarc1
lFormato:=.F.
// Use this to write the next cell in a table
For i=1 to 3
WRITE NEWCELL oRTF TEXT "cell 1" ALIGN CENTER
WRITE NEWCELL oRTF TEXT "cell 2" ALIGN RIGHT FONTCOLOR CLR_RED,CLR_WHITE
. . . . . . . . . . . . . . .
WRITE NEWCELL oRTF TEXT "cell 10" ALIGN CENTER
Next
END TABLE oRTF
CLOSE RTF oRtf
Puti:=MSWORD_PATH()
WinExec(puti+" "+ cOutFile)
MMK wrote:Twenty seven years ago Thomas R. Marchione wrote a class for creation rtf of files
// Copyright: (C) 01/28/97 1997, Thomas R. Marchione
oRTF := SetRT( cOutFile )
// Use this to write an entire paragraph, with optional formatting.
NEW PARAGRAPH oRTF TEXT 'OOO «Text in aligned left»';
FONTNUMBER 1 ;
APPEARANCE BOLD_OFF+ITALIC_OFF+CAPS_OFF;
FONTSIZE 12 ;
FONTCOLOR CLR_WHITE,CLR_NBLUE ;
ALIGN LEFT ;
SPACEBEFORE .12 ;
SETDEFAULT
// Use this to begin a new table
DEFINE NEWTABLE oRTF ; // Specify the RTF object
ALIGN LEFT ; // Center table horizontally on page
FONTNUMBER 2 ; // Use font #2 for the body rows
FONTSIZE 10 ; // Use 10 Pt. font for the body rows
CELLAPPEAR BOLD_OFF ; // Normal cells unbolded
CELLHALIGN LEFT ; // Text in normal cells aligned left
COLUMNS 10 ; // Table has n Columns
CELLWIDTHSээ ancho1 ; // Array of column widths
ROWHEIGHT .25 ; // Minimum row height is .25"
ROWBORDERS DOTTED ;
CELLBORDERS SINGLE ; // Outline cells with thin border
COLSHADE aMarc1; // Sombras en columnas
HEADERROWS 1; // dos lineas
HEADER {"","","Cartella n. 3","","","","","Mario","","" };
HEADERAPPEAR BOLD_ON;
HEADERHALIGN CENTER ;
HEADERFONTSIZE 10
HEADERSHADE 0;
HEADERJOIN {{1,6},{7,9} },{}; // Aqui cascaba el MSWORD. Las celdas
DEFINE CELL FORMAT oRTF ;
CELLSHADE aMarc1
lFormato:=.F.
// Use this to write the next cell in a table
For i=1 to 3
WRITE NEWCELL oRTF TEXT "cell 1" ALIGN CENTER
WRITE NEWCELL oRTF TEXT "cell 2" ALIGN RIGHT FONTCOLOR CLR_RED,CLR_WHITE
. . . . . . . . . . . . . . .
WRITE NEWCELL oRTF TEXT "cell 10" ALIGN CENTER
Next
END TABLE oRTF
CLOSE RTF oRtf
Puti:=MSWORD_PATH()
WinExec(puti+" "+ cOutFile)
#include "fivewin.ch"
#include "richtext.ch"
// need the RichText class of Thomas R. Marchione
Function RftDemo()
local cOutFile := "RTFDEMO.RTF"
local oRTF := SetupRTF( cOutFile )
local aTable:={}
AaDd(aTable,{1,,23,,44,,61,,80} )
AaDd(aTable,{,16,,36,,59,68,,89} )
AaDd(aTable,{,,28,,49,58,,71,84} )
MakeTable( oRTF, cFile, lLandScape,aTable )
// Close the output file
CLOSE RTF oRTF
FW_MemoEdit( cOutFile, "test", 10, 10, 50, 100, .t., , .t. )
RETURN NIL
STATIC FUNCTION SetupRTF( cOutFile)
LOCAL oRTF
DEFINE RTF oRTF FILE cOutFile ;
FONTS "Times New Roman Cyr", "Arial Cyr", "Courier New Cyr" ;
FONTSIZE 12 ;
TWIPFACTOR 1440
// Trim trailing spaces from data, to save file space.
oRTF:lTrimSpaces := .T.
DEFINE PAGESETUP oRTF MARGINS 1.75, 1.75, 1, 1 ;
TABWIDTH .5 ;
ALIGN CENTER
BEGIN HEADER oRTF
NEW PARAGRAPH oRTF TEXT "Sample RTF Output" ;
FONTSIZE 14 ;
ALIGN CENTER
END HEADER oRTF
BEGIN FOOTER oRTF
NEW PARAGRAPH oRTF TEXT DTOC( DATE() ) ;
FONTSIZE 12 ;
ALIGN CENTER
END HEADER oRTF
RETURN oRTF
STATIC FUNCTION MakeTable( oRTF, cFile, lLandScape,aTable )
LOCAL i, nWidth
LOCAL nTotWidth := 0, cName
local nRow,ncol
// Begin a new section of the document
IF lLandScape
NEW SECTION oRTF ;
LANDSCAPE ;
PAGEWIDTH 11 ;
PAGEHEIGHT 8.5 ;
MARGINS .5, .5, .5, .5 ;
ALIGN CENTER ;
SETDEFAULT
ELSE
NEW SECTION oRTF ;
PAGEWIDTH 8.5 ;
PAGEHEIGHT 11 ;
MARGINS .5, .5, .5, .5 ;
ALIGN CENTER ;
SETDEFAULT
ENDIF
// Define the table
DEFINE TABLE oRTF ; // Specify the RTF object
ALIGN CENTER ; // Center table horizontally on page
FONTNUMBER 2 ; // Use font #2 for the body rows
FONTSIZE 50 ; // Use 9 Pt. font for the body rows
CELLAPPEAR BOLD_OFF ; // Normal cells unbolded
CELLHALIGN LEFT ; // Text in normal cells aligned left
COLUMNS 9 ; // Table has n Columns
CELLWIDTHS 20 ; // Array of column widths
ROWHEIGHT .25 ; // Minimum row height is .25"
CELLBORDERS SINGLE // Outline cells with thin border
// Write the data rows
For nRow= 1 to Len(aTable)
FOR nCol := 1 TO oRTF:nTblColumns
WRITE CELL oRTF TEXT aTable[nRow][nCol]
NEXT
next
// Close the table
CLOSE TABLE oRTF
RETURN NIL
*********************** END OF DBFToRTF() *********************
Otto wrote:Hello,
Where do we find richtext.ch?
Best regards,
Otto
Otto wrote:C:\fwh2023\samples\tblrtf.prg(2) Error F0029 Can't open #include file: 'richtext.ch'
On my system there is no richtext.ch. I have richedit.ch but no richtext.ch
Return to FiveWin for Harbour/xHarbour
Users browsing this forum: No registered users and 46 guests