当前位置:文档管理控件网 > 技术中心 >正文

pdflib输出pdf中文demo代码



/* 


 * PDFlib client: hello example in C

PDFlib-Lite-7.0.5p3

 */

#include <stdio.h>
#include <stdlib.h>

#include "pdflib.h"

int main(void)
{
    PDF *p;
    int font;
int font_ch;

    /* create a new PDFlib object */
    if ((p = PDF_new()) == (PDF *) 0)
    {
        printf("Couldn't create PDFlib object (out of memory)!n");
        return(2);
    }

    PDF_TRY(p) {
/* This means we must check return values of load_font() etc. */
PDF_set_parameter(p, "errorpolicy", "return");
PDF_set_parameter(p, "hypertextencoding", "host"); //--(增加位置)

if (PDF_begin_document(p, "hello.pdf", 0, "") == -1) {
printf("Error: %sn", PDF_get_errmsg(p));
return(2);
}

/* This line is required to avoid problems on Japanese systems */
//PDF_set_parameter(p, "hypertextencoding", "host"); //--(注释位置)

PDF_set_info(p, "Creator", "hello.c");
PDF_set_info(p, "Author", "Thomas Merz");
PDF_set_info(p, "Title", "Hello, world (C)!");

PDF_begin_page_ext(p, a4_width, a4_height, "");

/* Change "host" encoding to "winansi" or whatever you need! */
font = PDF_load_font(p, "Helvetica-Bold", 0, "host", "");
if (font == -1) {
printf("Error: %sn", PDF_get_errmsg(p));
PDF_delete(p);
return(2);
}

font_ch = PDF_load_font(p, "STSong-Light", 0, "GB-EUC-H", "");
if (font == -1) {
printf("Error: %sn", PDF_get_errmsg(p));
PDF_delete(p);
return 0;
}

//=====================================
PDF_save(p);
PDF_setfont(p, font, 24);
PDF_set_text_pos(p, 50, 700);
PDF_show(p, "Hello, world!");

PDF_setfont(p, font_ch, 24);
PDF_continue_text(p, "您好,世界!");
PDF_restore(p);
//====================================

PDF_end_page_ext(p, "");

PDF_end_document(p, "");
    }

    PDF_CATCH(p) {
        printf("PDFlib exception occurred in hello sample:n");
        printf("[%d] %s: %sn",
   PDF_get_errnum(p), PDF_get_apiname(p), PDF_get_errmsg(p));
        PDF_delete(p);
        return(2);
    }

    PDF_delete(p);

    return 0;
}

pdflib

上一篇: 用 PHP 编写 PDF 文档生成器教程

下一篇: C#使用PDFLib生成PDF文档



单击获得帮助