libhaur笔记1:Usage

这里记录了几个重要函数。

Usage

Initialize document object

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 document object attributes

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");

 

 

Create new page

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);

 

 

Save document to file or memory stream

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;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值