浅谈PDFlib中文输出(一)如何使用Acrobat标准的简体中文字体
2007-03-15 21:55:55 来源:WEB开发网本文示例源代码或素材下载
PDF文件格式以其安全可靠,易于交换,及保真度高而成为电子文档的标准。PDFlib是一套在国际上非常流行的在服务器端批量生成PDF文档的功能强大的软件包。国外许多政府,税务,银行,水电,邮电部门用其在线生成PDF格式的单据及报表。
对于国内用户来说,如何使用PDFlib输出简体中文会是我们最关心的问题。在这里我将于大家一起分享自己的一些心得体会,不对之处请指正,若我所说于PDFlib手册有冲突,请以手册为准。我的邮箱是 :bowriver2001@yahoo.ca 。
对于没有接触过PDFlib的朋友,如果你们感兴趣,可以从这个链接http://www.pdflib.com/products/pdflib/download/index.html 下载PDFlib软件包。(也可以到VC知识库工具与资源栏目下载) 在没有license的情况下,你仍可使用其所有功能,只是生成的PDF文档带有PDFlib的水印。
PDFlib提供C,C++, Java, Perl, PHP, Python, Tcl 及RealBasic的语言接口。以下所有的例子将采用C。
如何使用Acrobat 标准的简体中文字体
PDFlib自带STSong-Light,AdobeSongStd-Light-Acro,及STSongStd-Light-Acro三种简体中文字体。这三种字体同时也是Acrobat的简体中文标准字体。
以上三种字体均支持以下几种编码(Encoding):UniGB-UCS2-H,UniGB-UCS2-V,UniGB-UTF16-H,UniGB-UTF16-V,GB-EUC-H,GB-EUC-V,GBpc-EUC-H,GBpc-EUC-V,GBK-EUC-H,GBK-EUC-V,GBKp-EUC-H,GBKp-EUC-V,GBK2K-H,及GBK2K-V。各编码的定义请见下表1.1:
表1.1
Encoding | Character set and text format |
UniGB-UCS2-H UniGB-UCS2-V | Unicode (UCS-2) encoding for the Adobe-GB1 character collection |
UniGB-UTF16-H UniGB-UTF16-V | Unicode (UTF-16BE) encoding for the Adobe-GB1 character collection.Contains mappings for all characters in the GB18030-2000 character set. |
GB-EUC-H GB-EUC-V | Microsoft Code Page 936 (charset 134), GB 2312-80 character set, EUC-CN encoding |
GBpc-EUC-H GBpc-EUC-V | Macintosh, GB 2312-80 character set, EUC-CN encoding, Script Managercode 2 |
GBK-EUC-H GBK-EUC-V | Microsoft Code Page 936 (charset 134), GBK character set, GBK encoding |
GBKp-EUC-H GBKp-EUC-V | Same as GBK-EUC-H, but replaces half-width Latin characters withproportional forms and maps code 0x24 to dollar ($) instead of yuan (¥). |
GBK2K-H GBK2K-V | GB 18030-2000 character set, mixed 1-, 2-, and 4-byte encoding |
编码以-H结尾的,表示字体将会横向输出;以 –V结尾的,表示字体将会纵向输出。以Uni开头的是Unicode类编码,如果你的输入字符串是Unicode,则应选择此类编码。以GB开头的是CP936类编码,如果你的输入字符串是Code Page 936,则应选择此类编码。
在PDFlib中若想调用以上其中一种字体,可直接用字体名和相应的编码:int Font_CS;
不久,你们将会发现,字体与编码间可有非常多的组合,而PDFlib的字体功能(function)并不支持所有的组合。最为保险的组合是PDFlib自带三种字体与Unicode类编码的组合。
Font_CS = PDF_load_font(p, " STSong-Light ", 0, " ", " UniGB-UTF16-H");
下面是一个使用PDFlib自带字体及编码的C 源程序/*******************************************************************/
(本篇完)
/* This example demostrates the usage of PDFlib builtin fonts
/* based on Chinese Simplifed Windows.
/*******************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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 TextUnicode[] = "x80x7Bx53x4Fx2Dx4Ex87x65";
char TextCp936[] = "xBCxF2xCCxE5xD6xD0xCExC4";
char EncodingName[100];
static const char *ChineseFont[] =
{"STSong-Light", "AdobeSongStd-Light-Acro", "STSongStd-Light-Acro", };
static const char *Encoding[] =
{
"UniGB-UCS2-H",
"UniGB-UCS2-V",
"UniGB-UTF16-H",
"UniGB-UTF16-V",
"GB-EUC-H",
"GB-EUC-V",
"GBpc-EUC-H",
"GBpc-EUC-V",
"GBK-EUC-H",
"GBK-EUC-V",
"GBKp-EUC-H",
"GBKp-EUC-V",
"GBK2K-H",
"GBK2K-V",
};
const int fsize = sizeof ChineseFont / sizeof (char *);
const int esize = sizeof Encoding / sizeof (char *);
/* create a new PDFlib object */
if ((p = PDF_new()) == (PDF *) 0)
{
printf("Couldn''t create PDFlib object (out of memory)!
");
return(2);
}
PDF_TRY(p) {
if (PDF_begin_document(p, "pdflib_cs1.pdf", 0, "") == -1)
{
printf("Error: %s
", PDF_get_errmsg(p));
return(2);
}
PDF_set_info(p, "Creator", "pdflib_cs1.c");
PDF_set_info(p, "Author", "bowriver2001@yahoo.ca");
PDF_set_info(p, "Title", "Output Chinese Simplify with PDFlib builtin font");
Font_E = PDF_load_font(p, "Helvetica-Bold", 0, "winansi", "");
for (i = 0; i < fsize; i++)
{
/*Start a new page. */
Top = 800;
PDF_begin_page_ext(p, a4_width, a4_height, "");
PDF_setfont(p, Font_E, 24);
PDF_show_xy(p, ChineseFont[i] , Left + 50, Top);
Top -= 30;
for (j = 0; j < esize; j++)
{
Font_CS = PDF_load_font(p, ChineseFont[i], 0, Encoding[j], "");
PDF_setfont(p, Font_E, 12);
strcpy(EncodingName, "");
strcat(EncodingName, Encoding[j]);
strcat(EncodingName, ":");
PDF_show_xy(p, EncodingName , Left, Top);
PDF_setfont(p, Font_CS, 12);
if (strstr(Encoding[j], "-H") != NULL)
{
/* It''s horizontal encoding. */
Top -= 15;
}
if (strstr(Encoding[j], "UniGB") != NULL)
{
/* It''s unicode encoding. */
PDF_show_xy2(p, TextUnicode, 8, Left, Top);
}
else
{
/* It''s code page 936 encoding. */
PDF_show_xy2(p, TextCp936, 8, Left, Top);
}
if (strstr(Encoding[j], "-H") != NULL)
{
/* It''s horizontal encoding. */
Top -= 25;
}
else
{
/* It''s vertical encoding. */
Top -= 65;
}
} /* for */
/* End of page. */
PDF_end_page_ext(p, "");
} /* for */
PDF_end_document(p, "");
}
PDF_CATCH(p) {
printf("PDFlib exception occurred in pdflib_cs1 sample:
");
printf("[%d] %s: %s
",
PDF_get_errnum(p), PDF_get_apiname(p), PDF_get_errmsg(p));
PDF_delete(p);
return(2);
}
PDF_delete(p);
return 0;
}
更多精彩
赞助商链接