在MFC项目中使用Quick PDF Library Lite读写pdf文件

简介

  Debenu Quick PDF Library是一个强大的PDF控件库,可以帮助开发者在自己的应用程序中添加处理PDF文件的功能,仅仅需要几行代码就可以创建,渲染,打印,加强安全,合并,分割和操作PDF文件。它包含超过900个函数,支持C,C++,C#,Delphi(version 4 to XE5),Objective-C,Python,PHP,Visual Basic,VB.NET,ASP,PowerBASIC,Pascal和其它语言,包括了ActiveX,DLL,LIB,Delphi和Dylib等版本。
  Debenu Quick PDF Library Lite是原控件库的免费精简版,提供了常用的功能。本文总结了在MFC项目中使用该库的常用方法,编程环境为VS2008 SP1。

安装

  首先到官网下载该库,官网地址为:http://www.debenu.com/。本文所使用的版本为11.14,下载后得到一个exe文件:debenu_quick_pdf_library_lite_en.exe。双击exe文件即可安装控件库,安装过程中会要求输入安装目录,选择合适的目录完成安装。
  安装完成后打开刚才所选择的目录,可以看到以下目录与文件。

<Import>
<Samples>
Debenu Quick PDF Library 11.14 Reference Guide.pdf
DebenuPDFLibrary64Lite1114.dll
DebenuPDFLibrary64Lite1114.tlb
DebenuPDFLibraryLite1114.dll
DebenuPDFLibraryLite1114.tlb
GettingStarted.pdf
license.pdf
readme.txt
reasons_to_upgrade.pdf
uninstall.exe

  文件GettingStarted.pdf介绍了在使用该控件库之前需要做的一些准备工作。首先以管理员身份运行命令提示符并切换到安装目录下,然后输入以下命令完成控件的注册。

regsvr32 DebenuPDFLibraryLite1114.dll

  上述命令注册的是32位版本的控件,如需使用64位版本,将命令中的DebenuPDFLibraryLite1114.dll替换为DebenuPDFLibrary64Lite1114.dll即可。该版本控件库的类名为DebenuPDFLibraryLite1114.PDFLibrary。类名中的1114代表控件的版本号,不同版本的控件库类名不同。
  控件注册完成后就可以使用了。以Microsoft Visual Studio 2008为例,首先打开一个MFC项目,然后在控件库安装目录下的Import\CPlusPlus文件夹中找到如下两个文件并复制到MFC项目的源代码目录下。

DebenuPDFLibraryLite1114.h
DebenuPDFLibraryLite1114.cpp

  接着将这两个文件添加到MFC项目中,添加完成后如果直接编译会报错。

1>e:\projects\example\example\debenupdflibrarylite1114.cpp(455) : fatal error C1010: 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include "stdafx.h"”?

  解决方法是在VS2008的解决方案资源管理器中右击DebenuPDFLibraryLite1114.cpp文件,在弹出的右键菜单中选择属性,打开属性页对话框,依次选择配置属性->C/C++->预编译头,将创建/使用预编译头选项修改为不使用预编译头
  除了以上错误外,还会出现两个警告。

1>e:\projects\example\example\debenupdflibrarylite1114.cpp : warning C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失
1>e:\projects\example\example\debenupdflibrarylite1114.h : warning C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失

  解决方法是在VS2008中分别打开两个文件debenupdflibrarylite1114.hdebenupdflibrarylite1114.cpp,按Ctrl+A选中全部内容,然后在菜单栏选择编辑->高级->设置选定内容的格式,即可转换文件编码,保存并重新编译后警告就消失了。
  编译成功后在需要使用该控件库的地方包含头文件DebenuPDFLibraryLite1114.h并定义类DebenuPDFLibraryLite1114的对象即可使用相关函数处理pdf文件。

示例程序

  在控件库的安装目录下有一个Samples文件夹,其中包含了一些使用VBScript语言调用该控件库的例子,本文使用C++语言实现了相应的功能。更多功能可以参考安装目录下的Debenu Quick PDF Library 11.14 Reference Guide.pdf文件。

Hello World

  创建一个新的pdf文件并将文本Hello world!写入文件中。

DebenuPDFLibraryLite1114 pdf;
pdf.DrawText(100, 500, _T("Hello world!"));
pdf.SaveToFile(_T("hello-world.pdf"));

Draw Text

  创建一个新的pdf文件并在其中写入文本。

DebenuPDFLibraryLite1114 pdf;

// 设置页面左上角为坐标系原点
// 0 左下角
// 1 左上角
// 2 右上角
// 3 右下角
// 其他值 左下角
pdf.SetOrigin(1);

// 在页面指定位置绘制文本
pdf.DrawText(100, 200, _T("Hello world!"));

