在libharu C中动态添加页面

时间:2014-08-28 22:17:23

标签: c pdf-generation libharu

我正在制作一份pdf报告,问题是我不知道报告的页数会有多大差异。我需要根据需要动态创建页面,我试试这个,但它不起作用。

//page counter
int page_num=1;

HPDF_Doc  pdf;
HPDF_Page *page;
char fname[256];
HPDF_Destination dst;
//This works for the first page 
page = malloc(sizeof(*page));
if (page==NULL) {
    printf("Error allocating memory!\n"); //print an error message
} else {
    printf("allocation successful\n");
}

并且我能够正常写入pdf文件,但是当我尝试创建另一个页面时 我得到了分段错误。我试试这个。

page_num ++;
HPDF_Page *temp;
*temp = realloc(page, (page_num+2) * sizeof(page));
if ( temp != NULL ) //realloc was successful
{
page = temp;
page[page_num] = HPDF_AddPage (pdf); //** This line makes it crash **//

printf("Success Reallocating into page\n");
}
else //there was an error
{  printf("Error reallocating into page\n"); }

我试图这样做,因为我需要回到一个特定的页面并插入一个带有页码示例的页脚"第1页,共50页"如果有不同的方法来实现这一点,请告诉我。另外在他们的网站上有一个包含多个页面的例子,但是在开头设置了一定数量的页面并不适用于我。

2 个答案:

答案 0 :(得分:0)

* temp = realloc(page,(page_num + 2)* sizeof(page))此语句出错。页面是地址点,sizeof(页面)是8

答案 1 :(得分:0)

请看type definition HPDF_Page HPDF_Page p;
p是指针

HPDF_Page *p;
* p现在是双指针

Malloc内存分配:

HPDF_Page *page; //this is double pointer!!!!

page = malloc(number_of_pages * sizeof(HPDF_Page*)); // memory allocation for array of pointers
if(page) 
{
    for(i = 0; i < number_of_pages; i++)
    {
        page[i] = malloc(sizeof(HPDF_Page)); // allocation for each page pointer
        page[i] = HPDF_AddPage (pdf);
    }
}

Realloc内存分配:

number_of_pages++;
page = realloc(page, number_of_pages * sizeof(HPDF_Page*)); 

if(page) 
{
        page[number_of_pages-1] = malloc(sizeof(HPDF_Page));
        page[number_of_pages-1] = HPDF_AddPage (pdf);
}


Malloc会奏效。但是realloc将不起作用,程序将失败!我不知道为什么。工作方法是使用链表:

struct my_ch
{
    struct my_ch *n;
    struct my_ch *p;
    HPDF_Page page;
    int page_number;
}*f_item = NULL, *l_item = NULL;

void add_item()
{
    struct my_ch *pom;
    pom = f_item;

    if(pom)
    {
        struct my_ch *prev;

        while(pom->n)
        {
             pom = pom->n;
        }

        prev = pom;
        pom->n = malloc(sizeof(struct my_ch));
        pom = pom->n;
        pom->p = prev;
        pom->n = NULL;
        pom->page = malloc(sizeof(HPDF_Page));
        pom->page_number = pom->prev->page_number + 1;
    }
    else
    {
        pom = malloc(sizeof(struct my_ch));
        pom->p = NULL;
        pom->n = NULL;
        pom->page = malloc(sizeof(HPDF_Page));
        pom->page_number = 1;
    }

    l_item = pom;
}

void add_my_new_page()
{
    add_item();
    l_item->page = HPDF_AddPage(pdf);
    //paste some nice graphic, text...
}

bool modify_page(int number_of_page)
{
    struct my_ch *p;
    p = f_item;

    while(p)
    {
        if(p->page_number == number_of_page)
        {
            // let`s do some modifycation on p->page
            HPDF_Page_Rectangle( p->page, ..., ...);
            HPDF_Page_Stroke(p->page);

            return true;
        }

        p = p->n;
    }

    return false;
}


我在我的程序中使用此代码并且它运行良好。