一 本例研究内容
- [1]输出中文到pdf
- [2]引用自己的字体库
- [3]设置中文文档属性
- [4]以中文命名pdf文档
二 本例缺陷:
- [1]文档属性中文乱码 该问题尝试解决失败
该问题有机会再尝试解决
三 示例
为方便学习演示,用不同的例子演示不同的使用方法
1 引用自己的字体 输出中文到PDF
《hello》示例讲解参见上篇,这里不再注释,使用方法改编自http://blog.csdn.net/weixinhum/article/details/47418975但原demo过于复杂不利于学习,本实例使用单行文本输出中文
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;
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;
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");
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;
}