轉帖|其它|編輯:郝浩|2011-01-28 11:39:24.000|閱讀 1199 次
概述:本文主要介紹PDFlib 的幾種文本輸出函數,希望對大家有幫助。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
1.PDF_show
void PDF_show(PDF *p, const char *text)
void PDF_show2(PDF *p, const char *text, int len)
在當前坐標用當前字體及字體大小輸出文本。
PDF_show將認為字符串是以空字符結尾(NULL);若字符串有可能含有空字符(如多字節字符串),用PDF_show2。
2.PDF_show_xy
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_xy將認為字符串是以空字符結尾(NULL);若字符串有可能含有空字符(如多字節字符串),用PDF_show_xy2。
3.PDF_continue_text
void PDF_continue_text(PDF *p, const char *text)
void PDF_continue_text2(PDF *p, const char *text, int len)
在下一行用當前字體及字體大小輸出文本。
PDF_continue_xy將認為字符串是以空字符結尾(NULL);若字符串有可能含有空字符(如多字節字符串),用PDF_continue_xy2。
4.PDF_fit_textline
void PDF_fit_textline(PDF*p, const char *text, int len, double x, double y, const char *optlist)
在給出的坐標根據選擇項輸出一行文本。
若字符串是以空字符結尾(NULL),len為0;否則,給出具體字節數。
5.PDF_fit_textflow
int PDF_create_textflow(PDF *p, const char *text, int len, const char *optlist)
建立文本流對象,并預處理文本為下面的文本格式化做準備。
若字符串是以空字符結尾(NULL),len為0;否則,給出具體字節數。
const char *PDF_fit_textflow(PDF *p, int textflow, double llx, double lly, double urx, double ury, const char *optlist)
將文本輸出到相應的矩形塊中。
lly, llx, ury, urx, 分別是矩形塊左下角及右上角的縱橫坐標。
void PDF_delete_textflow(PDF *p, int textflow)
刪除文本流對象及相關數據結構。
小結
1,2, 3 組函數簡潔,直觀,易用。4,5組函數可通過對選擇項的控制而輸出更靈活的文本格式。尤其是第5組函數,是專門為多行文本設計的,可通過選項控制對齊,字間距,邊框顯示,旋轉等。但4,5組函數有個局限,在字符串是多字節時,它們只能處理Unicode類編碼。換而言之,他們不支持cp936編碼。
下面是一個相關的例子--C 源程序(下載源代碼中包含了生成的pdf文件 –PDFlib_cs3.pdf)。
/*******************************************************************/
/* 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;
}
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:網絡轉載