由于开发的三维整形软件,需要提供整容的报告,所以要在虚拟整容之后提供一个PDF文档。在网上查询了许多,大家常用的就是PDFLib。如何使用Acrobat标准的简体中文字体呢?特转载收藏了一些PDF基本知识,希望以后软件开发能够用的上。
PDF文件式以其安全可靠,易于交换,及保真度高而成为电子文档的标准。PDFlib是一套在国际上非常流行的在服务器端批量生成PDF文档的功能强大的软件包。国外许多,税务,银行,水电,邮电部门用其生成PDF式的单据及报表。
对于国内用户来说,如何使用PDFlib输出简体中文会是我们最关心的问题。在这里我将于大家一起分享自己的一些心得体会,不对之处请指正,若我所说于PDFlib手册有冲突,请以手册为准。我的邮箱是 :bowriver2001@yahoo.ca 。
对于没有接触过PDFlib的朋友,如果你们感兴趣,可以从这个链接 下载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
编码以-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\x"; 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; }
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-21423-1.html
我们还应建造更多更好的军舰才符合我国的实际
好听好看