浅谈PDFlib中文输出(二)如何在PDFlib中使用其他简体中文字体
2007-03-15 21:55:57 来源:WEB开发网下面是一个相关的例子--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[] = "x80x7Bx53x4Fx2Dx4Ex87x65";
/* 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_cs2.pdf", 0, "") == -1)
{
printf("Error: %s
", PDF_get_errmsg(p));
return(2);
}
PDF_set_info(p, "Creator", "pdflib_cs2.c");
PDF_set_info(p, "Author", "myi@pdflib.com");
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:WINDOWSFontsSimHei.ttf.
PDFlib is using BOM UTF8 string to calling multi-byte character string
SimHei.ttf font name is "黑体", its corresponding BOM UTF8 string is
"xEFxBBxBFxE9xBBx91xE4xBDx93" */
Font_CS= PDF_load_font(p, "xEFxBBxBFxE9xBBx91xE4xBDx93", 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:PSFONTSCSgkai00mp.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:WINDOWSFontssimsun.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:
");
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;
}
更多精彩
赞助商链接