C++ PDFLib 8.0 中文输出

PDFLib8.0.2 Z中文输出示例。

在提供的字体文件中,选择了宋体,PDFlib-CMap-1.0\resource\cmap\ 下

VC6.0  多字节编码


注意:

1.拿出需要的字体文件

这里我用的宋体,是 GB-EUC-H 和 Adobe-GB1-UCS2,这两个都不能少。放到了某个文件夹下( Debug\resource\cmap )

2.设置文件位置 ,加载字体

PDF_set_parameter(p,"SearchPath","./resource/cmap");//字体文件位置

Font_E = PDF_load_font(p, "STSong-Light", 0, "GB-EUC-H", "");//设置字体  字体加载不成功会返回为0  这里宋体

PDF_setfont(p, Font_E, 12);

// 输出文字
PDF_show(p,"欢迎您!");


代码:

#include "../PDFLib.hpp"
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>

#pragma comment(lib, "PDFLib.lib")
int main(void)
{
    PDF         *p = NULL;
    int        	i = 0, j = 0, Left = 50, Top = 800;
    int       	Font_E = 0;//字体

    /* create a new PDFlib object */
    if ((p = PDF_new()) == (PDF *) 0)
    {
        printf("Couldn''t create PDFlib object (out of memory)!\n");
        return(2);
    }
	PDF_set_parameter(p,"SearchPath","./resource/cmap");//字体文件位置
	PDF_TRY(p) 
	{
		if (PDF_begin_document(p, "pdflib_cs1.pdf", 0, "") == -1) 
		{
			printf("Error: %s\n", PDF_get_errmsg(p));
			return(2);
		}

		PDF_set_info(p, "Creator", "pdflib_cs1.c");
		PDF_set_info(p, "Author", "123456");
		PDF_set_info(p, "Title", "Output Chinese Simplify with PDFlib builtin font");
		Font_E = PDF_load_font(p, "STSong-Light", 0, "GB-EUC-H", "");//设置字体

		/*Start a new page. */
		Top = 800;
		PDF_begin_page_ext(p, a4_width, a4_height, ""); 
		PDF_setfont(p, Font_E, 12);
		Top -= 30;

		PDF_set_text_pos(p,50, a4_height - 50);

		// 设置颜色为蓝色
		PDF_setcolor(p,"fill", "rgb", 0, 0, 1, 0);

		// 输出文字
		PDF_show(p,"欢迎您!");

		PDF_setcolor(p,"fill", "rgb", 0, 0, 0, 0);
		PDF_setfont(p,Font_E, 24);
		PDF_continue_text(p,"在线杂志");

		// 画两根绿线
		PDF_setcolor(p,"stroke", "rgb", 0.24f, 0.51f, 0.047f, 0);
		PDF_moveto(p,50, a4_height - 80);
		PDF_lineto(p,a4_width - 50, a4_height - 80);
		PDF_moveto(p,50, a4_height - 78);
		PDF_lineto(p,a4_width - 50, a4_height - 78);
		PDF_stroke(p);

		// 填充一个蓝色方框
		PDF_setcolor(p,"fill", "rgb", 0.04f, 0.24f, 0.62f, 0);
		PDF_rect(p,50, 50, a4_width - 100, 70);
		PDF_fill(p);

		// 在指定位置输出文字
		PDF_setcolor(p,"fill", "rgb", 0, 1, 1, 0);
		PDF_setfont(p,Font_E, 16);
		PDF_show_xy(p,"版权所有 123456", a4_width - 280, 60);

		/* End of page. */
		PDF_end_page_ext(p, "");
		/* for */

		PDF_end_document(p, "");
	}

	PDF_CATCH(p) {
		printf("PDFlib exception occurred in pdflib_cs1 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;
}



结果



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值