2.1 官方范例学习《hello》

来源:互联网 发布:润知文化传播有限公司 编辑:程序博客网 时间:2024/05/22 10:28

一 本例研究的内容:

  • [1]创建/保存PDF
  • [2]创建页面/简单设置
  • [3]设置文档属性
  • [4]简单那输出文本,换行

二 本例缺陷:

  • [1]文本输出中文乱码
  • [2]文档属性中文乱码

    缺陷将在下篇研究,尝试解决

三 示例

**环境配置参见此系列第一篇****讲解参见注释**
#include "stdafx.h"#include "pdflib.h"#include <afxwin.h>int main(){    PDF *p;    int font;    /* 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. */        /* errorpolicy:(关键字)控制出现错误时各种函数的行为。            return 如果发生错误,函数将返回。        */        PDF_set_option(p, "errorpolicy=return");        /*            1.PDF_begin_document() 与 PDF_end_document()成堆出现            简单形式PDF_begin_document(p,"创建的PDF文件名,不能为中文(目前),0,""),后面两个参数暂时用不到,先如此写。            2.PDF_get_errmsg(p) 获取上一个出错的原因,配套的还有PDF_get_errnum(p)获取出错代码,然而知道代码有什么卵用,还不如msg        */        if (PDF_begin_document(p, "hello.pdf", 0, "") == -1) {            printf("Error: %s\n", PDF_get_errmsg(p));            return(2);        }        /* This line is required to avoid problems on Japanese systems */        /*hypertextencoding 超文本字符串编码,默认值是auto,至于为什么是host?在文档中说的我没有找到,暂且认为是“避免在日语系统上出错”吧”*/        PDF_set_option(p, "hypertextencoding=host");        /*设置文档信息:右键->文档属性 查看            Creator->应用程序:            Author->作者:            Title->标题:            Subject:主题            Keywords:关键字        */        PDF_set_info(p, "Creator", "hello.cc");        PDF_set_info(p, "Author", "skysun");        PDF_set_info(p, "Title", "Hello, world (C)!");        /*设置页面属性,宽高:a4大小,宏定义了a0~a6,b2~b5的大小,optlist暂时为空*/        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: %s\n", PDF_get_errmsg(p));            PDF_delete(p);            return(2);        }        /*设置字体与字号*/        PDF_setfont(p, font, 24);        /*设置文字出现在文档中的位置 默认左下角为坐标原点         若想更改为从左上角为坐标原点,则应在PDF_begin_page_ext()函数前设置            PDF_set_option(p, "topdown=true");        */        PDF_set_text_pos(p, 50, 700);        /*            PDF_show() 在页面上输出内容,必须先设置好PDF_setfont()        */        PDF_show(p, "Hello, world!");        /*下一行打印*/        PDF_continue_text(p, "(says C)");        /*            和begin函数要配对嘛,这年头函数都出来虐狗        */        PDF_end_page_ext(p, "");        /*            那么如何开启下一页呢? 很简单,再来一个            PDF_begin_page_ext();            PDF_end_page_ext();        */        PDF_end_document(p, "");    }    PDF_CATCH(p) {        /*            PDF_get_apiname(p)获取出错的api名字        */        printf("PDFlib exception occurred in hello sample:\n");        printf("[%d] %s: %s\n",            PDF_get_errnum(p), PDF_get_apiname(p), PDF_get_errmsg(p));        PDF_delete(p);        return(2);    }    /*        new完就删,真tm爽    */    PDF_delete(p);    /*        打开生成的pdf,方便调试    */    system("hello.pdf");    return 0;}
0 0
原创粉丝点击