2.2 PDFlib 输出中文内容及命名

一 本例研究内容

  • [1]输出中文到pdf
  • [2]引用自己的字体库
  • [3]设置中文文档属性
  • [4]以中文命名pdf文档

二 本例缺陷:

  • [1]文档属性中文乱码 该问题尝试解决失败
    该问题有机会再尝试解决

三 示例

为方便学习演示,用不同的例子演示不同的使用方法

1 引用自己的字体 输出中文到PDF

《hello》示例讲解参见上篇,这里不再注释,使用方法改编自http://blog.csdn.net/weixinhum/article/details/47418975但原demo过于复杂不利于学习,本实例使用单行文本输出中文
#include "stdafx.h"
#include "pdflib.h"
#include <afxwin.h>



int _tmain(int argc, _TCHAR* argv[])
{
    PDF *p;
    int font;

    /* 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) {

        PDF_set_option(p, "errorpolicy=return");

        if (PDF_begin_document(p, "hello.pdf", 0, "") == -1) {
            printf("Error: %s\n", PDF_get_errmsg(p));
            return(2);
        }


        PDF_begin_page_ext(p, a4_width, a4_height, "");

        PDF_set_option(p, "FontOutline={ a=./font/simfang.ttf }");
        //这里的“./font/simfang.ttf”为字体文件的存放路径,也就是说需要将字库文件放到相应的位置,我们可以在自己的电脑里 windows/fonts里找个中文字库,在工程目录下健一个font文件夹。
        font = PDF_load_font(p, "a:0", 0, "unicode", "");//这里的a要和上条语句中的a保持一致,a:0 对于有些字体库,包含很多子库,这里a可以看成一个数组,0是索引 

        if (font == -1) {
            printf("Error: %s\n", PDF_get_errmsg(p));
            PDF_delete(p);
            return(2);
        }

        PDF_setfont(p, font, 24);

        PDF_set_text_pos(p, 50, 700);

        CString mPagenameAndDate = L"给我输出中文!";

        PDF_show(p, (char *)(mPagenameAndDate).AllocSysString());
        PDF_end_page_ext(p, "");

        PDF_end_document(p, "");
    }

    PDF_CATCH(p) {

        printf("PDFlib exception occurred in hello 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);
    system("hello.pdf");
    return 0;
}

2 创建中文名的pdf文档

本示例参考了以下两篇博客  
http://blog.csdn.net/p569354158/article/details/6567175  
http://blog.csdn.net/wzsda110/article/details/52035870
#include "stdafx.h"
#include <iostream>  

#include "pdflib.h"
#include <afxwin.h>


using namespace std;


//此函数将GBK编码转换成UTF8编码
string GBKToUTF8(const std::string& strGBK)
{
    string strOutUTF8 = "";
    WCHAR * str1;
    int n = MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, NULL, 0);
    str1 = new WCHAR[n];
    MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, str1, n);
    n = WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL);
    char * str2 = new char[n];
    WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, n, NULL, NULL);
    strOutUTF8 = str2;
    delete[]str1;
    str1 = NULL;
    delete[]str2;
    str2 = NULL;
    return strOutUTF8;
}

int _tmain(int argc, _TCHAR* argv[])
{
    PDF *p;
    int font;

    /* create a new PDFlib object */
    if ((p = PDF_new()) == (PDF *)0)
    {
        printf("Couldn't create PDFlib object (out of memory)!\n");
        return(2);
    }
    std::string chineseName = "中文名字";
    PDF_TRY(p) {

        PDF_set_option(p, "errorpolicy=return");


        //这里为中文字符串前加个Bom头,拼接成一个能被识别的字符串
        std::string fileName =  GBKToUTF8(chineseName);

        char Bom[4] = { 0xEF, 0xBB, 0xBF, 0 };
        string filenameBom = Bom;
        filenameBom = filenameBom + fileName + ".pdf";

        if (PDF_begin_document(p, filenameBom.c_str(), 0, "")   
        == -1) {
            printf("Error: %s\n", PDF_get_errmsg(p));
            return(2);
        }

        PDF_begin_page_ext(p, a4_width, a4_height, "");


        font = PDF_load_font(p, "Helvetica-Bold", 0, "host", "");

        if (font == -1) {
            printf("Error: %s\n", PDF_get_errmsg(p));
            PDF_delete(p);
            return(2);
        }

        PDF_setfont(p, font, 24);

        PDF_set_text_pos(p, 50, 700);


        PDF_show(p, "Hello ChineseName");
        PDF_end_page_ext(p, "");

        PDF_end_document(p, "");
    }

    PDF_CATCH(p) {

        printf("PDFlib exception occurred in hello 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);
    //这里拼接好中文名,再自动打开
    chineseName = chineseName + ".pdf";
    std::system(chineseName.c_str());
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值