WEB开发网
开发学院软件开发VC 浅谈PDFlib中文输出(三)PDFlib 的几种文本输出函... 阅读

浅谈PDFlib中文输出(三)PDFlib 的几种文本输出函数

 2007-03-15 21:55:59 来源:WEB开发网   
核心提示: const char *PDF_fit_textflow(PDF *p, int textflow, double llx, double lly, double urx, double ury, const char *optlist)将文本输出到相应的矩形块中,lly, llx, ur

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[] = "x80x7Bx53x4Fx2Dx4Ex87x65";
  char    TextCp936[] = "xBCxF2xCCxE5xD6xD0xCExC4";
  /* create a new PDFlib object */
  if ((p = PDF_new()) == (PDF *) 0)
  {
    printf("Couldn''t create PDFlib object (out of memory)!
");
    return(2);
  }
  PDF_TRY(p) {
  if (PDF_begin_document(p, "pdflib_cs3.pdf", 0, "") == -1)
      {
    printf("Error: %s
", 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:
");
    printf("[%d] %s: %s
",
    PDF_get_errnum(p), PDF_get_apiname(p), PDF_get_errmsg(p));
    PDF_delete(p);
    return(2);
  }
  PDF_delete(p);
  return 0;
}

上一页  1 2 

Tags:PDFlib 输出

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接