This is because HPDF_Page_TextRect can not automatically wrap lines when there is no split character in the string (space, comma). If the character string is too wide from the declared field size, then it is not printed, the function returns the PDF_STRING_OUT_OF_RANGE error.
option will wrap the line, even if there is no split character.
option, if the content from with the declared font size does not fit into the field, will result in adjusting the font size (by reducing it), so that the entire content fits in the field. The font sizes are adjusted to the minimum size of 4.
Code: Select all
#include "hmg.ch"
#include "harupdf.ch"
************* This part of the code can be placed in hmg_hpdf.ch **************
#xcommand @ <Row> , <Col> HPDFPRINT [ DATA ] <cText> ;
TO <ToRow> , <ToCol> ;
[ <lfont : FONT> <cFontName> ] ;
[ <lsize : SIZE> <nFontSize> ] ;
[ <bold : BOLD> [ IF <lBold> ] ] ;
[ <italic : ITALIC> [ IF <lItalic> ] ] ;
[ <underline : UNDERLINE> [ IF <lUnderline> ] ] ;
[ <strikeout : STRIKEOUT> [ IF <lStrikeout> ] ] ;
[ <lcolor : COLOR> <aColor> ] ;
[ <align:CENTER,RIGHT,JUSTIFY> ] ;
[ <wrap : WRAP> ] ;
[ <fontsizefit : FONTSIZEFIT> ] ;
=> ;
_HMG_HPDF_MULTILINE_PRINT ( <Row> , <Col> , <ToRow> , <ToCol> , <cFontName> , <nFontSize> , <aColor>\[1\] , <aColor>\[2\] , <aColor>\[3\] , <cText> , ;
<.bold.> .AND. iif( HB_IsLogical(<lBold>), <lBold>, HB_IsNil(<lBold>) ) ,;
<.italic.> .AND. iif( HB_IsLogical(<lItalic>), <lItalic>, HB_IsNil(<lItalic>) ) ,;
<.underline.> .AND. iif( HB_IsLogical(<lUnderline>), <lUnderline>, HB_IsNil(<lUnderline>) ) ,;
<.strikeout.> .AND. iif( HB_IsLogical(<lStrikeout>), <lStrikeout>, HB_IsNil(<lStrikeout>) ) ,;
<.lcolor.> , <.lfont.> , <.lsize.> , <"align">, <.wrap.>, <.fontsizefit.> )
*************** The end of this part of the code. *********************
Function Main()
Local cFonte1:= "Helvetica-Bold"
Local cEdit1 := "1XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
Local cEdit2 := "2XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
Local cEdit3 := "3XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
Local cEdit4 := "I am thinking about whether the standard function _HMG_HPDF_MULTILINE_PRINT can be forced to transfer text to the next line "+;
"when it does not fit in the declared width, not like now - by wrapping the text, but wrapping anywhere in the word."
SELECT HPDFDOC "sample.pdf" PAPERLENGTH 300 PAPERWIDTH 300 LOG
START HPDFDOC
SET HPDFDOC ENCODING TO "WinAnsiEncoding"
START HPDFPAGE
@ 95.5, 11 HPDFPRINT "DESCRICAO EQUIPAMENTO: " FONT cFonte1 size 9 COLOR { 0, 0, 0 }
@ 20, 15 HPDFPRINT RECTANGLE TO 40, 198 PENWIDTH 0.1
@ 20, 15 HPDFPRINT cEdit1 TO 40, 198 FONT cFonte1 size 9 COLOR { 0, 0, 0 } RIGHT WRAP
@ 150, 15 HPDFPRINT RECTANGLE TO 175, 198 PENWIDTH 0.1
@ 150, 15 HPDFPRINT cEdit2 TO 175, 198 FONT cFonte1 size 9 COLOR { 0, 0, 0 } JUSTIFY WRAP
@ 180, 15 HPDFPRINT RECTANGLE TO 200, 198 PENWIDTH 0.1
@ 180, 15 HPDFPRINT cEdit3 TO 200, 198 FONT cFonte1 size 9 COLOR { 0, 0, 0 } JUSTIFY
@ 205,120 HPDFPRINT "Standard 9px" FONT cFonte1 size 9
@ 205, 15 HPDFPRINT RECTANGLE TO 215, 115 PENWIDTH 0.1
@ 205, 15 HPDFPRINT cEdit4 TO 215, 115 FONT cFonte1 size 9 CENTER
@ 225,120 HPDFPRINT "Wrap 9px" FONT cFonte1 size 9
@ 225, 15 HPDFPRINT RECTANGLE TO 235, 115 PENWIDTH 0.1
@ 225, 15 HPDFPRINT cEdit4 TO 235, 115 FONT cFonte1 size 9 CENTER WRAP
@ 245,120 HPDFPRINT "Standard + Font Size Fit" FONT cFonte1 size 9
@ 245, 15 HPDFPRINT RECTANGLE TO 255, 115 PENWIDTH 0.1
@ 245, 15 HPDFPRINT cEdit4 TO 255, 115 FONT cFonte1 size 9 CENTER FONTSIZEFIT
@ 265,120 HPDFPRINT "Wrap + Font Size Fit" FONT cFonte1 size 9
@ 265, 15 HPDFPRINT RECTANGLE TO 275, 115 PENWIDTH 0.1
@ 265, 15 HPDFPRINT cEdit4 TO 275, 115 FONT cFonte1 size 9 CENTER WRAP FONTSIZEFIT
END HPDFPAGE
END HPDFDOC
Execute File 'sample.pdf'
Return Nil
************ This part of the code can be placed in h_HMG_HPDF.Prg **********
********* Change "STATIC Function _HMG_HPDF_MULTILINE_PRINT" to *************
****************** "Function _HMG_HPDF_MULTILINE_PRINT" *********************
******* Replace the existing _HMG_HPDF_MULTILINE_PRINT function *************
********************** then, HMG needs to be rebuilt ************************
STATIC Function _HMG_HPDF_MULTILINE_PRINT ( nRow, nCol, nToRow, nToCol, cFontName, nFontSize, nRColor, nGColor, nBColor, cText, lBold, lItalic, lUnderline, lStrikeout, lColor, lFont, lSize, cAlign, lWrap, lFontSizeFit )
Local nWidth := _HMG_SYSDATA[ 150 ][ 4 ]
Local nHeight := _HMG_SYSDATA[ 150 ][ 5 ]
Local nTextWidth := 0
Local nxPos := _HMG_HPDF_MM2Pixel( nCol )
Local nyPos := nHeight - _HMG_HPDF_MM2Pixel( nRow )
Local nToxPos := nxPos + _HMG_HPDF_MM2Pixel( nToCol - nCol )
Local nToyPos := nyPos - _HMG_HPDF_MM2Pixel( nToRow - nRow )
Local oFont := Nil
Local cFont := ''
Local cTtfFnt // Pablo June, 2014
Local cTextIn, cTextOut, cLine, nLineSpacingPrev, nLineSpacing, nLineCount
default cFontName := ''
//default nFontSize := 12
default nFontSize := _HMG_SYSDATA[ 150 ][ 9 ]
default nRColor := 0
default nGColor := 0
default nBColor := 0
default lBold := .f.
default lItalic := .f.
default lUnderline := .f.
default lStrikeout := .f.
default lColor := .f.
default lFont := .f.
default lSize := .f.
default cAlign := ''
default lWrap := .f.
default lFontSizeFit := .f.
cTtfFnt := cFontName // Pablo June, 2014
If _HMG_SYSDATA[ 150 ][ 1 ] == Nil // PDF object not found!
_HMG_HPDF_Error( 3 )
Return Nil
endif
If _HMG_SYSDATA[ 150 ][ 7 ] == Nil // PDF Page object not found!
_HMG_HPDF_Error( 5 )
Return Nil
endif
// set color
If lColor
HPDF_Page_SetRGBFill( _HMG_SYSDATA[ 150 ][ 7 ], nRColor/255, nGColor/255, nBColor/255 )
else
HPDF_Page_SetRGBFill( _HMG_SYSDATA[ 150 ][ 7 ], 0.0, 0.0, 0.0 )
endIf
// set font
//If HMG_LEN( alltrim( cFontName ) ) == 0
// cFontName := _HMG_HPDF_SetFont( cFontName, lBold, lItalic )
// oFont := HPDF_GetFont( _HMG_SYSDATA[ 150 ][ 1 ], cFontName, _HMG_SYSDATA[ 150 ][ 10 ] )
//else
// cFontName := alltrim( cFontName )
cFontName := AllTrim(_HMG_HPDF_SetFont( cFontName, lBold, lItalic ))
if HMG_UPPER (cFileExt (cFontName)) == '.TTF' // load ttf font
cFont := HPDF_LOADTTFONTFROMFILE( _HMG_SYSDATA[ 150 ][ 1 ], cFontName, .t. )
If HMG_LEN( alltrim( cFont ) ) == 0
_HMG_HPDF_Error( 6 , cFontName )
Return Nil
endif
oFont := HPDF_GetFont( _HMG_SYSDATA[ 150 ][ 1 ], cFont, _HMG_SYSDATA[ 150 ][ 10 ] )
else
If HMG_UPPER( alltrim( cFontName ) ) == "SYMBOL" .or. HMG_UPPER( alltrim( cFontName ) ) == "ZAPFDINGBATS"
oFont := HPDF_GetFont( _HMG_SYSDATA[ 150 ][ 1 ], cFontName, Nil )
else
oFont := HPDF_GetFont( _HMG_SYSDATA[ 150 ][ 1 ], cFontName, _HMG_SYSDATA[ 150 ][ 10 ] )
endIf
endif
//endIf
If oFont == Nil
_HMG_HPDF_Error( 6 , cFontName )
Return Nil
else
nLineSpacingPrev := HPDF_Page_GetTextLeading(_HMG_SYSDATA[ 150 ][ 7 ])
nLineSpacing := nLineSpacingPrev
IF nLineSpacing == 0
nLineSpacing := nFontSize * ( 1 + ( 47 / 300 ) )
ENDIF
DO WHILE .T. //Fitting text
HPDF_Page_SetFontAndSize( _HMG_SYSDATA[ 150 ][ 7 ], oFont, nFontSize )
HPDF_Page_SetTextLeading(_HMG_SYSDATA[ 150 ][ 7 ], nLineSpacing )
cTextIn := cText
cTextOut := ""
nLineCount := 0
DO WHILE !EMPTY(cTextIn)
nLineCount ++
nLineLen := HPDF_Page_MeasureText( _HMG_SYSDATA[ 150 ][ 7 ], cTextIn, nToxPos - nxPos, !lWrap, NIL )
IF nLineLen = 0
EXIT
ENDIF
cLine := Trim( hb_USubStr( cTextIn , 1 , nLineLen ) )
cTextIn := hb_USubStr( cTextIn, nLineLen + 1 )
cTextOut += cLine + IF( !EMPTY( cTextIn ) , hb_eol() , "")
ENDDO
IF !lFontSizeFit /* "Don't fit font" */ .OR. ;
( nLineCount * HPDF_Page_GetTextLeading(_HMG_SYSDATA[ 150 ][ 7 ]) ) <= nyPos - nToyPos /* "Fitted" */ .OR. ;
nFontSize <= 4 /* "Abandon further reducing the font" */
EXIT
ENDIF
nFontSize -= 0.01 //decrease font size
nLineSpacing := nFontSize * ( 1 + ( 47 / 300 ) ) //decrease line spacing
ENDDO
cText := cTextOut
HPDF_Page_BeginText( _HMG_SYSDATA[ 150 ][ 7 ] )
do case
case HMG_UPPER( cAlign ) == 'CENTER'
HPDF_Page_TextRect( _HMG_SYSDATA[ 150 ][ 7 ], nxPos, nyPos, nToxPos, nToyPos, cText, HPDF_TALIGN_CENTER, Nil )
case HMG_UPPER( cAlign ) == 'RIGHT'
HPDF_Page_TextRect( _HMG_SYSDATA[ 150 ][ 7 ], nxPos, nyPos, nToxPos, nToyPos, cText, HPDF_TALIGN_RIGHT, Nil )
case HMG_UPPER( cAlign ) == 'JUSTIFY'
HPDF_Page_TextRect( _HMG_SYSDATA[ 150 ][ 7 ], nxPos, nyPos, nToxPos, nToyPos, cText, HPDF_TALIGN_JUSTIFY, Nil )
otherwise
HPDF_Page_TextRect( _HMG_SYSDATA[ 150 ][ 7 ], nxPos, nyPos, nToxPos, nToyPos, cText, HPDF_TALIGN_LEFT, Nil )
endcase
HPDF_Page_EndText( _HMG_SYSDATA[ 150 ][ 7 ] )
//restore previous line spacing
HPDF_Page_SetTextLeading(_HMG_SYSDATA[ 150 ][ 7 ], nLineSpacingPrev )
If lDoLog // Pablo June, 2014
HTML_TABLE_LINE(nFHandle,{{StrZero(nRow,4,0)+" ("+StrZero(nyPos,7,2)+")","#FFFFFF",.t.},;
{StrZero(nCol,4,0)+" ("+StrZero(nxPos,7,2)+")","#FFFFFF",.t.},;
{StrZero(_HMG_SYSDATA[ 150 ][ 13 ],3,0),"#FFFFFF",.t.},;
{PadR(If(Empty(cTtfFnt),"<Default>",cTtfFnt),If(HMG_Len(cTtfFnt)>30,HMG_Len(cTtfFnt),30)),"#FFFFFF",.f.},;
{StrZero(nFontSize,2,0),"#FFFFFF",.t.},;
{If(lBold,"BOLD" ,Space(4))+If(lItalic," ITALIC",Space(7)),"#FFFFFF",.f.},;
{cFontName,If(If(Empty(cTtfFnt),HMG_Len(cFontName),HMG_Len(cTtfFnt))==HMG_Len(cFontName),"#FFFFFF","#FFFF66"),.f.}})
Endif
endif
Return Nil
*************** The end of this part of the code. *********************