C++ 操作PDFlib实例

由于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;
}



  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值