-- 如何使用Acrobat標准的簡體中文字體
作者: Michelle Yi
下載源代碼
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;
Font_CS = PDF_load_font(p, " STSong-Light ", 0, " ", " UniGB-UTF16-H");
不久,你們將會發現,字體與編碼間可有非常多的組合,而PDFlib的字體功能(function)並不支持所有的組合。最爲保險的組合是PDFlib自帶三種字體與Unicode類編碼的組合。
下面是一個使用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[] = "\x80\x7B\x53\x4F\x2D\x4E\x87\x65";
char TextCp936[] = "\xBC\xF2\xCC\xE5\xD6\xD0\xCE\xC4";
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)!\n");
return(2);
}
PDF_TRY(p) {
if (PDF_begin_document(p, "pdflib_cs1.pdf", 0, "") == -1)
{
printf("Error: %s\n", 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:\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;
}