podofo 中文乱码
1、为了方便,在文件头中加入这个预编译宏,表示把所有的字符当成utf8编码
#if defined(_MSC_VER) && (_MSC_VER >= 1600)
# pragma execution_character_set("utf-8")
#endif
2、设置字体,好像pdf中,只有这个字体支持中文
const PdfEncoding* pp = PdfEncodingFactory::GlobalIdentityEncodingInstance();
PdfFont* pFont = document.CreateFont("SimHei", true, true, false, pp);
3、告诉PdfString 将字符串当成utf8编码解析
double w = pPage->GetPageSize().GetWidth();
double h = pPage->GetPageSize().GetHeight();
painter.DrawText( 10, 10, "Hello World!" );
PdfString str((pdf_utf8* )"测试podofo显示中文文本显示是否为乱码! \n \n");
painter.DrawMultiLineText( 0, 0, w, h, str);
4、下面我以podofo的helloworld为例子,helloworld是一个简单生成pdf的demo,我结合这个demo讲述一下整个实现过程,打开Podofo的vs2010工程,这个工程是基于podofo 0.9.3版本的源码生成的,在下图可以看到Helloworld这个工程
5、将上面的全部代码段写入到源码中去
const PdfEncoding* pp = PdfEncodingFactory::GlobalIdentityEncodingInstance();
PdfFont* pFont = document.CreateFont("SimHei", true, true, false, pp);//Helvetica
painter.SetFont( pFont );
double w = pPage->GetPageSize().GetWidth();
double h = pPage->GetPageSize().GetHeight();
painter.DrawText( 10, 10, "Hello World!" );
PdfString str((pdf_utf8* )"测试podofo显示中文文本显示是否为乱码! \n \n");
painter.DrawMultiLineText( 0, 0, w, h, str);
6、编译helloworld工程,运行helloworld,会在工程目录里生成一个output.pdf的文档,打开output.pdf文档,如下图所示,中文显示正常