由于开发的三维整形软件,需要提供整容的专业报告,所以要在虚拟整容之后提供一个PDF文档。在网上查询了许多,大家常用的就是PDFLib。如何使用Acrobat标准的简体中文字体呢?特转载收藏了一些PDF基本知识,希望以后软件开发能够用的上。
PDF文件格式以其安全可靠,易于交换,及保真度高而成为电子文档的标准。PDFlib是一套在国际上非常流行的在服务器端批量生成PDF文档的功能强大的软件包。国外许多政府,税务,银行,水电,邮电部门用其在线生成PDF格式的单据及报表。
对于国内用户来说,如何使用PDFlib输出简体中文会是我们最关心的问题。在这里我将于大家一起分享自己的一些心得体会,不对之处请指正,若我所说于PDFlib手册有冲突,请以手册为准。我的邮箱是 :[email protected] 。
对于没有接触过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 |
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) { 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", "[email protected]"); 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; }
下面是一个相关的例子--C 源程序
/*******************************************************************/ /* This example demostrates the usage of host font and other 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 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", "[email protected]"); 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; }
/*******************************************************************/ /* This example demostrates different ways to output Chinese Simplified text /* under 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, Right = 545; int Font_E = 0, Font_CS = 0, Font_CS2 = 0, TextFlow = 0; char TextUnicode[] = "\x80\x7B\x53\x4F\x2D\x4E\x87\x65"; char TextCp936[] = "\xBC\xF2\xCC\xE5\xD6\xD0\xCE\xC4"; /* 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_cs3.pdf", 0, "") == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); return(2); } PDF_set_info(p, "Creator", "pdflib_cs3.c"); PDF_set_info(p, "Author", "[email protected]"); PDF_set_info(p, "Title", "Different Ways To Output Chinese Simplify"); /* Start a new page. */ PDF_begin_page_ext(p, a4_width, a4_height, ""); Font_E = PDF_load_font(p, "Helvetica-Bold", 0, "winansi", ""); Font_CS = PDF_load_font(p, "STSong-Light", 0, "UniGB-UCS2-H", ""); Font_CS2 = PDF_load_font(p, "STSong-Light", 0, "GB-EUC-H", ""); /* Using PDF_set_text_pos and PDF_show functions. */ PDF_setfont(p, Font_E, 20); PDF_set_text_pos(p, Left, Top); PDF_show(p, "Using PDF_set_text_pos and PDF_show to output text:"); Top-=30; PDF_set_text_pos(p, Left+20, Top); PDF_show(p, "UniGB-UCS2-H encoding:"); PDF_setfont(p, Font_CS, 24); Top-=30; PDF_set_text_pos(p, Left+20, Top); PDF_show2(p, TextUnicode, 8); Top-=30; PDF_setfont(p, Font_E, 20); PDF_set_text_pos(p, Left+20, Top); PDF_show(p, "GB-EUC-H encoding:"); PDF_setfont(p, Font_CS2, 24); Top-=30; PDF_set_text_pos(p, Left+20, Top); PDF_show2(p, TextCp936, 8); /* Using PDF_show_xy function. */ Top-=50; PDF_setfont(p, Font_E, 20); PDF_show_xy(p, "Using PDF_show_xy to output text:" , Left, Top); Top-=30; PDF_show_xy(p, "UniGB-UCS2-H encoding:" , Left+20, Top); PDF_setfont(p, Font_CS, 24); Top-=30; PDF_show_xy2(p, TextUnicode, 8, Left+20, Top); Top-=30; PDF_setfont(p, Font_E, 20); PDF_show_xy(p, "GB-EUC-H encoding:", Left+20, Top); Top-=30; PDF_setfont(p, Font_CS2, 24); PDF_show_xy2(p, TextCp936, 8, Left+20, Top); /* Using PDF_continue_text function. */ Top-=30; PDF_setfont(p, Font_E, 20); PDF_set_text_pos(p, Left, Top); PDF_continue_text(p, "Using PDF_continue_text to output text:"); Top-=30; PDF_set_text_pos(p, Left+20, Top); PDF_continue_text(p, "UniGB-UCS2-H encoding:"); PDF_setfont(p, Font_CS, 24); PDF_continue_text2(p, TextUnicode, 8); PDF_setfont(p, Font_E, 20); PDF_continue_text(p, "GB-EUC-H encoding:"); PDF_setfont(p, Font_CS2, 24); PDF_continue_text2(p, TextCp936, 8); /* Using PDF_fit_textline function. */ Top-=140; PDF_setfont(p, Font_E, 20); PDF_fit_textline(p, "Using PDF_fit_textline to output text:", 0, Left, Top, ""); Top-=30; PDF_fit_textline(p, "UniGB-UCS2-H encoding:", 0, Left+20, Top, ""); PDF_setfont(p, Font_CS, 24); Top-=30; PDF_fit_textline(p, TextUnicode, 8, Left+20, Top, ""); /* Using PDF_create_textflow, PDF_fit_textflow and PDF_delete_textflow function. */ Top-=30; PDF_setfont(p, Font_E, 20); TextFlow = PDF_create_textflow(p, "Using PDF_create_textflow, PDF_fit_textflow and PDF_delete_textflow to output text:", 0, "fontname=Helvetica-Bold fontsize=20 encoding=winansi"); PDF_fit_textflow(p, TextFlow, Left, Top, Right, Top-60, ""); Top-=60; TextFlow = PDF_create_textflow(p, "UniGB-UCS2-H encoding:", 0, "fontname=Helvetica-Bold fontsize=20 encoding=winansi"); PDF_fit_textflow(p, TextFlow, Left+20, Top, Right, Top-30, ""); Top-=30; TextFlow = PDF_create_textflow(p, TextUnicode, 8, "fontname=STSong-Light fontsize=24 encoding=UniGB-UCS2-H textlen=8"); PDF_fit_textflow(p, TextFlow, Left+20, Top, Right, Top-30, ""); PDF_delete_textflow(p, TextFlow); /* End of page. */ PDF_end_page_ext(p, ""); PDF_end_document(p, ""); } PDF_CATCH(p) { printf("PDFlib exception occurred in pdflib_cs3 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; }