Printing a pre-formated letter

Printing a pre-formated letter

Postby Rick Lipkin » Wed Feb 18, 2009 11:43 pm

To All

I have text stored in VarChar(max) SQL Server field and I have no problem importing and exporting the text in Notepad.

I want to be able to print the text and have it come out the way it is formatted in the field .. when I look at the data like this :

Code: Select all  Expand view

cTEXT := oRsLett:Fields("letter_text"):Value
MsgInfo( cTEXT )
 


MsgInfo displays the text correctly formatted perfectically .. just as it was inserted into the SQL table .. however, I am trying to print this USING

Code: Select all  Expand view

oPRINT:StartPage()

cDEFA := SET(7)
IF FILE( cDEFA+"\AGENCY.BMP" )
   oPRINT:SayBitMap( 10,500, cDEFA+"\AGENCY.BMP", 900,700 )    //800,500
ENDIF

oPrint:Say ( 1000, 100, cTEXT , oFONT )

oPRINT:EndPage()
 


and all I get is the first line that runs off the page .. surely there must be a way to print formatted text easily at certain co-ordanents on a page ??

Thanks
Rick Lipkin
User avatar
Rick Lipkin
 
Posts: 2615
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA


Re: Printing a pre-formated letter

Postby Rick Lipkin » Thu Feb 19, 2009 2:13 pm

Enrico

I tried your suggestion with the same result .. I had some old code from extracting letters from a .fpt memo field and I thought I would give it a shot and it worked .. here is the code .. a bit crude, but it works :

Code: Select all  Expand view


cTEXT := oRsLett:Fields("letter_text"):Value
oRsLett:Close()

oPRINT:lPrvModal := .T.

DEFINE FONT oFont NAME "Courier New" SIZE 0,-10 BOLD of oPRINT  // SIZE 10 -40

nLINES := 68

nRowStep := oPrint:nVertRes() / nLINES  // pixel per line = 57.65 @ 55
nColStep := oPrint:nHorzRes() /80       // columns

nBREAK := ( nRowStep * nLINES ) -;  // 57.65 * 55 = 3170.75 pixels resolution
          ( nRowStep * 5 )          // back off 5 lines

Line   := 0
nPAGE  := 1

oPRINT:StartPage()

xLINES := MLCount( cTEXT, 70 )
LINE := 5

IF EMPTY( cTEXT )
ELSE

   FOR i = 1 to xLINES
       pLINE := "    "+ALLTRIM(HardCr(MemoLine( cTEXT, 70, i )))
        prntline( pLINE )

       IF LINE >= nBREAK
          LINE := 0
          oPRINT:EndPage()
          oPRINT:StartPage()
          nPAGE++
          Line += nROWSTEP
          Line += nROWSTEP
       ENDIF
   NEXT
ENDIF

oPRINT:EndPage()
CursorArrow()

IF mVIEW = 'V'
   oPRINT:Preview()
ELSE
   PrintEnd()
ENDIF

oFONT:END()
RELEASE FONT oFont

MsgInfo( "Automated Letter sent to the Printer" )

RETURN(.T.)

//-----------------
Static Func prntline( pLINE )

LOCAL i, nCOL

nCOL := 0
FOR i = 1 to LEN(pLINE)
    oPRINT:SAY ( LINE, nCOL, SUBSTR(pLINE,i,1) , oFONT )
    nCOL += nColStep
NEXT
Line += nRowStep

RETURN(.T.)


 
User avatar
Rick Lipkin
 
Posts: 2615
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA

Re: Printing a pre-formated letter

Postby Enrico Maria Giordano » Thu Feb 19, 2009 5:00 pm

Please build a reduced and self-contained sample of what you are trying to do. Please note that CmSay() means "Centimeters Say", ie. you have to provide the coordinates in centimeters.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8315
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Printing a pre-formated letter

Postby Rick Lipkin » Thu Feb 19, 2009 9:21 pm

Enrico

What I have done is stored a complete document in a SQL Varchar(max) field .. what that does is stores the data exactically as it was put in .. for example, I have a complete letter with 3 paragraphs of text .. I can store it directly to the field like any other field where cLETTER is that variable that contains the formatted text using Notepad :

Code: Select all  Expand view


DO CASE
CASE cFIELD = "LETTER"
     cFILENAME := "LETTER.TXT"
     cREAD := cLETTER

     IF empty( cREAD ) .or. empty( alltrim( cREAD) )

        IF cMODE = "V"
           SAYING := "Sorry .. No Letter Text here to View"
           MsgInfo( SAYING )
           RETURN(.F.)
        ENDIF

        cREAD := " "
     ENDIF

OTHERWISE
     RETURN(.F. )
ENDCASE

