您的位置:首页 > 编程语言 > C语言/C++

C++ 操作PDFlib实例

2016-12-17 07:45 3207 查看
由于pdflib 4.02以上收费,本文就采用4.02版本进行pdf操作:

提示:若没有pdflib 4.02的库,下载网址请见;http://download.csdn.net/detail/u011269801/9713779

1、建立工程后,将except.cpp,except.h,pdflib.cpp,pdflib.h,pdflib.lib拷贝到工程目录,pdflib.dll拷贝到Debug目录。

2、添加头文件的引用  以及lib文件

#include "PDFLib.hpp"

#pragma comment(lib, "PDFLib.lib")

3、实现代码如下:

try
{
PDFlib pdf;
// 设置兼容参数
pdf.set_parameter("compatibility", "1.4");  // 兼容Acrobat 5
// 打开文档
if (pdf.open("vckbase.pdf") == -1)
throw("打开文件出错!");
// 设置文档信息
pdf.set_info("Creator", "PDF Creator");
pdf.set_info("Author", "WangJun");
pdf.set_info("Title", "Convert to PDF");
pdf.set_info("Subject", "PDF Creator");
pdf.set_info("Keywords", "vckbase.com");
// 开始A4页面
pdf.begin_page(a4_width, a4_height);
// 设置字体为12号宋体
int font_song = pdf.findfont("STSong-Light", "GB-EUC-H", 0);
pdf.setfont(font_song, 12);
// 设置起始点
pdf.set_text_pos(50, a4_height - 50);
// 设置颜色为蓝色
pdf.setcolor("fill", "rgb", 0, 0, 1, 0);
// 输出文字
pdf.show("VCKBASE.COM欢迎您!");
pdf.setcolor("fill", "rgb", 0, 0, 0, 0);
pdf.setfont(font_song, 24);
pdf.continue_text("在线杂志");
// 画两根绿线
pdf.setcolor("stroke", "rgb", 0.24f, 0.51f, 0.047f, 0);
pdf.moveto(50, a4_height - 80);
pdf.lineto(a4_width - 50, a4_height - 80);
pdf.moveto(50, a4_height - 78);
pdf.lineto(a4_width - 50, a4_height - 78);
pdf.stroke();
// 填充一个蓝色方框
pdf.setcolor("fill", "rgb", 0.04f, 0.24f, 0.62f, 0);
pdf.rect(50, 50, a4_width - 100, 70);
pdf.fill();
// 在指定位置输出文字
pdf.setcolor("fill", "rgb", 0, 1, 1, 0);
pdf.setfont(font_song, 16);
pdf.show_xy("版权所有 VCKBASE", a4_width - 280, 60);
// 打开并显示一个图像
//int img = pdf.open_image_file("jpeg", "vckbase.jpg", "", 0);
//pdf.place_image(img, 200, 400, 1);
//pdf.close_image(img);
//// 添加附件
//pdf.attach_file(a4_width - 50, 0, 0, a4_height - 150,
// "vckbase.zip", "VCKBASE", "wj", "zip", "paperclip");
// 结束本页
pdf.end_page();
// 关闭PDF文件
pdf.close();
}
catch (PDFlib::Exception &ex)
{
cerr << "错误信息:" << ex.get_message() << endl;
return -1;
}

catch (char *pStrErr)
{
cerr << pStrErr << endl;
return -1;
}

catch (...)
{
cerr << "发生未知异常!" << endl;
return -1;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PDFlib