Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1218128
  • 博文数量: 950
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 13070
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-04 09:23
文章分类

全部博文(950)

文章存档

2011年(1)

2008年(949)

我的朋友

分类: C/C++

2008-08-04 09:35:31

下载本文示例代码
下载源代码

除了PDFlib自带字体外,用户还可以使用安装在系统上的字体及其他用户字体。

PDFlib称安装在Windows和Mac操作系统中的(存在于或被拷入相应系统字体目录的)TrueType, OpenType 和PostScript字体为宿主字体(Host Font)。PDFlib可直接引用字体名进行调用,但必须与文件名完全相同(严格区分大小写)。例如,调用安装在Windows系统中的字体:C:\WINDOWS\Fonts\SimHei.ttf

 int Font_CS = 0;

Font_CS = PDF_load_font(p, "SimHei", 0, "unicode", "");

需要注意的是,字体名可能与字体文件名不同,甚至相同的字体在不同语言的操作系统下字体名称会有所不同。在 Windows 环境下查看字体名,可双击该字体文件,窗口打开后的第一行字除结尾的 TrueType, OpenType 外为字体名。例如,调用安装在 Windows 系统中的字体: C:\WINDOWS\Fonts\SimHei.ttf ,双击该文件后,窗口的第一行为“黑体 TrueType” 。则该文件的字体名为“黑体”。在 PDFlib 中若要调用多字节的文件名,须以 BOM UTF8 的形式。 “黑体”的 BOM UTF8 的形式为“ \xEF\xBB\xBF\xE9\xBB\x91\xE4\xBD\x93 ”。
因此对于中文黑体, 在中文WINDOWS下,则我们使用
PDF_load_font(p, "\xEF\xBB\xBF\xE9\xBB\x91\xE4\xBD\x93", 0, "unicode", "");
在英文WINDOWS下则应使用
PDF_load_font(p, "SimHei", 0, "unicode", "");
(小技巧: 我们可以使用Windows2000/XP自带的notepad获得UTF8编码,具体方法举例:在notepad中输入"黑体"并保存, 保存时在编码下拉框中选择UTF-8, 然后用UltraEdit,WinHex,VC等可以进行二进制编辑的工具打开该文件即可取得带BOM的UTF8字符串)

除安装在Windows系统中的字体之外,PDFlib还可以调用其他用户字体。但在调用之时,需要给出路径名。如我想用C:\Program Files\Adobe\Acrobat 7.0\Resource\CIDFont\AdobeSongStd-Light.otf 这个字体:
Font_CS= PDF_load_font(p,

"C:\\Program Files\\Adobe\\Acrobat 7.0\\Resource\\CIDFont\\ AdobeSongStd-Light",

0, "unicode", "");
但这里有个例外,那就是.ttc(TrueType Collection)字体。.ttc是集合字体文件,每个文件中含有多种字体。所以用户不能用文件名调用字体,而是要用真正的字体名。比方说,我们知道C:\WINDOWS\Fonts\MSGOTHIC.TTC 包含三种字体依次名为MS Gothic,MS PGothic,和MS UI Gothic。我们可以用以它们相应的字体名调用:
 int Font_E = 0;

Font_E= PDF_load_font(p, "MS Gothic", 0, "winansi", ""); /* Use MS Gothic */

PDF_setfont(p, Font_E, 20);

PDF_show_xy(p, "MS Gothic font:" , 50,  800);

Font_E= PDF_load_font(p, "MS PGothic", 0, "winansi", ""); /* Use MS PGothic */

……

Font_E= PDF_load_font(p, "MS UI Gothic", 0, "winansi", ""); /* Use MS UI Gothic */

可是我们经常并不清楚.ttc里包含哪些字体。在这种情况PDFlib提供了另一种调用方式—索引(Index)。用此方式,首先须给字体文件名一个别名,然后在别名后加冒号再加数字(0表示文件中第一种字体,1 表示第二种,依次类推。)
int Font_E = 0;

/*  Give “C:\WINDOWS\Fonts\MSGOTHIC.TTC an alias “gothic” */

 PDF_set_parameter(p, "FontOutline", "gothic=C:\\WINDOWS\\Fonts\\MSGOTHIC.TTC");

 Font_E= PDF_load_font(p, "gothic:0", 0, "winansi", ""); /* Use MS Gothic */

 Font_E= PDF_load_font(p, "gothic:1", 0, "winansi", ""); /* Use MS PGothic */

 Font_E= PDF_load_font(p, "gothic:2", 0, "winansi", ""); /* Use MS UI Gothic */


下面是一个相关的例子--C 源程序
/*******************************************************************/

/* This example demostrates the usage of host font and other fonts

/* based on Chinese Simplifed Windows.

/*******************************************************************/



#include 

