Usage examples - libharu/libharu GitHub Wiki
First, invoke HPDF_New() or HPDF_NewEx() to create an HPDF_Doc "document object". If user-defined memory management is needed, use HPDF_NewEx(). The document object handle which is returned from HPDF_New() or HPDF_NewEx() is used in the following steps.
You can specify a user-defined error function ("error_handler" here) in this step. After HPDF_New() succeeds, you can call setjmp() for exception handling. (See the chapter of "Error Handling")
#include "hpdf.h"
HPDF_Doc pdf;
pdf = HPDF_New (error_handler, NULL);
if (!pdf) {
printf ("ERROR: cannot create pdf object.\n");
return 1;
}
if (setjmp(env)) {
HPDF_Free (pdf);
return 1;
}
Set compression, encryption, page mode, and password if necessary.
/* set compression mode */
HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL);
/* set page mode to use outlines. */
HPDF_SetPageMode (pdf, HPDF_PAGE_MODE_USE_OUTLINE);
/* set password */
HPDF_SetPassword (pdf, "owner", "user");
Call HPDF_AddPage() to add a page to the document. The page handle returned is used in later operations on the page.
HPDF_Page page_1;
page_1 = HPDF_AddPage (pdf);
To insert a new page before an existing page, call HPDF_InsertPage(). For example, to insert "page_0" before "page_1":
HPDF_Page page_0;
page_0 = HPDF_InsertPage (pdf, page_1);
Set attributes of the page object if necessary.
HPDF_Page_SetSize (page_1, HPDF_PAGE_SIZE_B5, HPDF_PAGE_LANDSCAPE);
Perform drawing operations. Place text on the page. For details, please refer to "Graphics" chapter.
HPDF_SaveToFile() saves a document to a file.
HPDF_SaveToFile (pdf, "test.pdf");
HPDF_SaveToStream() saves a document to a temporary stream. An application can get the data saved by HPDF_SaveToStream() by invoking HPDF_ReadFromStream(). The following code shows how to output the document to stdout.
/* save the document to a stream */
HPDF_SaveToStream (pdf);
fprintf (stderr, "the size of data is %d\n", HPDF_GetStreamSize(pdf));
HPDF_ResetStream (pdf); /* rewind the stream. */
/* get the data from the stream and output it to stdout. */
for (;;) {
HPDF_BYTE buf[4096];
HPDF_UINT32 siz = 4096;
HPDF_STATUS ret = HPDF_ReadFromStream (pdf, buf, &siz);
if (siz == 0)
break;
if (fwrite (buf, siz, 1, stdout) != 1) {
fprintf (stderr, "cannot write to stdout", siz, wsiz);
break;
}
}
If you want to create another document, call HPDF_NewDoc(). HPDF_NewDoc() creates a new document after revoking the current document.
HPDF_NewDoc (pdf);
After all processes are finished, call HPDF_Free() to free all resources which belong to the document object.
HPDF_Free (pdf);