// 在指定位置的文本框中绘制文本
// Options = 0 垂直居中对齐
// Options = 1 垂直顶部对齐
// Options = 2 垂直底部对齐
// Options = 3 垂直居中对齐,不换行
// Options = 4 垂直顶部对齐,不换行
// Options = 5 垂直底部对齐,不换行
pdf.DrawTextBox(350, 150, 200, 200, _T("This text was drawn ")
    _T("using the DrawTextBox function. Similar to the DrawText ")
    _T("function except that the alignment can be specified and ")
    _T("line wrapping occurs."), 1);

// 设置文本颜色
pdf.SetTextColor(0.9, 0.2, 0.5);

// 设置文本大小
pdf.SetTextSize(30);

pdf.DrawText(100, 100, _T("Big and Colorful."));

pdf.SaveToFile(_T("text.pdf"));

Fonts and Text

  创建一个新的pdf文件并使用不同的字体写入文本。

DebenuPDFLibraryLite1114 pdf;

// 为文档添加字体
// StandardFontID = 0 Courier
// StandardFontID = 1 CourierBold
// StandardFontID = 2 CourierBoldOblique
// StandardFontID = 3 CourierOblique
// StandardFontID = 4 Helvetica
// StandardFontID = 5 HelveticaBold
// StandardFontID = 6 HelveticaBoldOblique
// StandardFontID = 7 HelveticaOblique
// StandardFontID = 8 TimesRoman
// StandardFontID = 9 TimesBold
// StandardFontID = 10 TimesItalic
// StandardFontID = 11 TimesBoldItalic
// StandardFontID = 12 Symbol
// StandardFontID = 13 ZapfDingbats
// 返回值 0 添加失败 其他值 字体ID
int fontID1 = pdf.AddStandardFont(0);

// 选择字体
pdf.SelectFont(fontID1);

// 在页面指定位置绘制文本
pdf.DrawText(100, 700, _T("Courier"));

int fontID2 = pdf.AddStandardFont(1);
pdf.SelectFont(fontID2);
pdf.DrawText(100, 650, _T("CourierBold"));

int fontID3 = pdf.AddStandardFont(2);
pdf.SelectFont(fontID3);
pdf.DrawText(100, 600, _T("CourierBoldOblique"));

int fontID4 = pdf.AddStandardFont(3);
pdf.SelectFont(fontID4);
pdf.DrawText(100, 550, _T("CourierOblique"));

int fontID5 = pdf.AddStandardFont(4);
pdf.SelectFont(fontID5);
pdf.DrawText(100, 500, _T("Helvetica"));

pdf.SaveToFile(_T("different-fonts.pdf"));

Set Document Properties

  创建一个新的pdf文件并设置文件属性。

DebenuPDFLibraryLite1114 pdf;

// 在页面指定位置绘制文本
pdf.DrawText(50, 500, _T("Open this PDF in Adobe Reader ")
    _T("and press Ctrl + D to see the document properties for this PDF."));

// 设置文件属性
// 0 PDF版本
// 1 作者
// 2 标题
// 3 主题
// 4 关键字
// 5 生成器
// 6 创建工具
// 7 创建时间
// 8 最近更新时间
// 9 XMP dc:subject (看不懂)
// 返回值 0 失败 1 成功
pdf.SetInformation(1, _T("Debenu"));
pdf.SetInformation(2, _T("Sample Document Properties"));

pdf.SaveToFile(_T("set-document-properties.pdf"));

Image to pdf

  创建一个新的pdf文件并将图像写入文件。

DebenuPDFLibraryLite1114 pdf;

// 从文件中加载图像
pdf.AddImageFromFile(_T("image.png"), 0);

// 获取图像的宽与高
int lWidth = pdf.ImageWidth();
int lHeight = pdf.ImageHeight();

// 设置所选文档的页面大小
pdf.SetPageDimensions(lWidth, lHeight);

// 绘制图像
pdf.DrawImage(0, lHeight, lWidth, lHeight);

pdf.SaveToFile(_T("image.pdf"));

  创建一个新的pdf文件并将Web链接写入文件。

DebenuPDFLibraryLite1114 pdf;

// 设置页面左上角为坐标系原点
pdf.SetOrigin(1);

// 添加Web链接
// Options:0 无边框 1 有边框
pdf.AddLinkToWeb(200, 100, 60, 20, _T("http://www.debenu.com"), 1);

// 在链接区域内绘制文字提示有链接存在
pdf.DrawText(205, 114, _T("Click me!"));

pdf.SaveToFile(_T("web-link.pdf"));

其他程序

在指定位置添加水印

DebenuPDFLibraryLite1114 pdf;

// 加载pdf文件
pdf.LoadFromFile(_T("license.pdf"), _T(""));

// 选择指定页面
pdf.SelectPage(2);

// 设置页面左上角为坐标系原点
pdf.SetOrigin(1);

// 加载png图像
// Option = 6 表示加载图像时包含alpha通道
pdf.AddImageFromFile(_T("image.png"), 6);

pdf.DrawImage(100, 100, pdf.ImageWidth(), pdf.ImageHeight());

pdf.SaveToFile(_T("image_water.pdf"));
  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值