PDFlib 提供多組文本輸出函數,從簡單的單行輸出到複雜的多行排版,滿足不同場景需求。本文系統介紹五組核心函數的特性、適用場景與限制,並提供完整 C 語言範例,展示在簡體中文環境下如何靈活運用這些函數輸出中英文混合文本。
void PDF_show(PDF *p, const char *text);
void PDF_show2(PDF *p, const char *text, int len);
void PDF_show_xy(PDF *p, const char *text, double x, double y);
void PDF_show_xy2(PDF *p, const char *text, int len, double x, double y);
PDF_show/PDF_show_xy 假設字串以 NULL 結尾;若字串可能包含空字符(如多位元組編碼),必須使用 PDF_show2/PDF_show_xy2 並顯式指定長度。void PDF_continue_text(PDF *p, const char *text);
void PDF_continue_text2(PDF *p, const char *text, int len);
void PDF_fit_textline(PDF *p, const char *text, int len, double x, double y, const char *optlist);
optlist 選項控制字體、字號、顏色、對齊、旋轉、字間距等豐富排版效果。len=0;否則傳入實際位元組數。int PDF_create_textflow(PDF *p, const char *text, int len, const char *optlist);
const char *PDF_fit_textflow(PDF *p, int textflow, double llx, double lly, double urx, double ury, const char *optlist);
void PDF_delete_textflow(PDF *p, int textflow);
PDF_create_textflow 建立文本流物件並預處理,再用 PDF_fit_textflow 將文本流倒入矩形區域(llx,lly 左下角,urx,ury 右上角),最後用 PDF_delete_textflow 釋放資源。| 函數組 | 優點 | 缺點/限制 | 建議場景 |
|---|---|---|---|
| PDF_show 系列 | 簡單、直觀、支援所有編碼 | 無排版控制,手動管理座標 | 簡單標籤、固定位置單行文字 |
| PDF_continue_text | 自動換行 | 排版控制有限 | 簡單多行連續輸出 |
| PDF_fit_textline | 豐富單行排版選項 | 僅支援 Unicode 編碼 | 需要精細控制的單行文字 |
| PDF_fit_textflow | 專業多行排版、功能最完整 | 僅支援 Unicode 編碼;API 較複雜 | 段落、報表、圖文混排等複雜排版 |
程式碼展示在簡體中文 Windows 下,分別使用 PDF_show、PDF_show_xy、PDF_continue_text、PDF_fit_textline、PDF_fit_textflow 輸出 Unicode (UniGB-UCS2-H) 與 CP936 (GB-EUC-H) 兩種編碼的「簡體中文」四字,直觀對比各函數效果與編碼相容性。
/*******************************************************************/
/* 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", "myi@pdflib.com");
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;
}
UniGB-UCS2-H 或 UniGB-UTF16-H,可完整支援所有 PDFlib 文本函數。PDF_show 系列函數,放棄進階排版功能。PDF_fit_textflow 架構,預留未來擴展彈性。