首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> VC >> 關於VC++

PDFlib 中文輸出實戰(二):靈活使用系統字體與自定義字體

     編輯:關於VC++

前言

PDFlib 內建字體雖方便,但實際專案常需使用系統安裝字體或專案專屬字體檔。本文詳細說明如何在 PDFlib 中調用 Windows/Mac 宿主字體、磁碟字體檔、TrueType Collection (.ttc) 字體,並解決中文字體名稱跨語言環境差異的關鍵技巧,附完整 C 語言實作範例。

字體來源分類與調用方式

1. 宿主字體——系統已安裝字體

PDFlib 稱存在於系統字體目錄的 TrueType、OpenType、PostScript 字體為「宿主字體」。調用時直接使用字體名稱(非檔案名),嚴格區分大小寫。

關鍵技巧:獲取真正的字體名稱

  • Windows 下:雙擊 .ttf/.otf 檔案,視窗第一行文字(去除結尾 "TrueType"/"OpenType")即為字體名稱。
  • 範例:C:\WINDOWS\Fonts\SimHei.ttf 雙擊顯示「黑體 TrueType」→ 字體名稱為「黑體」。

跨語言環境陷阱與解決方案

環境字體檔案視窗顯示名稱PDFlib 調用字串
中文 WindowsSimHei.ttf黑體 TrueType"\xEF\xBB\xBF\xE9\xBB\x91\xE4\xBD\x93" (BOM+UTF-8)
英文 WindowsSimHei.ttfSimHei TrueType"SimHei"

解決方案:中文環境下中文字體名稱需以 BOM + UTF-8 形式傳遞給 PDF_load_font。取得方法:記事本輸入字體名 → 另存為 UTF-8 → 用十六進位編輯器複製內容。

2. 磁碟字體檔——未安裝於系統目錄

透過 FontOutline 參數建立別名,再以別名調用:

Font_CS = PDF_load_font(p,
    "C:\\Program Files\\Adobe\\Acrobat 7.0\\Resource\\CIDFont\\AdobeSongStd-Light",
    0, "unicode", "");

或使用別名方式(更靈活):

PDF_set_parameter(p, "FontOutline", "kai=C:\\PSFONTS\\CS\\gkai00mp.ttf");
Font_CS = PDF_load_font(p, "kai", 0, "unicode", "");

3. TrueType Collection (.ttc)——集合字體檔

.ttc 內含多個字體,不能直接用檔案名調用,必須使用索引方式:

  1. 先建立別名:PDF_set_parameter(p, "FontOutline", "gothic=C:\\WINDOWS\\Fonts\\MSGOTHIC.TTC");
  2. 再以 gothic:0、gothic:1、gothic:2 分別調用第 1、2、3 個字體。

若不確定 .ttc 內含字體,可用字體檢視工具查看,或嘗試索引 0、1、2... 直到報錯為止。

完整 C 語言範例:三種字體來源實戰

程式碼示範在簡體中文 Windows 下:

  • 調用宿主字體「黑體」(SimHei.ttf),使用 BOM+UTF-8 字串
  • 載入磁碟字體 gkai00mp.ttf (AR PL KaitiM GB) 並建立別名
  • 使用索引方式調用 simsun.ttc 中的前兩個字體
/*******************************************************************/
/* 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", "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:\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;
}

避坑指南總結

  1. 字體名 ≠ 檔案名:務必雙擊字體檔確認真實字體名稱。
  2. 中文環境必用 BOM+UTF-8:中文字體名稱傳遞給 PDFlib 時需加 BOM (\xEF\xBB\xBF)。
  3. .ttc 必須用索引:建立別名後以 alias:0、alias:1 方式調用。
  4. 編碼統一用 unicode:搭配系統字體時,PDF_load_font 的 encoding 參數建議統一使用 "unicode"。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved