center a string in pdf file (hbwin and pdfcreator)

295 views
Skip to first unread message

jparada

unread,
Dec 25, 2010, 3:07:57 PM12/25/10
to Harbour Users
Hi,

I'm trying to center a string in pdf file, I'm trying to hbwin and
pdfcreator, and I do something like this:

oPrinter:SetColor( HB_WIN_RGB_GREEN )
oPrinter:TextOut( ""Hello, PDFCreator! This is Harbour", .t., .f.,
WIN_TA_CENTER ), or,
oPrinter:TextOut( ""Hello, PDFCreator! This is Harbour", , , ,
WIN_TA_CENTER ), or,
oPrinter:TextOut( ""Hello, PDFCreator! This is Harbour",
WIN_TA_CENTER )


But this does not work, it does not center the text string, and even
part of the text is not properly appreciated.

I appreciate any help.

Thank you very much

Best Regards,
Javier

Xavi

unread,
Dec 28, 2010, 3:20:51 PM12/28/10
to harbou...@googlegroups.com
Hi,

For center a string in pdf file page is need calculate the coordinates.
In the method TextOut( cString, lNewLine, lUpdatePosX, nAlign ) of WIN_PRN class, the nAlign parameter is the point inside text
rectangle of cString parameter to determine the print coordinates PosX, PosY.

Try this example, I hope that is clearer.-

#include "hbwin.ch"

Procedure Main()
Local nPosX, nPosY, nRow, nCol
Local cString, lNewLine, lUpdatePosX, nAlign

WITH OBJECT Win_Prn():New( "PDFCreator" )
if :Create() .and. :StartDoc()

nPosX := (:LeftMargin + :RightMargin) / 2 // X Page Center
nPosY := (:TopMargin + :BottomMargin) / 2 // Y Page Center
cString := "Te�st"
nAlign := hb_BitOr( WIN_TA_BASELINE, WIN_TA_CENTER ) // Y X Center
:SetPos( nPosX, nPosY ); :TextOut( cString, lNewLine, lUpdatePosX, nAlign )

nRow := nCol := 10
cString := ".Test"
nAlign := hb_BitOr( WIN_TA_BOTTOM, WIN_TA_LEFT ) // Default Normal
:SetPrc( nRow, nCol ); :TextOut( cString, lNewLine, lUpdatePosX, nAlign )

cString := " Te.st"
nAlign := hb_BitOr( WIN_TA_BOTTOM, WIN_TA_CENTER ) // Special
:SetPrc( ++nRow, nCol ); :TextOut( cString, lNewLine, lUpdatePosX, nAlign )

cString := ".Test" // Default
:SetPrc( ++nRow, nCol ); :TextOut( cString )
? "Ok"
else
? "Error"
endif
END
Wait
return

HTH,
Merry Christmas and a Happy New Year.
--
Xavi

El 25/12/2010 21:07, jparada escribi�:

Qatan

unread,
Dec 28, 2010, 3:27:36 PM12/28/10
to harbou...@googlegroups.com
Hello,

I am not sure but I am using LibHaru for PDF creation and after some
hardwork I now belive that "maybe" it is a better option but I can't really
say that because that may have advantages using your way with pdfcreator.
Please give a look at the nice example you find under
\contrib\hbhpdf\tests
Just an idea...

Qatan


Hi,

#include "hbwin.ch"

--
You received this message because you are subscribed to the Google
Groups "Harbour Users" group.
Unsubscribe: harbour-user...@googlegroups.com
Web: http://groups.google.com/group/harbour-users

jparada

unread,
Dec 28, 2010, 4:19:36 PM12/28/10
to Harbour Users
Hi,

Thanks for answering

@Xavi

Thanks for the example, please let me try.

@Qatan

My first attempt was with libharu, but honestly speaking, I am
overwhelmed by so much code, I am confused looking for a simple
solution at all that code (center a text), I do not need much, so far,
only the text center on the page and some text right aligned if you
have an easy and simple example, that you can show, to take a look, I
appreciate it.

Merry Christmas and Happy New Year to all.

Best Regards,
Javier
>           cString := "Te�st"
> El 25/12/2010 21:07, jparada escribi�:
> > Hi,
>
> > I'm trying to center a string in pdf file, I'm trying to hbwin and
> > pdfcreator, and I do something like this:
>
> > oPrinter:SetColor( HB_WIN_RGB_GREEN )
> > oPrinter:TextOut( ""Hello, PDFCreator! This is Harbour", .t., .f.,
> > WIN_TA_CENTER ), or,
> > oPrinter:TextOut( ""Hello, PDFCreator! This is Harbour", , , ,
> > WIN_TA_CENTER ), or,
> > oPrinter:TextOut( ""Hello, PDFCreator! This is Harbour",
> > WIN_TA_CENTER )
>
> > But this does not work, it does not center the text string, and even
> > part of the text is not properly appreciated.
>
> > I appreciate any help.
>
> > Thank you very much
>
> > Best Regards,
> > Javier
>
> --
> You received this message because you are subscribed to the Google
> Groups "Harbour Users" group.
> Unsubscribe: harbour-user...@googlegroups.com
> Web:http://groups.google.com/group/harbour-users- Ocultar texto de la cita -
>
> - Mostrar texto de la cita -

