我的环境是VS2008
首先在网上下载PDFLIB库(百度搜就有,自己下) 先在解决方案里添加4个文件,分别是pdflib.lib pdflib.h pdflib.reg pdflib.dll
下面直接上代码~~~
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include "pdflib.h"
#pragma comment(lib, "pdflib.lib")
</pre><pre name="code" class="cpp">
int Send_Pdf() //可以传参进来
{
//初始化
PDF *p;
int font,font1;
int font_ch;
int font_ch1;
// 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)
{
// This means we must check return values of load_font() etc.
PDF_set_parameter(p, "errorpolicy", "return");
PDF_set_parameter(p, "hypertextencoding", "host"); //--(增加位置)
if (PDF_begin_document(p, "hellonew1.pdf", 0, "") == -1)
{
printf("Error: %s\n", PDF_get_errmsg(p));
return(2);
}
PDF_set_info(p, "Creator", "hello.c");
PDF_set_info(p, "Author", "Thomas Merz");
PDF_begin_page_ext(p, a4_width, a4_height, "");
//***************************输出中英文
// Change "host" encoding to "winansi" or whatever you need!
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);
}
</pre><pre name="code" class="cpp"><pre name="code" class="cpp"> PDF_save(p);
PDF_setfont(p, font, 12);
PDF_set_text_pos(p, 10, 803);
PDF_show(p, "Hello, world!/* -+=/*-- ");
PDF_set_parameter(p, "FontOutline", "MKZ1=C:\\WINDOWS\\Fonts\\SIMHEI.TTF");
font_ch = PDF_load_font(p, "MKZ1:0", 0, "unicode", "");
if (font_ch == -1)
{
printf("Error: %s\n", PDF_get_errmsg(p));
PDF_delete(p);
return 0; }
PDF_setfont(p, font_ch, 12);
PDF_set_text_pos(p, 100, 100);
CString strText = _T("六六六六六六!:但不能包含数字和英文字符,输出时还要处理一下");
PDF_continue_text(p, (LPSTR)(LPCTSTR)strText);
PDF_restore(p);
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);
return 0;
}