FERASE( xVOL+"\DBTMP\"+cFILENAME )

nHANDLE := FCREATE(  xVOL+"
\DBTMP\"+cFILENAME, 0 )
IF FERROR() <> 0
   SAYING := "
Error Creating file "+(xVOL+"\DBTMP\"+cFILENAME)+CHR(10)
   SAYING += "
Error "+STR(FERROR())+CHR(10)
   MsgInfo( SAYING )
   RETURN(.F.)
ENDIF

FWRITE( nHANDLE, cREAD )   // write out the file
FCLOSE( nHANDLE )

SysReFresh()

WAITRUN( "
NOTEPAD " + xVOL+"\DBTMP\"+cFILENAME, 1 )

SysReFresh()

cFILE :=  xVOL+"
\DBTMP\"+cFILENAME

// open file //
nHANDLE := FOpen( cFILE )
IF FERROR() <> 0
   SAYING := "
Error reading file "+cFILE+CHR(10)
   SAYING += "
"+STR(FERROR())+CHR(10)
   FERASE( xVOL+"
\DBTMP\"+cFILENAME )
   MsgInfo( SAYING )
   RETURN(.F.)
ENDIF

// get number of bytes in file
nBYTES := FSEEK( nHANDLE, 0,2 )

// pad the buffer nBytes+1
cBUFFER := SPACE(nBYTES+1)

FSeek( nHANDLE, 0, 0 )
nBytesRead   := FRead( nHANDLE, @cBuffer, nBytes )

FClose( nHANDLE )
FERASE( xVOL+"
\DBTMP\"+cFILENAME )

SysReFresh()

lOK := .F.

IF cMODE = "
V"
ELSE
   DO CASE
   CASE cFIELD = "
LETTER"

        IF nBYTESREAD < 2  // blank file

          cBUFFER     := "
"
          lLETTER     := .F.
          lOK         := .F.

          cLETPROMPT := "
LETTER Text  [not found]"
          oBtn3:cCAPTION := cLETPROMPT
          oBtn3:SetColor( "
R+/W*" )
          oBtn3:ReFresh()
          SysReFresh()

       ELSE

          lLETTER     := .T.
          lOK         := .T.
          cLETPROMPT := "
LETTER Text  [read me]"
          oBtn3:cCAPTION := cLETPROMPT
          oBtn3:SetColor( "
G/W*" )
          oBTN3:ReFresh()
          SysReFresh()
       ENDIF

       cLETTER := cBUFFER

   OTHERWISE
        RETURN(.F. )
   ENDCASE

   cBUFFER := NIL
ENDIF

RETURN( lOK )



Once the binary data has been stored to cLETTER .. I can write it to the sql table like this oRs:Fields("letter"):Value := cLETTER

Then when I extract the data all I have to do is store the binary data to another variable like cLETTER := oRs:Fields("letter"):Value

NOW, cLETTER contains the complete formatted text just as it was typed in Notepad .. formatted correctly and in three paragraphs... and you can test this and look at the document like :

MsgInfo( cLETTER )

and it will look just like the letter formatted perfectically.

The problem I had was dumping the entire contents of cLETTER to the printer .. SAY and cmSay extract the entire variable and print one continious line on the page and it looks nothing like the document ..

That is why I had to parce up cLETTER into individual lines so I could get the entire document to print each line.

Hope that made sense ??

Thanks
Rick Lipkin
User avatar
Rick Lipkin
 
Posts: 2615
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA

Re: Printing a pre-formated letter

Postby Enrico Maria Giordano » Thu Feb 19, 2009 10:23 pm

Understood. I think you have to use MLCOUNT()/MEMOLINE() to get and print each single line from your text.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8315
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Printing a pre-formated letter

Postby Rick Lipkin » Thu Feb 19, 2009 11:47 pm

Enrico

Actually, I was pleasantly suprised that this code snipit worked

Code: Select all  Expand view

xLINES := MLCount( cTEXT, 100 )
LINE := 5

IF EMPTY( cTEXT )
ELSE

   FOR i = 1 to xLINES
       pLINE := "    "+MemoLine( cTEXT, 100, i )
        prntline( pLINE )

       IF LINE >= nBREAK
          LINE := 0
          oPRINT:EndPage()
          oPRINT:StartPage()
          nPAGE++
          Line += nROWSTEP
          Line += nROWSTEP
       ENDIF
   NEXT
ENDIF


 


After my amazement that the above code worked .. I took out the hardcr() and the Alltrim because the paragraphs were indented .. adjusting the LINE variable .. I was able to place the whole block of text exacticly where I wanted it ..

Rick Lipkin
User avatar
Rick Lipkin
 
Posts: 2615
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA



Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 58 guests