You can create a Table of Contents (aka Outline) by saving the link to pages as they are added to the document.
The relevant function names are:
HPDF_CreateOutline
HPDF_Outline_SetOpened
HPDF_CreateOutline
HPDF_Page_CreateDestination
HPDF_Destination_SetFit
HPDF_Outline_SetDestination
Some declarations first:
static HPDF_Page current_page; // use this for annotating in a bookmark
static HPDF_Page PageIndex[MAX_PDF_PAGES]; // for annotation
static HPDF_Outline Outline[MAX_PDF_PAGES];
static char *BookmarkText[MAX_PDF_PAGES]/*[MAX_BOOKMARK_TEXT]*/;
static int current_pages_indexed = 0;
My function to create the running account of the book marks is:
void PDFAddPageToOutline(char *bookmarktext)
{
char *localtext = (char *) calloc(1,strlen(bookmarktext)+1);
if(current_pages_indexed < MAX_PDF_PAGES)
{
strcpy(localtext,bookmarktext);
BookmarkText[current_pages_indexed] = localtext;
PageIndex[current_pages_indexed] = current_page; // save the current page pointer
++current_pages_indexed;
}// bump the index
}
And the code just before I close and save the PDF file is:
if(current_pages_indexed)
{
root = HPDF_CreateOutline(pdf,NULL,"Pages Available",NULL);
HPDF_Outline_SetOpened(root,HPDF_TRUE);
for(i=0;i<current_pages_indexed;++i)
{
Outline[i] = HPDF_CreateOutline(pdf,root,BookmarkText[i],NULL);
free(BookmarkText[i]);
}
for(i=0;i<current_pages_indexed;++i)
{
dst=HPDF_Page_CreateDestination(PageIndex[i]);
// HPDF_Destination_SetXYZ(dst,0,HPDF_Page_GetHeight(PageIndex[i]),1);
HPDF_Destination_SetFit(dst); // Fit the bookmarked page in the space available
HPDF_Outline_SetDestination(Outline[i],dst);
}
}
This code requires a few more defines, etc. but should serve as a guide on how one can do this.