2.2 PDFlib 输出中文内容及命名
来源:互联网 发布:88折话费 源码 编辑:程序博客网 时间:2024/06/06 03:11
一 本例研究内容
- [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;}
0 0
- 2.2 PDFlib 输出中文内容及命名
- pdflib输出中文pdf
- 浅谈PDFlib中文输出(三)
- C++ PDFLib 8.0 中文输出
- PDFLIB打印PDF,能输出中文英文
- pdflib及PHP生成pdf文件(文件内容中有中文)
- 浅谈PDFlib中文输出(二)如何在PDFlib中使用其他简体中文字体
- 浅谈PDFlib中文输出(四)PDFlib接收的几种文本输入形式
- MFC中PDFLib组件的配置,以及中文输出测试,去水印
- No2:luacom中文内容输出BUG及修正
- 淺談PDFlib中文輸出(一)
- Servlet输出内容中文编码问题
- 使用PDFlib在PHP网页导出中文pdf文档
- PDFLib (8.0.2)中文路径处理使用UTF8
- PHP 打印输出数组内容及结构
- python中文注释及输出出错
- python 中中文编码及输出问题
- Python的变量的命名规则及打印输出语句
- WPF教程(二十九)Grid行和列
- leetcode---Reverse Bits
- java中ArrayList和LinkedList使用注意事项
- [Android 外挂字幕维护Note] MakeFile文件修改添加
- VMware中Ubuntu出现Unknown Display问题
- 2.2 PDFlib 输出中文内容及命名
- 用Android Studio编辑代码时,光标变粗
- Git基本使用整理
- JSP新闻系统跟我学之一数据库设计
- Zabbix 3配置脚本报警(二)
- Pandas Series
- zynq_devicetree
- JSP知识点
- Spring SET注入