Qatan

unread,
Dec 28, 2010, 7:03:08 PM12/28/10
to harbou...@googlegroups.com
Hello Javier,

I understand you.
I have an example of a form that I had to develop and here I will show
you a simple example with some functions created by me to help the task and
reduce some code.
Compile it with hbmk2 example -lhbhpdf -llibhpdf -lpng -w0
It will create a file named example.pdf with some boxes and text (some
centered).
I hope it will help you.

Qatan


[code]

/*
example.prg
*/

#include 'harupdf.ch'

procedure main()

local pdf := HPDF_New()

HPDF_SetCompressionMode( pdf, HPDF_COMP_ALL )

HPDF_Page_SetSize( pdf, 4, 1 ) // A4 Portrait 595x841

HPDF_SetPageMode( pdf, HPDF_PAGE_MODE_USE_OUTLINE )

page := HPDF_AddPage( pdf )

HPDF_Page_SetLineWidth( page, 0.5 )

line( page, 817, 20, 555 )
line( page, 797, 20, 465 )
line( page, 767, 20, 555 )

line( page, 762, 20, 555, .T. )

line( page, 755, 20, 555 )
line( page, 720, 351, 224 )
line( page, 700, 351, 224 )
line( page, 665, 20, 555 )
line( page, 645, 20, 555 )
line( page, 625, 20, 555 )

colu( page, 20, 625, 130 )
colu( page, 260, 665, 90 )
colu( page, 205, 625, 20 )
colu( page, 351, 645, 110 )
colu( page, 390, 625, 20 )
colu( page, 575, 625, 130 )

colu( page, 20, 767, 50 )
colu( page, 198, 767, 30 )
colu( page, 485, 767, 50 )
colu( page, 575, 767, 50 )

HPDF_Page_Rectangle( page, 329, 710, 10, 10 )

HPDF_Page_Stroke( page )

def_font1 := HPDF_GetFont( pdf, "Times-Roman", "WinAnsiEncoding" )
def_font2 := HPDF_GetFont( pdf, "Times-Bold", "WinAnsiEncoding" )

HPDF_Page_BeginText( page )

// 595x841

HPDF_Page_SetFontAndSize( page, def_font2, 10 )
texto( page, 485, 803, '-*-O-*-', 575 ) // centralizing text

HPDF_Page_SetFontAndSize( page, def_font1, 6 )
texto( page, 20, 791, 'TEST TEST' )
texto( page, 198, 791, 'NNNN NNNNNN NNNNNNNNNNN NNNNNN NNN' )
texto( page, 20, 749, 'XHXHXHXHXH XHXHXHXHXHX HXHHXHXHXHXH XH XH' )
texto( page, 351, 714, 'JFJFDJFDJFDJFJFJ FJ FJ FJ FJFJFJFJFJFJFJ FJ F' )

HPDF_Page_SetFontAndSize( page, def_font2, 12 )
texto( page, 260, 745, 'CENTER', 351 )
HPDF_Page_SetFontAndSize( page, def_font1, 8 )
texto( page, 260, 735, 'Center here too ', 351 )
texto( page, 260, 727, 'here a bit more or center', 351 )
texto( page, 270, 714, '1 - Option' )
texto( page, 270, 706, '2 - Other' )

