Haru contrib improvement

248 views
Skip to first unread message

Luigi Ferraris

unread,
Jul 16, 2025, 6:28:28 AMJul 16
to Harbour Developers
Hi friends and Pritpal

I thought it might be useful to add these functions to the source harbour/contrib/hbhpdf/core.c

I'm not a C programmer, so I do copy&paste code but I don't know if they are right!

Basically, Haru stores the created pages in an internal vector.

Using the function:

/* HPDF_GetPageByIndex( hDoc, index ) --> hPage */
HB_FUNC( HPDF_GETPAGEBYINDEX )
{
   hb_retptr( ( void * ) HPDF_GetPageByIndex( hb_HPDF_Doc_par( 1 ), ( HPDF_UINT ) hb_parni( 2 ) ) );
}


it is possible to retrieve a page using an index (IOW page number) from Haru vector "pdf->page_list"
I think index follow C style IOW from 0 to pdf->cur_page_num - 1

Next function is used to set current page to operate on. See previous function but not only.

/* HPDF_Doc_SetCurrentPage( hDoc, page )  --> nStatus */
HB_FUNC( HPDF_SETCURRENTPAGE )
{
   hb_retnl( ( long ) HPDF_Doc_SetCurrentPage( hb_HPDF_Doc_par( 1 ), ( HPDF_Page ) hb_parptr( 2 ) ) );
}


Another function that might be useful, is to return the number of pages (see pdf->cur_page_num), but I didn't find this function in the HARU library.
So I can only show a brief template
HB_FUNC( HPDF_CURRPAGE )
{
   hb_retni( ???????? );
}

Best regards
Luigi

Aleksander Czajczynski

unread,
Jul 17, 2025, 10:32:49 AMJul 17
to harbou...@googlegroups.com
Hello!

Page counter is not exported in Haru, maybe the reason is: you can count the pages while creating in your code. Below I've included something that "abuses"
HPDF_GetPageByIndex(), but i'm not sure if we should add it.

Best regards, Aleksander

diff --git a/contrib/hbhpdf/core.c b/contrib/hbhpdf/core.c
index c9cb14be37..30228fb338 100644
--- a/contrib/hbhpdf/core.c
+++ b/contrib/hbhpdf/core.c
@@ -248,12 +248,41 @@ HB_FUNC( HPDF_SETOPENACTION )
    hb_retnl( ( long ) HPDF_SetOpenAction( hb_HPDF_Doc_par( 1 ), ( HPDF_Destination ) hb_parptr( 2 ) ) );
 }
 
+/* HPDF_Doc_SetCurrentPage( hDoc, page )  --> nStatus */
+HB_FUNC( HPDF_SETCURRENTPAGE )
+{
+   hb_retnl( ( long ) HPDF_Doc_SetCurrentPage( hb_HPDF_Doc_par( 1 ), ( HPDF_Page ) hb_parptr( 2 ) ) );
+}
+
 /* HPDF_GetCurrentPage( hDoc ) --> hPage */
 HB_FUNC( HPDF_GETCURRENTPAGE )
 {
    hb_retptr( ( void * ) HPDF_GetCurrentPage( hb_HPDF_Doc_par( 1 ) ) );
 }
 
