pdf之五 简体中文输出

本文介绍了PDFlib库如何使用人工字形生成功能,模拟创建粗体、斜体和粗斜体等字体样式。这个功能适用于Acrobat标准字体,特别是简体中文的STSong-Light字体。示例C程序展示了如何在PDF文档中应用这些样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

般来说, 每种基本字体,都会有在其基础上变化字形的附加字体。比如,字体Arial, 就有其附加字体Arial Bold(粗体), ArialItalic(斜体), 及Arial BoldItalic(粗斜体)。一般你都可以找到或购买到相应的附加字体。
   但有时为了应急,或对字体字形没有非常严格的要求。在这样的情况下,我们可以采用人工字形生成(Artificial font styles)。Artificial font styles是Acrobat的一个功能,它根据基本字形而模拟生成粗体,斜体及粗斜体。PDFlib支持这一功能,并遵守Acrobat对此功能的限制。目前此功能之局限于:
   1.Acrobat标准字体, 就简体中文来说也就是PDFlib自带的STSong-Light,AdobeSongStd-Light-Acro,及STSongStd-Light-Acro三种简体中文字体。
   2.PDFlib可以访问的.otf OpenType字体,并使用表1.1的编码(见《浅谈PDFlib中文输出(一)》), 且“embedding”参数设为假。

   下面是一个相关的例子--C源程序(附上生成的pdf文件–PDFlib_cs5.pdf)。


   
   
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include"pdflib.h"
    intmain(void)
    {
   PDF             *p = NULL;
   int                Font_H = 0, Font_CS = 0, Left = 50, y =700;
    constint       INCRY = 25;
    constchar    TextUnicode[] = "\x80\x7B\x53\x4F\x2D\x4E\x87\x65";
    constint       TEXTLEN = 8;
   
    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_cs5.pdf", 0, "") == -1)
    {
   printf("Error: %s\n", PDF_get_errmsg(p));
   return(2);
    }
   PDF_set_info(p, "Creator", "pdflib_cs5.c");
   PDF_set_info(p, "Author", "myi@pdflib.com");
   PDF_set_info(p, "Title", "Usage of Artificial font styles");
   
   PDF_begin_page_ext(p, a4_width, a4_height, "");
    Font_H =PDF_load_font(p, "Helvetica-Bold", 0, "winansi", "");
   PDF_setfont(p, Font_H, 24);
   PDF_show_xy(p, "Artificial Font Styles", Left +100,  y);
   
    y -= 2 *INCRY;
   PDF_setfont(p, Font_H, 14);
   PDF_show_xy(p, "Normal", Left,  y);
    y -=INCRY;
    Font_CS =PDF_load_font(p, "STSong-Light", 0, "UniGB-UCS2-H", "");
   PDF_setfont(p, Font_CS, 14);
   PDF_show_xy2(p, TextUnicode, TEXTLEN, Left, y);
   
    y -= 2 *INCRY;
   PDF_setfont(p, Font_H, 14);
   PDF_show_xy(p, "Italic", Left,  y);
    y -=INCRY;
    Font_CS =PDF_load_font(p, "STSong-Light", 0, "UniGB-UCS2-H", "fontstyleitalic");
   PDF_setfont(p, Font_CS, 14);
   PDF_show_xy2(p, TextUnicode, TEXTLEN, Left, y);
   
    y -= 2 *INCRY;
   PDF_setfont(p, Font_H, 14);
   PDF_show_xy(p, "Bold", Left,  y);
    y -=INCRY;
    Font_CS =PDF_load_font(p, "STSong-Light", 0, "UniGB-UCS2-H", "fontstylebold");
   PDF_setfont(p, Font_CS, 14);
   PDF_show_xy2(p, TextUnicode, TEXTLEN, Left, y);
   
    y -= 2 *INCRY;
   PDF_setfont(p, Font_H, 14);
   PDF_show_xy(p, "Bold-italic", Left,  y);
    y -=INCRY;
    Font_CS =PDF_load_font(p, "STSong-Light", 0, "UniGB-UCS2-H",
    "fontstylebolditalic");
   PDF_setfont(p, Font_CS, 14);
   PDF_show_xy2(p, TextUnicode, TEXTLEN, Left, y);
   
   PDF_end_page_ext(p, "");
   PDF_end_document(p, "");
    }
    PDF_CATCH(p){
   printf("PDFlib exception occurred in pdflib_cs5 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);
    return0;
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值