#include 

#include 



#include "pdflib.h"



int main(void)

{

    PDF         *p = NULL;

    int             i = 0, j = 0, Left = 50, Top = 800;

    int             Font_E = 0, Font_CS = 0;

    char          fontfile[1024];

    char          buf[1024];

    char          TextUnicode[] = "\x80\x7B\x53\x4F\x2D\x4E\x87\x65";



    /* 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) {

	if (PDF_begin_document(p, "pdflib_cs2.pdf", 0, "") == -1) 

             {

	    printf("Error: %s\n", PDF_get_errmsg(p));

	    return(2);

	}



	PDF_set_info(p, "Creator", "pdflib_cs2.c");

	PDF_set_info(p, "Author", "myi@pdflib.com");

	PDF_set_info(p, "Title", "Output Chinese Simplify with host font and others");



        /* Start a new page. */

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

        Font_E = PDF_load_font(p, "Helvetica-Bold", 0, "winansi", "");



        /* Using host font -- C:\WINDOWS\Fonts\SimHei.ttf. 

           PDFlib is using BOM UTF8 string to calling multi-byte character string

           SimHei.ttf font name is "黑体", its corresponding BOM UTF8 string is 

           "\xEF\xBB\xBF\xE9\xBB\x91\xE4\xBD\x93" */



        Font_CS= PDF_load_font(p, "\xEF\xBB\xBF\xE9\xBB\x91\xE4\xBD\x93", 0, "unicode", "");

        /* Font_CS= PDF_load_font(p, "SimHei", 0, "unicode", ""); */

		

        PDF_setfont(p, Font_E, 20);

        PDF_show_xy(p, "SimHei font:" , Left,  Top);

        PDF_setfont(p, Font_CS, 24);

        Top-=30;

        PDF_show_xy(p, TextUnicode , Left,  Top);



        /* Using other disk-based font file that is not installed in system directory -- 

            C:\PSFONTS\CS\gkai00mp.ttf*/

        Top-=50;

        strcpy(fontfile, "C:\\PSFONTS\\CS\\gkai00mp.ttf");

        sprintf(buf, "kai=%s", fontfile);

        /* Defines kai as alias for ..\gkai00mp.ttf */

        PDF_set_parameter(p, "FontOutline", buf); 

        Font_CS= PDF_load_font(p, "kai", 0, "unicode", "");

        PDF_setfont(p, Font_E, 20);

        PDF_show_xy(p, "AR PL KaitiM GB  font:" , Left,  Top);

        PDF_setfont(p, Font_CS, 24);

        Top-=30;

        PDF_show_xy(p, TextUnicode , Left,  Top);



        /* Using TrueType collection font with index -- C:\WINDOWS\Fonts\simsun.ttc*/

        Top-=50;

        strcpy(fontfile, "C:\\WINDOWS\\Fonts\\simsun.ttc");

        sprintf(buf, "simsun=%s", fontfile);

        /* Defines AdobeSongStd as alias for ..\AdobeSongStd-Light.otf 

        This only need to claim once will be sufficient to 

		configure all fonts in simsun.ttc*/

        PDF_set_parameter(p, "FontOutline", buf); 

        /* TTC files contain multiple separate fonts.

        Address 1st font by appending a colon character and 0 after alias simsun */

        Font_CS= PDF_load_font(p, "simsun:0", 0, "unicode", "");

        PDF_setfont(p, Font_E, 20);

        PDF_show_xy(p, "simsun:0 font:", Left, Top);

        PDF_setfont(p, Font_CS, 24);

        Top-=30;

        PDF_show_xy2(p, TextUnicode, 8, Left,  Top);



        /*Address 2nd font by appending a colon character and 1 after alias simsun */

        Top-=50;

        Font_CS= PDF_load_font(p, "simsun:1", 0, "unicode", "");

        PDF_setfont(p, Font_E, 20);

        PDF_show_xy(p, "simsun:1 font:", Left, Top);

        PDF_setfont(p, Font_CS, 24);

        Top-=30;

        PDF_show_xy2(p, TextUnicode, 8, Left,  Top);



        /* End of page. */

        PDF_end_page_ext(p, "");

        PDF_end_document(p, "");

    }



    PDF_CATCH(p) {

        printf("PDFlib exception occurred in pdflib_cs2 sample:\n");

        printf("[%d] %s: %s\n",

	    PDF_get_errnum(p), PDF_get_apiname(p), PDF_get_errmsg(p));

        PDF_delete(p);

        return(2);

    }



    PDF_delete(p);

    return 0;

}

相关链接:

PDFLIB官方网站:
PDFLIB官方下载专页:/products/pdflib/download/index.html
VC知识库PDFLIB下载:

(本篇完) 下载本文示例代码
阅读(486) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~