c++ pdflib 生成中文内容

步骤一:新建控制台项目
步骤二:pdflib.lib和pdflib.dll放到指定位置和配置.lib引用信息
步骤三:增加对应的相应字体资源,这个是关键一步,pdf使用cmap进行资源映射,找到指定的字体,Adobe-GB1-UCS2是pdf中文字体标准库。

源码如下:

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>

#include "pdflib.h"

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, "AdobeSongStd-Light-Acro", 0, "GB-EUC-H", "");//设置字体 STSong-Light

        /*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;
}

运行效果:

这里写图片描述

参考文章:
https://blog.csdn.net/wzsda110/article/details/52024305

完整资源和代码下载路径:
https://download.csdn.net/download/xiao3404/10517624

猜你喜欢

转载自blog.csdn.net/xiao3404/article/details/80905788
今日推荐