HPDF_Page_SetFontAndSize( page, def_font1, 7 )
texto( page, 351, 689, 'Some centered text to text our little Harbour
program', 575 )
texto( page, 351, 680, 'note that you can create simple functions', 575 )
texto( page, 351, 671, 'to help you do that', 575 )

HPDF_Page_SetFontAndSize( page, def_font1, 6 )
texto( page, 20, 659, 'NKFDKDFKFDK FDKLFDKFDKLDF' )
texto( page, 351, 659, 'XNXNXNX NXNXNXNXNXNXNXNNX NX NXNXNXNX' )

texto( page, 20, 639, 'IUIIUIUIUIUI U IUIUIUIUIUIUIU IUIIU' )
texto( page, 205, 639, 'DKDKDKDKD KDK DK DKDKDKDKDKDKDK D KDK' )
texto( page, 390, 639, 'XXXXXXXXXXXXXXXX' )

HPDF_Page_EndText( page )

HPDF_SaveToFile( pdf, 'example.pdf' )

HPDF_Free( pdf )

return NIL

////////////////////////////////////////////////////////////////////////////
static function line( page, line, ini, tam, picote )

picote := if(picote = NIL, .F., picote)

if picote
HPDF_Page_SetDash(page, {3}, 1, 1)
endif
HPDF_Page_MoveTo( page, ini, line )
HPDF_Page_LineTo( page, ini+tam, line )
HPDF_Page_Stroke(page)
if picote
HPDF_Page_SetDash(page, NIL, 0, 0)
endif

return NIL

////////////////////////////////////////////////////////////////////////////
static function colu( page, col, ini, tam, picote )

picote := if(picote = NIL, .F., picote)

if picote
HPDF_Page_SetDash(page, {3}, 1, 1)
endif
HPDF_Page_MoveTo( page, col, ini )
HPDF_Page_LineTo( page, col, ini+tam )
HPDF_Page_Stroke(page)
if picote
HPDF_Page_SetDash(page, NIL, 0, 0)
endif

return NIL

////////////////////////////////////////////////////////////////////////////
static function texto( page, col, lin, texto, fim, direita )

local tw


direita := if( direita = NIL, .F., direita )

if fim = NIL
col += 2
elseif ! direita
tw := HPDF_Page_TextWidth( page, texto )
col += ( ( fim - col ) / 2 ) - ( tw / 2 )
endif
if direita
tw := HPDF_Page_TextWidth( page, texto )
col -= tw + 4
endif
HPDF_Page_TextOut( page, col, lin, texto )

return NIL

[/code]


Hi,

Thanks for answering

@Xavi

@Qatan

Best Regards,
Javier

> cString := "Te�st"

> El 25/12/2010 21:07, jparada escribi�:

example.pdf
example.prg

jparada

unread,
Dec 29, 2010, 9:38:06 AM12/29/10
to Harbour Users
Hi Qatan

Thanks a Lot for the simple example, please let me try it, and see if
I can adapt it for my basic needs, compared with the use of pdfreator.

Thank you very much, I appreciate your help.

Best Regards,
Javier
> ///////////////////////////////////////////////////////////////////////////­/
> static function line( page, line, ini, tam, picote )
>
>    picote := if(picote = NIL, .F., picote)
>
>    if picote
>       HPDF_Page_SetDash(page, {3}, 1, 1)
>    endif
>    HPDF_Page_MoveTo( page, ini, line )
>    HPDF_Page_LineTo( page, ini+tam, line )
>    HPDF_Page_Stroke(page)
>    if picote
>       HPDF_Page_SetDash(page, NIL, 0, 0)
>    endif
>
> return NIL
>
> ///////////////////////////////////////////////////////////////////////////­/
> static function colu( page, col, ini, tam, picote )
>
>    picote := if(picote = NIL, .F., picote)
>
>    if picote
>       HPDF_Page_SetDash(page, {3}, 1, 1)
>    endif
>    HPDF_Page_MoveTo( page, col, ini )
>    HPDF_Page_LineTo( page, col, ini+tam )
>    HPDF_Page_Stroke(page)
>    if picote
>       HPDF_Page_SetDash(page, NIL, 0, 0)
>    endif
>
> return NIL
>
> ///////////////////////////////////////////////////////////////////////////­/
> > cString := "Te�st"
> > El 25/12/2010 21:07, jparada escribi�:
> > > Hi,
>
> > > I'm trying to center a string in pdf file, I'm trying to hbwin and
> > > pdfcreator, and I do something like this:
>
> > > oPrinter:SetColor( HB_WIN_RGB_GREEN )
> > > oPrinter:TextOut( ""Hello, PDFCreator! This is Harbour", .t., .f.,
> > > WIN_TA_CENTER ), or,
> > > oPrinter:TextOut( ""Hello, PDFCreator! This is Harbour", , , ,
> > > WIN_TA_CENTER ), or,
> > > oPrinter:TextOut( ""Hello, PDFCreator! This is Harbour",
> > > WIN_TA_CENTER )
>
> > > But this does not work, it does not center the text string, and even
> > > part of the text is not properly appreciated.
>
> > > I appreciate any help.
>
> > > Thank you very much
>
> > > Best Regards,
> > > Javier
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Harbour Users" group.
> > Unsubscribe: harbour-user...@googlegroups.com
> > Web:http://groups.google.com/group/harbour-users-Ocultar texto de la
> > cita -
>
> > - Mostrar texto de la cita -
>
> --
> You received this message because you are subscribed to the Google
> Groups "Harbour Users" group.
> Unsubscribe: harbour-user...@googlegroups.com
> Web:http://groups.google.com/group/harbour-users
>
>
>
>  example.pdf
> 1 KVerDescargar
>
>  example.prg
> 3 KVerDescargar- Ocultar texto de la cita -
Reply all
Reply to author
Forward
0 new messages