+/* HPDF_GetPageByIndex( hDoc, index ) --> hPage */
+HB_FUNC( HPDF_GETPAGEBYINDEX )
+{
+   hb_retptr( ( void * ) HPDF_GetPageByIndex( hb_HPDF_Doc_par( 1 ), ( HPDF_UINT ) hb_parni( 2 ) ) );
+}
+
+/* Harbour extension
+   HB_HPDF_GetPageCount( hDoc ) --> nCount
+*/
+HB_FUNC( HB_HPDF_GETPAGECOUNT )
+{
+   HPDF_UINT page_count = 0;
+
+   /* TOFIX: if HPDF_SetErrorHandler() is used, it should be disabled here */
+
+   while( HPDF_GetPageByIndex( hb_HPDF_Doc_par( 1 ), page_count ) )
+      page_count++;
+
+   HPDF_ResetError( hb_HPDF_Doc_par( 1 ) );
+
+   hb_retnl( ( long ) page_count );
+}
+
 /* HPDF_AddPage( hDoc ) --> hPage */
 HB_FUNC( HPDF_ADDPAGE )
 {

Luigi Ferraris

unread,
Jul 18, 2025, 12:08:57 PMJul 18
to Harbour Developers
Hi Aleksander
many thanks for your job.

>Page counter is not exported in Haru, maybe the reason is: you can count the pages while creating in your code. Below I've included something that "abuses"
>HPDF_GetPageByIndex(), but i'm not sure if we should add it.


About HPDF_GetPageByIndex() this is my little opinion:
I think everyone need to retrieve a page, as example to place page number.Little example as CLipper
   FOR iK := 0 TO pageCount-1
      pPage := getPageByIndex( iK )
      setCurrentPage( pPage )
      ? "Pag. " + hb_NtoS(iK) + "/" + hb_NtoS(pageCount)
   NEXT

Without any additional lib (in my lib I'm using a hbArray for this scope, but not only) AFAIK there is no another way to move up and down or localize a page.
So, for this reason I thought "why not use internal Haru?".

About HB_HPDF_GETPAGECOUNT as a newbe (so, I'm sorry) I thought "wow it's a complicated code,  I'm surprised there isn't an easy way to get such useful information".

anyway, thank you very much.
Luigi

Aleksander Czajczynski

unread,
Jul 18, 2025, 1:22:31 PMJul 18
to harbou...@googlegroups.com
Luigi Ferraris wrote:
many thanks for your job.

>Page counter is not exported in Haru, maybe the reason is: you can count the pages while creating in your code. Below I've included something that "abuses"
>HPDF_GetPageByIndex(), but i'm not sure if we should add it.


About HPDF_GetPageByIndex() this is my little opinion:
I think everyone need to retrieve a page, as example to place page number.Little example as CLipper
   FOR iK := 0 TO pageCount-1
      pPage := getPageByIndex( iK )
      setCurrentPage( pPage )
      ? "Pag. " + hb_NtoS(iK) + "/" + hb_NtoS(pageCount)
   NEXT

Without any additional lib (in my lib I'm using a hbArray for this scope, but not only) AFAIK there is no another way to move up and down or localize a page.
So, for this reason I thought "why not use internal Haru?".
You could make an array yourself, while creating pages.
AAdd( aPages, page := HPDF_AddPage( pdf ) )
This way you keep references for adding "n/totalpages" at the end of the operation. This is what Haru creators assume.


About HB_HPDF_GETPAGECOUNT as a newbe (so, I'm sorry) I thought "wow it's a complicated code,  I'm surprised there isn't an easy way to get such useful information".
Can make it simpler by extending Haru lib, but the reason, why it's not there, stays.


+/* HPDF_Doc_SetCurrentPage( hDoc, page )  --> nStatus */
+HB_FUNC( HPDF_SETCURRENTPAGE )
+{
+   hb_retnl( ( long ) HPDF_Doc_SetCurrentPage( hb_HPDF_Doc_par( 1 ), ( HPDF_Page ) hb_parptr( 2 ) ) );
+}
+

This really is in another namespace, should be HPDF_DOC_SETCURRENTPAGE. Another API group with some extended purpose(?) Can you draw on multiple pages at once?

I think I will only add HPDF_GetPageByIndex() for now.

Best regards, Aleksander

anyway, thank you very much.
Luigi

Il giorno giovedì 17 luglio 2025 alle 16:32:49 UTC+2 Aleksander Czajczynski ha scritto:
Hello!

Page counter is not exported in Haru, maybe the reason is: you can count the pages while creating in your code. Below I've included something that "abuses"
HPDF_GetPageByIndex(), but i'm not sure if we should add it.

Best regards, Aleksander

Luigi Ferraris wrote:
Hi friends and Pritpal

I thought it might be useful to add these functions to the source harbour/contrib/hbhpdf/core.c

I'm not a C programmer, so I do copy&paste code but I don't know if they are right!

Basically, Haru stores the created pages in an internal vector.

Using the function:

/* HPDF_GetPageByIndex( hDoc, index ) --> hPage */
HB_FUNC( HPDF_GETPAGEBYINDEX )
{
   hb_retptr( ( void * ) HPDF_GetPageByIndex( hb_HPDF_Doc_par( 1 ), ( HPDF_UINT ) hb_parni( 2 ) ) );
}


it is possible to retrieve a page using an index (IOW page number) from Haru vector "pdf->page_list"
I think index follow C style IOW from 0 to pdf->cur_page_num - 1

Next function is used to set current page to operate on. See previous function but not only.

/* HPDF_Doc_SetCurrentPage( hDoc, page )  --> nStatus */
HB_FUNC( HPDF_SETCURRENTPAGE )
{
   hb_retnl( ( long ) HPDF_Doc_SetCurrentPage( hb_HPDF_Doc_par( 1 ), ( HPDF_Page ) hb_parptr( 2 ) ) );
}


Another function that might be useful, is to return the number of pages (see pdf->cur_page_num), but I didn't find this function in the HARU library.
So I can only show a brief template
HB_FUNC( HPDF_CURRPAGE )
{
   hb_retni( ???????? );
}


--
You received this message because you are subscribed to the Google Groups "Harbour Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to harbour-deve...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/harbour-devel/5e2b6aac-9c5e-40fd-bab7-4ce5441f4e0an%40googlegroups.com.

Luigi Ferraris

unread,
Jul 18, 2025, 1:29:52 PMJul 18
to harbou...@googlegroups.com
Hi Alex,
Wow, I think you are right. It's better.
Many thanks.
Best regards

Luigi

You received this message because you are subscribed to a topic in the Google Groups "Harbour Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/harbour-devel/T-HT86RpSuY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to harbour-deve...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/harbour-devel/687A82CE.2080608%40fki.pl.

Luigi Ferraris

unread,
Sep 3, 2025, 6:00:33 AM (3 days ago) Sep 3
to Harbour Developers
Hi Alex,
I'm using your new patch and for now works fine.
Many thanks.
Luigi
Reply all
Reply to author
Forward
0 new messages