浅谈PDFlib中文输出(三)

来源:互联网 发布:windows安装nginx 编辑:程序博客网 时间:2024/06/08 03:25
-- PDFlib 的几种文本输出函数
   
   作者:Michelle Yi
   下载源代码
   
   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;
  }
   相关链接:
   
   PDFLIB官方网站: http://www.pdflib.com
   PDFLIB官方下载专页:http://www.pdflib.com/products/pdflib/download/index.html
   VC知识库PDFLIB下载:http://www.vckbase.com/tools
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 成绩考的不好怎么办读技校有用吗 孩子大学挂科太多家长应该怎么办 中专升大专的入学考没考上怎么办 小孩摔跤额头出了个包怎么办 小孩摔跤后脑勺出了个包怎么办 结婚后疏于关心老婆寒心了怎么办 江苏取消小高考高二学生怎么办 上海学而思家长陪读听不懂怎么办 高考报名的电话号码填错了怎么办 高考报名用的电话号码变换了怎么办 弟媳妇一个月就大闹一次怎么办 丈夫出轨我亲弟媳妇我怎么办 被山西博大泌尿医院坑了怎么办 家长反应孩子学校受欺负老师怎么办 白色衣服和牛仔裤洗变色了怎么办 生完孩子肚子上的松皮怎么办 xp电脑玩cf进入地图黑屏怎么办 爸妈吵架妈妈走了爸爸哭了该怎么办 总担心旅馆被拍视频传上网怎么办 微博买了猜冠军现在停了怎么办 脸上毛孔大有黑头怎么办小窍门去 进去精神病院出来真的疯了怎么办 房子已过户新业主不交物业费怎么办 村委会欠百姓征地补偿款不给怎么办 因为近亲人人都不看好的婚姻怎么办 碰到工作中特别积极的同事怎么办 丈夫车祸死亡妻子和孩子以后怎么办 丈夫死后妻子改嫁儿子不同意怎么办 满了60岁社保没满15年怎么办 捷豹的dpf灯亮了怎么办 朋友如新直销产品是你该怎么办 传福音接受了却被家人拦阻该怎么办 奶奶出钱由孙子抓奖中奖后怎么办 我不想学车了驾校不同意退学怎么办 2017年大学挂科面临退学怎么办 微信重新登录后东西全没了怎么办 宝宝吃鸡蛋过敏全身起红疹怎么办 180在产蛋鸡因断鸡减产怎么办 住友39熔接机熔接损耗大怎么办 支付宝实名认证刷脸失败怎么办 支付宝注册刷脸不是本人怎么办