pdf之一 中文输出
PDF文件格式以其 安全可靠,易于 交换,及保真度高而成为电子文档的标准。PDFlib是一套在国际上非常流行的在 服务器端批量生成PDF文档的功能强大的软件包。国外许多政府,税务,银行,水电,邮电部门用其在线生成PDF格式的单据及报表。如何使用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 charactercollection |
UniGB-UTF16-H UniGB-UTF16-V | Unicode (UTF-16BE) encoding for the Adobe-GB1 charactercollection.Contains mappings for all characters in the GB18030-2000character set. |
GB-EUC-H GB-EUC-V | Microsoft Code Page 936 (charset 134), GB 2312-80 characterset, EUC-CN encoding |
GBpc-EUC-H GBpc-EUC-V | Macintosh, GB 2312-80 character set, EUC-CN encoding, ScriptManagercode 2 |
GBK-EUC-H GBK-EUC-V | Microsoft Code Page 936 (charset 134), GBK character set, GBKencoding |
GBKp-EUC-H GBKp-EUC-V | Same as GBK-EUC-H, but replaces half-width Latin characterswithproportional forms and maps code 0x24 to dollar ($) instead ofyuan (¥). |
GBK2K-H GBK2K-V | GB 18030-2000 character set, mixed 1-, 2-, and 4-byteencoding |
编 码以-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 源程序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pdflib.h"
int main(void)
{
*p = NULL;
inti = 0, j = 0, Left = 50, Top = 800;
intFont_E = 0, Font_CS = 0;
charTextUnicode[] = "\x80\x7B\x53\x4F\x2D\x4E\x87\x65"; char TextCp936[] = "\xBC\xF2\xCC\xE5\xD6\xD0\xCE\xC4";
charEncodingName[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 intfsize = sizeof ChineseFont / sizeof (char *);
const intesize = sizeof Encoding / sizeof (char *);
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++)
{
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)
{
Top -= 15; } if (strstr(Encoding[j], "UniGB") != NULL) { PDF_show_xy2(p, TextUnicode, 8, Left,Top); } else
{ PDF_show_xy2(p, TextCp936, 8, Left,Top); }
if (strstr(Encoding[j], "-H") != NULL)
{ Top -= 25; } else { Top -= 65;
}
}
PDF_end_page_ext(p, ""); }
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;
}